-3

Hello friends I'd be happy if you help me

I wanted to know how I make a delay of the next view?

I mean I have a view with a button, and clicking on the button I show a picture. I want to wait a few seconds after I show the pic and switch in modal to the next view

I tried a lot and it's not working

Here is example for my try :

- (IBAction)button1Click:(id)sender {
   // here after i click i want to show the pic in imageview for some seconds
    self.imageView.image = [UIImage imageNamed:@"bird.png"];
    // and then i call to the function for waiting some seconds before the view change by modal 
    [self delayView];
}

- (void)delayView {
    [NSThread sleepForTimeInterval:8.0];
}

Also i try to use :

[self performSelector : @selector() withObject : self afterDelay : 3.00];

but i didn't know exactly how to use the @selector()

Please I would appreciate your help

Raptor
  • 53,206
  • 45
  • 230
  • 366
Sh Isas
  • 1
  • 1
  • Welcome to SO. What have you tried ? show us! Please show your codes by editing your question. – Raptor Mar 04 '15 at 02:59

3 Answers3

1

If you look into performSelector documentation, you'll find that you can use it to delay a function for 3 seconds by:

[self performSelector:@selector(delayView2) withObject:nil afterDelay:3.0f];

where delayView2 is:

- (void)delayView2 {
  self.imageView.image = [UIImage imageNamed:@"bird.png"];
}

Therefore, you should change the code of the IBAction to:

- (IBAction)button1Click:(id)sender {
  [self performSelector:@selector(delayView2) withObject:nil afterDelay:3.0f];
}
Raptor
  • 53,206
  • 45
  • 230
  • 366
1

dispatch_after is preferred to the performSelector: approach, and also works similarly in Swift:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    <#code to be executed after a specified delay#>

});

And unlike your sleepForTimeInterval: approach, it won't block the main thread.

For more details see this answer.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • can you explain why `dispatched_after` is preferred to `performSelector:` ? – Raptor Mar 05 '15 at 06:14
  • 1
    In this particular case it doesn't matter, but It's generally recommended to avoid `performSelector:` because it inhibits the compiler from type checking and can cause memory leaks in some cases (see [this answer](http://stackoverflow.com/a/20058585/1445366) for a more extensive explanation). – Aaron Brager Mar 05 '15 at 06:27
  • excellent. I thought the only advantage of `dispatch_after` is that i can pass primitive types to the block. – Raptor Mar 05 '15 at 06:28
-2

I have modified your code below, try this and let me know if you face any other issues:

- (IBAction)button1Click:(id)sender {
self.imageView.image = [UIImage imageNamed:@"bird.png"];
[self performSelector:@selector(delayView) withObject:nil afterDelay:3.0f];
}

- (void)delayView {
[self.navigationController pushViewController:myNextView animated:YES];
}
Taranjit
  • 13
  • 3
  • 1
    why push view controller ? OP does not mention of it in the question. Plus, the image view is not delayed. – Raptor Mar 05 '15 at 06:16
  • hi Right, i need to click on the button and show the image for some seconds and only then to change the view in MODAL (How do i define the next modal view in the delayView function ?) , pls help me thanks – Sh Isas Mar 05 '15 at 07:45
  • Freinds can anyone help me please? I got stuck, the way you presented what happens is when I press the button it remains down for the time delay and the photo is replaced quickly in the image, and you can't see it and the new view appears quickly in modal ,so what I need is after you click the button to show the image with a delay of a few seconds and then go through to the new view in modal, please help I got stuck and I need your help. Thanks – Sh Isas Mar 06 '15 at 05:13
  • Someone can help me? , I can't continue please give me solution, thanks – Sh Isas Mar 06 '15 at 20:03