2012-11-23 33 views
0

我是從推viewControllerB如,的Xcode pushviewcontroller自定義初始化數據

if(!self.controller2){ 

       self.controller2 = [[viewControllerB alloc] initWithNibName:@"viewControllerB" bundle:nil anUser:self.idUser aidioma:self.idioma]; 
      } 

    [[self navigationController] pushViewController:self.controller2 animated:NO]; 

然後,我彈出B到A現在需要一個重新初始化,但是再次B或爲了呼籲B功能推通過新的變數。以下選項可能有效,但我沒有成功,

  1. 釋放controller2和= nil,但由於控制器2仍然處於活動狀態,因此不執行IF語句!
  2. 在viewControllerB上調用函數以便在沒有init的情況下傳遞新的parser,但函數不會被調用。

什麼是做錯了?謝謝。

回答

0

嘗試使用NSNotificationCentercheck this Link它正確地描述了它。

Send and receive messages through NSNotificationCenter in Objective-C?

使用此代碼在A級viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(receiveTestNotification:) 
     name:@"TestNotification" 
     object:nil]; 

使用下面的代碼在你的B類流行功能。

[[NSNotificationCenter defaultCenter] 
     postNotificationName:@"TestNotification" 
     object:self]; 
1

下面的代碼確保每一個你從A瀏覽時間 - 「乙 - > A(通過導航控制器的後退按鈕) - > B,B的init方法被調用每一次(我使用ARC這裏......如果你都沒有,讓我知道,我會編輯的例子):

的AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

    viewControllerA *vcA = [[viewControllerA alloc] initWithNibName:@"viewControllerA" bundle:nil]; 
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vcA]; 
    self.window.rootViewController = nav; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

viewControllerA的按鈕操作方法:

- (IBAction)moveForward:(id)sender { 
    // change this line to call your custom init method. 
    viewControllerB *vc = [[viewControllerB alloc] initWithNibName:@"viewControllerB" bundle:nil]; 
    [self.navigationController pushViewController:vc animated:YES]; 
}