0

我将 viewControllerB 从 A 推为,

    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。现在需要再次推送 A,但再次初始化 B 或在 B 上调用函数以传递新的变量。以下选项可能有效,但我没有成功,

  1. 释放controller2 and = nil,但是没有执行IF语句,因为controller2仍然活跃!
  2. 在 viewControllerB 上调用函数以便在没有 init 的情况下传递新的 pars,但不调用函数。

做错了什么?谢谢。

4

2 回答 2

1

The following code ensures that every time you navigate from A -> B -> A (via the Nav Controller's back button) -> B, B's init method is called every time (I'm using ARC here... if you aren't, let me know and I'll edit the example):

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's button action method:

- (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];
}
于 2012-11-23T13:36:15.237 回答
0

尝试使用NSNotificationCenter 检查正确描述它的这个链接。

在 Objective-C 中通过 NSNotificationCenter 发送和接收消息?

在您的 A 类中使用此代码viewDidLoad

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

在 B 类弹出函数中使用以下代码。

 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];
于 2012-11-23T13:26:01.413 回答