![](/img/trans.png)
[英]How to call viewDidLoad method when back button is pressed in UINavigationController? (iPhone / iPad)
[英]How to call a method when back button on a UINavigationController is pressed? (iPhone)
當按下UINavigationController
上的后退按鈕時,如何調用方法。 我需要能夠在顯示上一個視圖之前調用一個方法。 這可能嗎? 如果是這樣,有人可以建議如何做嗎?
一種快速的解決方案是為viewWillDisappear:方法添加一個實現。 一旦viewController響應后退按鈕的按下而消失,它將被觸發。
- (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];
}
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];
}
我認為我找到了一種“干凈”的方法:
首先,將您的視圖控制器設置為符合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;
}
最后,使用UINavigationControllerDelegate協議中的此方法:
#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;
}
}
最簡單的方法是將代碼放入ViewController中,該代碼將在按下后退按鈕時顯示。 您可以使用viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Your code here
}
請注意,由於任何其他原因在顯示視圖時也會執行此操作,因此,如果只希望在按下后退按鈕時執行此視圖,則必須使用委托: UINavigationControllerDelegate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.