0

I want to make a button appear, then I want to show it for 1 second and after that the view controller has to change. I tried my best. but it doesn't matter where I put the sleep(1); - it always does the same. It waits for 1 second, then displays the button for a very very short time and changes to the other view controller.

Can anyone help?

My code:

[self.resultButton setTitle:category forState:UIControlStateNormal];
self.resultButton.hidden = NO;

sleep(1);

UIViewController *vc = [[UIViewController alloc] init];
vc = [self.storyboard instantiateViewControllerWithIdentifier:@"mapViewControllerID"];

[self presentModalViewController:vc animated:YES];
LilK3ks
  • 205
  • 2
  • 3
  • 13

3 Answers3

2

Use GCD's dispatch_after to accomplish this.

[self.resultButton setTitle:category forState:UIControlStateNormal];
self.resultButton.hidden = NO;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"mapViewControllerID"];
  [self presentModalViewController:vc animated:YES];
});

You should also note that the view controller assignment that you've written creates a default UIViewController and then immediately throws that object away and assigns a view controller from your storyboard.

Ian MacDonald
  • 11,433
  • 1
  • 21
  • 41
0

To achieve this you can use NSTimer to call a specific function after a period of delay and in that function put the code to show new view controller

NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector:  Selector("showController"), userInfo: nil, repeats: false)

func showController(){

    //present view controller here
}
Zell B.
  • 9,879
  • 3
  • 38
  • 49
0

I have used dispatch_after() like it is being described here

Community
  • 1
  • 1
ziogaschr
  • 76
  • 2
  • 4