Storyboard 简述 使用 创建 数据传送 跳转

来源:互联网 发布:淘宝客服晚班可在家上 编辑:程序博客网 时间:2024/06/16 06:06
一 、简述
Storyboard是你可以用来定义用户界面的一种新的方式,像xib。
与xib不同的是它可以同时管理多个ViewController,而且可以在Storyboard中配置ViewController 之间的跳转关系。
 
二、Storyboard使用
如果你是创建新项目,Xcode模版可以提供一个配置好的Storyboard供你使用。对于其它的应用,使用Storyboard的过程如下:
1、配置应用程序Info.plist文件
添加UIMainStoryboardFile ,值为storyboard的文件名。
删除原来的NSMainNibFile
2、像以前创建xib文件一样创建一个storyboard文件
3、配置 storyboard中的viewController
 
三、Storyboard的创建
            你可以用InterfaceBuilder 去为你的应用程序创建一个Stroyboard,一般来说一个应用使用一个 Storyboard就够了,但是如果你想创建多个也是可以的,只要你愿意。一个 Stroyboard应该至少含有一个ViewController。
            在iPhone中,对于每一个在Storyboard的ViewController都管理着一个scene,每个scene又管理着screen上的东东,但对于iPad来说,多个scene可以同时呈现在一个screen上。你可以从library中拖拽viewController到你的Storyboard上。
            当你想关联两个viewController时,你可以按着control键,用鼠标从一个ViewController中的button,table view cell…拖拽连接到另一个你想跳转到的ViewController,这样就创建了一个segue,不要忘记设置identifier哦。
          
四、 Scene之间的数据传递
            当你从当前 scene中触发一个segue的时候,系统会自动调用prepareForSegue:sender:这个方法。如果你想从一个界面切换到里另一个界面的时候传递数据,你应该override这个方法。
A---》B
想把数据  NSString A_data   从AController传到BController,则在BController中 
@property 一个NSString data
然后在AController中添加方法


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    NSLog(@"The segue id is %@", segue.identifier );    UIViewController *destination = segue.destinationViewController;      if ([destination respondsToSelector:@selector(setData:)])    {        [destination setValue:@"这是要传递的数据" forKey:@"data"];    }   }



之后,Bcontroller中的data属性,就接收到数据了。
 
五、ViewController之间的跳转
            1、如果在 Storyboard中当前的 ViewController和要跳转的ViewController之间的segue存在,则可以执行performSegueWithIdentifier:sender:这个方法实现跳转。
            2、如果目标ViewController存在Storyboard中,但是没有segue。你可以通过UIStoryboard的instantiateViewControllerWithIdentifier:这个方法获取到它,然后再用你想要的方式实现跳转,如:压栈。
            3、如果目标ViewController不存在,那就去创建它吧。
0 0
原创粉丝点击