![](/img/trans.png)
[英]is it possible to not dealloc a view controller when the back button is pressed in a navigation controller?
[英]Xcode: Detecting when a navigation controller implemented “back button” is pressed
情境
我有一個帶有導航控制器的應用程序。 當導航控制器將另一個控制器推到堆棧上時,在屏幕的左上角顯示后退按鈕“ <(最后一個視圖控制器的標題)”。
我需要的
我需要類似 (偽代碼)的東西...
-(void)detectedBackButtonWasPushed {
NSLog(@"Back Button Pressed");
//Do what I need done
}
題
由於此按鈕是由導航控制器創建的,而我沒有在情節提要中創建此按鈕,因此如何使“后退”按鈕“鈎住”這樣的方法?
我為奧列格(Oleg)嘗試過的例子
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"notification"];
if (viewController == vc) {
NSLog(@"BACK BUTTON PRESSED");
}
}
這是我應該怎么做嗎? 因為這不起作用。
使用viewWillDisappear
進行檢測。
-(void) viewWillDisappear:(BOOL)animated
{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
[self backButtonPressed];
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
-(void)backButtonPressed
{
NSLog(@"YEA");
}
以前,我通過將navigationBar leftItem設置為帶有自定義選擇器的后退按鈕來解決此問題,該選擇器可關閉視圖以及其他所需的操作。
我可能還會建議您查看后退按鈕項,並添加一個target:self,它會在觸摸時被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.