簡體   English   中英

我如何將數據傳遞給以模態形式呈現的子UINavigationController(即,通過initWithRootViewController)

[英]how do I pass data to a child UINavigationController which is presented modally (i.e. via initWithRootViewController)

如何將數據傳遞給通過“ [[UINavigationController alloc] initWithRootViewController:newItemController];”模態呈現的子UINavigationController?

這種創建子控制器(即本例中的newItemController)的方法就是這種方式,它是通過UINavigationController initWithRootViewController方法初始化的,因此這里似乎沒有能力調用自定義newItemController init方法? 也沒有訪問newItemController實例本身來調用自定義“ setMyData”類型方法的權限?

NewItemController *newItemController = [NewItemController alloc];
newItemController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newItemController];
[self.navigationController presentModalViewController:navController animated:YES];

您問題中的代碼缺少調用NewItemController的init。 例如:

NewItemController *newItemController = [[NewItemController alloc] init];

現在,當您創建NewItemController時,您可以創建自己的init:

-(id)initWithStuff:(NSString *)example {
    self = [super init];
    if (self) {
        // do something with the example data
    }
    return self;
}

或者您可以將屬性添加到NewItemController類

// header file
@property (nonatomic, copy) NSString *example;

// .m file
@synthesize example;

// when you create the object
NewItemController *item = [[NewItemController alloc] init];
item.example = @"example string data";

關鍵是您不將數據傳遞給導航控制器,而是將其傳遞給導航控制器的根視圖控制器。

暫無
暫無

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

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