1

如何将数据传递给通过“[[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];
4

2 回答 2

4

您问题中的代码缺少调用 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";
于 2011-03-27T06:14:48.027 回答
1

关键是您不要将数据传递给导航控制器,而是将其传递给导航控制器的根视图控制器。

于 2011-03-27T06:25:47.097 回答