i have modal view in countdown starts when user had performed action. @ end of countdown, modal view close. following procedure have written towards goal unfortunately, causing thread problems. there way rewrite in way not have potential thread issues?
- (void)countdown { static int = 3; if (i == 3) { i--; uiimage *three = [uiimage imagenamed:@"three"]; countdownflag = [[uiimageview alloc] initwithimage:three]; countdownflag.frame = cgrectmake(0, 370, countdownflag.frame.size.width, countdownflag.frame.size.height); countdownflag.center = cgpointmake(width / 2, countdownflag.center.y); [self.view addsubview:countdownflag]; [self performselector:@selector(countdown) withobject:nil afterdelay:0.5]; } else if (i == 2) { i--; uiimage *two = [uiimage imagenamed:@"two"]; [countdownflag setimage:two]; [self performselector:@selector(countdown) withobject:nil afterdelay:0.5]; } else if (i == 1) { i--; uiimage *one = [uiimage imagenamed:@"one"]; [countdownflag setimage:one]; [self performselector:@selector(countdown) withobject:nil afterdelay:0.5]; } else { [self dismissviewcontrolleranimated:yes completion:nil]; } } edit: picture shows xcode tells me problem

more edit:
my code has changed reflect vadian's answer. here update
- (void)startcountdown { nslog(@"starting count down"); counter = 3; [nstimer scheduledtimerwithtimeinterval:0.5 target:self selector:@selector(countdown:) userinfo:nil repeats:yes]; } - (void)countdown:(nstimer *)timer { if (counter == 3) { countdownflag = [[uiimageview alloc] init]; countdownflag.frame = cgrectmake(0, 370, countdownflag.frame.size.width, countdownflag.frame.size.height); countdownflag.center = cgpointmake(width / 2, countdownflag.center.y); dispatch_async(dispatch_get_main_queue(), ^{ [self.view addsubview:countdownflag]; }); } else if (counter == 0) { [timer invalidate]; [self dismissviewcontrolleranimated:yes completion:nil]; return; } nsarray *imagenamearray = @[@"", @"one", @"two", @"three"]; uiimage *image = [uiimage imagenamed:imagenamearray[counter]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.countdownflag setimage:image]; }); counter--; } i new guess problem lies in code calls countdown. here code this.
- (void)changelanguage:(bool) ismyanmar { if (hasrun == yes) { return; } hasrun = yes; nsuserdefaults * userdefaults = [nsuserdefaults standarduserdefaults]; [userdefaults setbool:ismyanmar forkey:@"myanmar"]; [userdefaults synchronize]; [self updatecurrentlanguagetext]; if ([self ismyanmar]) { [self changelanguageflag:@"myanmarflagbig"]; } else { [self changelanguageflag:@"unitedkingdomflagbig"]; } //>>>>>>>>>>>>>>>>>>>> problem here [self startcountdown]; } any code runs after changing language code fails. problem there.
edit: the thread problem gone countdown isn't happening anymore.
after have moved code in changelanguage methods call it, problem mysteriously gone. (repetitive works.)
- (void)changelanguagetoenglish { [thelock lock]; if (hasrun == yes) { [thelock unlock]; return; } hasrun = yes; [userdefaults setbool:false forkey:@"myanmar"]; [userdefaults synchronize]; [self updatecurrentlanguagetext]; if ([self ismyanmar]) { [self changelanguageflag:@"myanmarflagbig"]; } else { [self changelanguageflag:@"unitedkingdomflagbig"]; } [self startcountdown]; [thelock unlock]; } - (void)changelanguagetomyanmar { [thelock lock]; if (hasrun == yes) { [thelock unlock]; return; } hasrun = yes; [userdefaults setbool:true forkey:@"myanmar"]; [userdefaults synchronize]; [self updatecurrentlanguagetext]; if ([self ismyanmar]) { [self changelanguageflag:@"myanmarflagbig"]; } else { [self changelanguageflag:@"unitedkingdomflagbig"]; } [self startcountdown]; [thelock unlock]; } but problem countdown isn't happening anymore.
you use nstimer perform countdown , static variable counter.
the method startcountdown starts timer.
the method countdown:(nstimer *)timer called every 0.5 seconds. passed nstimer argument timer has been started. performs action according value , decrements counter. if counter 0, timer invalidated , controller dismissed.
static uint8 counter = 0; - (void)startcountdown { countdownflag = [[uiimageview alloc] init]; countdownflag.frame = cgrectmake(0, 370, countdownflag.frame.size.width, countdownflag.frame.size.height); countdownflag.center = cgpointmake(width / 2, countdownflag.center.y); [self.view addsubview:countdownflag]; counter = 3; [nstimer scheduledtimerwithtimeinterval:0.5 target:self selector:@selector(countdown:) userinfo:nil repeats:yes]; } - (void)countdown:(nstimer *)timer { if (counter == 0) { [timer invalidate]; [self dismissviewcontrolleranimated:yes completion:nil]; return; } nsarray *imagenamearray = @[@"", @"one", @"two", @"three"]; uiimage *image = [uiimage imagenamed:imagenamearray[counter]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.countdownflag setimage:image]; }); counter--; }
Comments
Post a Comment