0

I have add the navigationItem.backBarButtonItem when turn to the new page like the following code , but I want to add a Timer for change some image before turn back to the first by backBarButtonItem.

    UIViewController *ReconnectView = [[AITReconnectView alloc] initWithNibName:@"AITReconnectView" bundle:nil] ;      
    ReconnectView.edgesForExtendedLayout = UIRectEdgeNone;          
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStyleBordered target:nil action:nil];               
   [self.navigationController pushViewController:ReconnectView animated:YES];

For example : When I click the backBarButtonItem , it will run the Timer for 3 second. And then turn back to the first view.

I have search for some information , but it only overwrite the new method for backBarButtonItem.

How to add a Timer in method of backBarButtonItem but retain the original method of backBarButtonItem ?

Thanks in advance.

Rambatino
  • 4,716
  • 1
  • 33
  • 56
Martin
  • 2,813
  • 11
  • 43
  • 66

2 Answers2

2

There are two ways:

  1. Create your own back button (similar like native) and selector method and assign it to button:

    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:@"BackToVcA"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(addAction:)] autorelease];
    
    self.navigationItem.rightBarButtonItem = addButton;
    
  2. Use UIViewController life cycle method, viewWillDisappear or viewDidDisappear.

Hope this is what you're looking for.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • I try to add a timer in `viewWillDisappear` or `viewDidDisappear`. But the view turn back to the first view and didn't wait for timer finish. When the view turn to the first view , the timer still count... – Martin Jun 21 '14 at 07:50
  • I know the first way which you post. But I don't know how to turn back to the first view by code. – Martin Jun 21 '14 at 07:55
0
  • If you want to delay the transition to the first view by 3 seconds, Just add, [NSThread sleepForTimeInterval:3.0]; to viewWillDisappearmethod of the second view.

  • If you want to execute method invocation in objective C, then [self performSelector:@selector(methodName) withObject:self afterDelay:3.0]; should work fine. Share code for some more information.

viral
  • 4,168
  • 5
  • 43
  • 68