简体   繁体   English

如何从基于Storyboard的应用程序将参数传递给UIViewController?

[英]How to pass parameter to UIViewController from Storyboard based app?

I'm using xcode 4.3 with storyboards. 我正在使用带有故事板的xcode 4.3。

this is how I am calling my secong UIViewController, called BookMenuController 这就是我调用我的secong UIViewController,名为BookMenuController

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
BookMenuController* bookControll = [sb instantiateViewControllerWithIdentifier:@"contBook"];

bookControll.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:bookControll animated:YES];

how can I pass a parameter in such case? 在这种情况下如何传递参数? I wanna pass a integer but in the future I want to pass an NSMutableArray. 我想传递一个整数,但将来我想传递一个NSMutableArray。

any thoughts ? 有什么想法吗 ?

You'd do that part the same with or without storyboards: define and declare a method (or property) on your view controller class ( BookMenuController ), and call that method (or set the property) to pass data to the view controller. 无论有没有故事板,您都可以使用相同的部分:在视图控制器类( BookMenuController )上定义和声明方法(或属性),并调用该方法(或设置属性)以将数据传递给视图控制器。

On a (more or less) unrelated note, you're not really gaining anything from using storyboards when you use this pattern to instantiate and transition to view controllers. 在(或多或少)不相关的注释上,当您使用此模式实例化并转换到视图控制器时,您并没有真正从使用故事板获得任何东西。 Part of what storyboards are designed to help you with is eliminate some of this kind of boilerplate code. 故事板的一部分旨在帮助您消除一些这种样板代码。 If the snipped you've quoted is to be invoked when the user taps on a control, you don't need any code at all -- just drag a segue in IB connecting the control to the destination view controller. 如果在用户点击控件时调用您引用的剪切,则根本不需要任何代码 - 只需在IB中拖动一个segue,将控件连接到目标视图控制器。 (You can specify the segue type and transition style in IB, too.) If you need to control when the segue happens more precisely, use performSegueWithIdentifier:sender: , and you can still offload segue setup from code to IB. (您也可以在IB中指定segue类型和过渡样式。)如果您需要更精确地控制segue何时发生,请使用performSegueWithIdentifier:sender: ,然后您仍然可以将segue设置从代码卸载到IB。 Either way, you can then call methods / set properties on the destination view controller in prepareForSegue:sender: . 无论哪种方式,您都可以在prepareForSegue:sender:调用目标视图控制器上的方法/设置属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从UIViewController访问故事板 - accessing storyboard from UIViewController 从UITabbarItem和Storyboard创建一个UIViewController - Create a UIViewController from UITabbarItem and Storyboard 如何从controller.m更新storyboard uiviewcontroller的标签/文本字段? - how to update label/textfield of storyboard uiviewcontroller from controller.m? 如何将touchesBegin从UIScrollView传递到UIViewController - how to pass touchesBegan from UIScrollView to UIViewController 如何将字符串从一个uiviewcontroller传递到另一个 - how to pass a string from one uiviewcontroller to another 如何从一个Containerview传递到另一个UIViewController - How to pass from one Containerview to Another UIViewController 从UIScrollView子视图呈现一个故事板UIViewController - present a storyboard UIViewController from a UIScrollView subview 从情节提要UIViewController将视图加载到外部显示 - Load view from storyboard UIViewController to external display 如何将Storyboard UIViewController与我的自定义UIViewController类相关联? - How do I associate a Storyboard UIViewController with my custom UIViewController class? 如何在故事板中创建UIViewController布局,然后在代码中使用它? - How to create a UIViewController layout in storyboard and then use it in code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM