2012-11-23 25 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]; 
}