带有自定义init数据的xcode pushviewcontroller

时间:2012-11-23 13:08:32

标签: iphone objective-c ios xcode cocoa-touch

我正在从A as推送viewControllerB,

    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并且= nil,但因为controller2仍处于活动状态而未执行IF句子!
  2. 在viewControllerB上调用函数,以便在没有init的情况下传递新的分析,但不调用函数。
  3. 出错了什么?感谢。

2 个答案:

答案 0 :(得分:1)

以下代码可确保您每次从A - >导航时B - > 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];
}

答案 1 :(得分:0)

尝试使用NSNotificationCenter check 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];
相关问题