iphone - 如何将数据传递给以模态方式呈现的子 UINavigationController(即通过 initWithRootViewController)

标签 iphone ios uinavigationcontroller presentmodalviewcontroller

如何将数据传递给通过“[[UINavigationController alloc] initWithRootViewController:newItemController];”模态呈现的子 UINavigationController?

这就是创建子 Controller 的方法(即本例中的 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";

关于iphone - 如何将数据传递给以模态方式呈现的子 UINavigationController(即通过 initWithRootViewController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5447538/

相关文章:

xcode - 无法存档,但 "build for archive"仍然有效(Xcode 4)

ios - 如何向已有的 Storyboard添加后退按钮和导航栏?

ios - 如何从一个导航 Controller 切换到另一个导航 Controller

iphone - @property 是 Interface Builder 所必需的吗?

ios - 设置值 :forKey crash with _sigtramp

ios - 我的项目在 tableViewCell 显示标签完成之前显示,CollectionViewCell 在 swit3 中显示内部标签?

iphone - 谁的 View 不在窗口层次结构问题中

php - Array/Dict 从 Xcode 到 JSON 到 PHP 到 mySql

iphone - 尝试运行应用程序时,xcode模拟器使xcode崩溃

ios - 从父应用程序发送消息到 WatchKit 接口(interface)