2014年7月9日 星期三

按下backButton所觸動的method

我總共查到四種方法:

方法一:viewWillDisappear
-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
       // back button was pressed.  We know this is true because self is no longer
       // in the navigation stack.  
    }
}

方法二:viewDidDisappear
- (void)viewDidDisappear:(BOOL)animated
{
    if (self.navigationController == nil) {
        NSLog(@"self.navigationController == nil");
    }
    if (self.navigationController.view == nil) {
        NSLog(@"self.navigationController.view == nil");
    }
    if (self.navigationController.viewControllers == nil) {
        NSLog(@"self.navigationController.viewControllers = nil");
    }
}

方法三:willMoveToParentViewController:parent
-(void)willMoveToParentViewController:(UIViewController *)parent {
    NSLog(@"This VC has has been pushed popped OR covered"); 
    if (!parent)
        NSLog(@"This happens ONLY when it's popped");
}
使用這個方法有個問題,就是不知道為什麼進入這個viewController也會觸動這個method



方法四:重新寫一個UINavigationBar,把舊的覆蓋掉
Add this custom Button on UINavigationBar , just override this button on BackButton and you can perform any action with click event.
- (void)viewDidLoad
{
        UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
        UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        //[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
        [backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
        [backbutton setTitle:@"Back" forState:UIControlStateNormal];
        backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
        UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
        [backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = back;
}
when back button clicked bellow method call and you will go to previous viewcontroller.
-(void)back_Clicked{
      [self.navigationController popViewControllerAnimated:YES];
}
出處:http://stackoverflow.com/a/13856441/3295047

不過只不過為了取得觸發按backButton的method有點太大費周章了,原出處是想要自訂UINavigationBar



方法五:下面這幾種方法也是網路上找到的,但不知道為什麼都不能觸發,原因可能是因為在tab下無法啟動,但不知道為什麼我另外開了個project還是不行,以後再來找原因吧,如果有誰知道拜託告訴我~~
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    NSLog(@"navigationController:willShowViewController:animated");
}

- (void)navigationBar:(UINavigationBar *)navigationBar
           didPopItem:(UINavigationItem *)item
{
    NSLog(@"navigationBar:didPopItem");
    
}

- (UIViewController *)popNavigationItemAnimated:(BOOL)animated
{
    NSLog(@"popNavigationItemAnimated");
    return self.parentViewController;
}

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController 
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC
{
    NSLog(@"from VC class %@", [SLWakeUpTwoViewController class]);
    if ([fromVC isKindOfClass:[SLWakeUpTwoViewController class]])
    {
        NSLog(@"Returning from popped controller");
        
    }
    return nil;
}


延伸:如何直接回到 RootViewController(跳過中間的ViewController)