簡體   English   中英

具有自定義初始化數據的xcode pushviewcontroller

[英]xcode pushviewcontroller with custom init data

我正在從A推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上的function以便不通過init傳遞新的par,但是不調用function。

怎么了? 謝謝。

以下代碼可確保每次從A-> B-> A(通過Nav Controller的后退按鈕)-> B導航時,每次都會調用B的init方法(如果您不這樣做,我在這里使用ARC ... t,讓我知道,我將編輯示例):

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];
}

嘗試使用NSNotificationCenter 選中正確描述此鏈接的鏈接

通過Objective-C中的NSNotificationCenter發送和接收消息?

在您的A類viewDidLoad使用此代碼:

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

在B類彈出功能中使用以下代碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM