2012-01-23 47 views

回答

15

快速解決方案是爲viewWillDisappear:方法添加實現。將盡快的viewController將響應後退按鈕PRESSION消失觸發。

- (void)viewWillDisappear:(BOOL)animated { 
    //... 
    //make you stuff here 
    //... 
} 

另一種解決方案是向後退按鈕添加自定義響應者。您可以修改您的viewController的init方法如下:

- (id)init { 
    if (self = [super init]) { 
     //other your stuff goes here 
     //... 
     //here we customize the target and action for the backBarButtonItem 
     //every navigationController has one of this item automatically configured to pop back 
     self.navigationItem.backBarButtonItem.target = self; 
     self.navigationItem.backBarButtonItem.action = @selector(backButtonDidPressed:); 
    } 
    return self; 
} 

然後您可以使用選擇器方法如下。一定要正確解僱viewController,否則你的導航控制器將不會彈出想要的。

- (void)backButtonDidPressed:(id)aResponder { 
    //do your stuff 
    //but don't forget to dismiss the viewcontroller 
    [self.navigationController popViewControllerAnimated:TRUE]; 
} 
+4

一個兩者之間的區別是,回去或穿過導航堆棧轉發當第一溶液(即'viewWillDisappear')被調用。第二種解決方案只是爲了回去。 – Matthew

+5

這通常不會正常工作,因爲您會發現backBarButtonItem爲零,除非您明確地爲其指定按鈕。無論如何,無論如何,leftBarButtonItem和backBarButtonItem都是零。 – Diziet

+3

它不工作 –

1

最簡單的方法是,把代碼中的ViewController按下後退按鈕時將提交。您可以使用viewWillAppear

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    //Your code here 
} 

注意,當提出任何其他理由認爲這也將被執行,所以如果你想只有當返回按鈕被按下,你必須使用委託這樣的情況發生: UINavigationControllerDelegate

+0

該方法會當VC出現在所有的,被稱爲不一定只是剛剛回到它 – Colin

+1

@Colin這是正確的,這取決於他想要的確切行爲,他的結果可以使用委託,如果他只在按下後退按鈕時才需要它。 –

6
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 52, 31)]; 
[backButton setBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal]; 
//[backButton setTitle:@"CLOSE" forState:UIControlStateNormal]; 
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[backButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]]; 
[backButton addTarget:self action:@selector(tappedBackButton:) forControlEvents:UIControlStateHighlighted]; 

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:backButton]; 

self.navigationItem.leftBarButtonItem = item; 
[item release]; 

創建自定義後退按鈕,並使用該選擇調用您希望的方法。還可以使用[self.navigationController popViewControllerAnimated:TRUE];

- (void)tappedBackButton:(id)button 
{ 
    // call your method here 
    [self.navigationController popViewControllerAnimated:TRUE]; 
} 
+0

請注意,當您以這種方式替換後退按鈕時,會丟失iOS 7中引入的「從左側滑動以返回到父視圖控制器」功能,該功能非常實用iPhone 6和6+上的快捷方式 –

+0

這不適用於self.navigationItem.rightBarButtonItem ...請給出另一個解決方案 – Vidhi

6

我想我發現了一個「乾淨」的方式做到這一點:

首先,設置您的視圖控制器與UINavigationControllerDelegate遵從協議

@interface MyViewController() <UINavigationControllerDelegate> 

然後,把它定義爲委託

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    // Set this View Controller as the navigation controller delegate 
    self.navigationController.delegate = self; 

} 

最後,使用此方法從UINavigationControllerDeleg吃了協議:

#pragma mark - Navigation controller delegate 
- (void)willMoveToParentViewController:(UIViewController *)parent 
{ 
    // If there is no parent, then it means that the view controller has been removed from the stack, which happens when the back button has been pressed 
    if (!parent) { 
     NSLog(@"Back button pressed"); 

     // it can be useful to store this into a BOOL property 
     self.backButtonPressed = YES; 
    } 
} 
相關問題