2010-03-13 79 views
4

這是我的代碼:檢測當導航控制器彈出根

SignupController* signupController = [[SignupController alloc] initWithNibName:@"SignupController" bundle:nil]; 
    [window addSubview:[navigationController view]]; 
    [window makeKeyAndVisible]; 

    self.navigationController.title = @"MyNavController"; 

    [self.navigationController pushViewController:signupController animated:YES]; 
    [signupController release]; 

不幸的是,我打電話pushViewController不同步,所以下一行([signupController釋放])被立即執行。

我需要檢測到母雞signupController已經彈出回到根目錄,因此我可以從註冊控制器獲取數據並進行註冊或登錄。

任何想法?

感謝

回答

2

在SignupController實施-viewWillDisappear:登記。但是如果登錄失敗,則無法阻止控制器消失。

您可能希望將SignupController作爲模型視圖控制器呈現,而「後退」按鈕不再自動顯示,因此您可以控制何時離開該頁面。

1

有你能做到這幾個方面:

  1. 給這個UINavigationController代表,並有navigationController:willShowViewController:animated:委託迴應。在該方法中,檢查並查看將要顯示的視圖控制器是否是導航控制器的viewControllers陣列中的第一個視圖控制器。這具有不能區分popToRoot和簡單的後退動作的缺點。
  2. 子類UINavigationController並重寫popToRoot方法:

    - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated { 
        //somehow notify (NSNotification, delegate, etc) that you're popping to root 
        return [super popToRootViewControllerAnimated:animated]; 
    }
  3. 實現你的根視圖的控制器viewWillAppear方法。這具有不能區分初始推動,popToRoot和簡單的後退動作的缺點。

+0

我真的很喜歡#2的想法(儘管我儘量避免在Cocoa中繼承子類,natch)。就我而言,我將表視圖控制器推入堆棧,而不是普通的VC。我當然可以繼承UITableViewController的子類,但不會響應popToRootViewControllerAnimated:在這種情況下。此外,我需要區分各種操作(列表中的#1和#3)。嗯,該怎麼做。 :\ – 2010-05-10 15:42:55

4

使用委託模式回調主控制器讓它知道註冊控制器即將退出。在導航控制器釋放之前,將註冊控制器中的值複製。

// SignupController.h 
@protocol SignupDelegate; 
@interface SignupController : UIViewController { 
    id <SignupDelegate> delegate; 
    NSString *name; 
} 
@property (nonatomic, assign) id <SignupDelegate> delegate; 
@property (nonatomic, retain) NSString *name; 
@end 
@protocol SignupDelegate 
- (void) signupControllerDidFinish:(SignupController *)controller; 
@end 

// SignupController.m 
@implementation SignupController 
@synthesize delegate; 
@synthesize name; 

- (void) viewWillDisappear:(BOOL)animated { 
    [self.delegate signupControllerDidFinish:self]; 
} 
@end 

// YourViewController.m 
- (void) signupSelected { 
    SignupController *signup = [[SignupController alloc] initWithNibName:NSStringFromClass([SignupController class]) bundle:nil]; 
    signup.delegate = self; 
    [controller.navigationController pushViewController:signup animated:YES]; 
    [signup release]; 
} 

- (void) signupControllerDidFinish:(SignupController *)signup { 
    NSLog(@"Signup with name %@", signup.name); 
} 
0

不知道如果我理解你的問題......

但是,如果我沒有...爲什麼不使用BOOL isSigningUp。

在您的根目錄上,在ViewDidLoad上,將其分配爲NO。

當你推動你的signupController時,你將BOOL賦值給YES。

然後,在你的根的viewWillAppear中,你把...

if (isSigningUp == YES) { 
    isSigningUp = NO; 
    //do registration or login 
} 

這很容易,這意味着我可能沒明白你的要求。如果是這種情況,抱歉花時間。

一切順利。

相關問題