Storyboard是iOS5的新特性。使用Storyboard能够更方便的管理应用的界面,同时为视图间的跳转提供了清晰的脉络。app
任什么时候候你均可以在你的项目中添加Storyboard,无论你建立项目的时候有没有勾选“Usb Storyboard”选项,以下图所示。框架
若是你建立的时候勾选了该选项,那么恭喜你,你已经有了一个Storyboard,而且被设为了Main Storyboard,以下图所示。ide
同时你的代理类是这样的:atom
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {spa
return YES;代理
}code
这跟之前可大不同了!这是由于程序会自动加载Main Storyboard。若是你把上面的Main Storyboard项清空了,那咱们的MainStoryboard.storyboard就是一个孤立的文件了,你必须手动加载它,因此上面的代码也 得改一改,以下:orm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions对象
{事件
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
上面加粗的两句比较重要,前一句是从文件建立UIStoryboard实例对象,后一句则是从这个对象里获取“初始视图控制器”(一个 storyboard里只能够设置一个初始视图控制器,至关于这个storyboard的入口,后面还会说到)。 但若是你建立项目的时候没有勾选“Use Storyboard”项,那你就要手动添加Storyboard了,以下图所示。
手动建立的Storyboard和自动建立的没有任何不一样,打开咱们刚刚建立的Storyboard,里面什么都没有,咱们不妨拖拽一个Navigation Controller到里面。
能够看到咱们一次拖出了两个视图控制器,这是由于Navigation Controller须要一个rootViewController。注意我标注的两个小箭头,左边的表明这是一个“初始视图控制器”,右边的表明两个视 图控制器之间的关系。选中视图控制器可查看属性。
取消勾选“Is Initial View Controller”后指向视图控制器的箭头也就消失了。再次说一下,一个Storyboard里只能有一个“初始视图控制器”。
下面要作的是在根控制器中添加一个button,而后继续拖拽一个ViewController,点击button新ViewController push进来。
首先添加button,就跟使用IB同样。此次咱们拖拽一个UITabbarController,这时以下图所示。
接下来有两种方法: 第一种:咱们能够直接在Storyboard中完成,按住ctrl从button链接到TabbarController,松开并选择push,这样咱们就建立了一个segue(UISotryboardSegue)。
保存并运行
咱们没有写任何代码就已经完成了一个简单的框架。
下面是第二种方法:咱们准备用代码来实现。(记得把上一步中push类型的segue删除。)首先建立类 FirstViewController(Subclass of UIViewController),而后将RootViewController的底层类改成FirstViewController,以下图。
继续选中TabbarController,并设置Identifier为“second”。
在FirstViewController.m中添加下面的方法并链接到button。
- (IBAction)pressed
{
UIStoryboard *board=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *nextViewController =[board instantiateViewControllerWithIdentifier:@"second"];
[self.navigationController pushViewController:nextViewController animated:YES];
}
加粗的代码展现了如何使用Identifier从Storyboard中获取指定的视图控制器。保存并运行,效果和前一种方法彻底相同。
下面咱们将完成另外一个目标:手动触发一个segue。(Storyboard里没法建立一个经过touch来触发的segue。)
拖拽一个UIViewController,按住ctrl从TabbarController的第一个分支链接到新的viewController,一样选择push。
选择刚刚建立的segue并设置Identifier为“touch to push”,而后设置新viewController的Identifier为“third”。(Identifier只是一个名称没有实际意义,能够随便设置。)
添加新类SecondViewController(Subclass of UIViewController),选中第一个分支并设置其底层类为SecondViewController。而后添加以下方法。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self performSegueWithIdentifier:@"touch to push" sender:nil];
}
明白这个方法后就能够用任何类型的事件去触发咱们在Storyboard中建立的segue了。
继续下一个目标:在segue被触发的时候传递参数到目标视图控制器。
删除TabbarController的第二个分支,拖拽一个UITableViewController,按住ctrl从 TabbarController链接到UITableViewController,松开并选择relationship。选中cell设置 Identifier为“cell”。而后再拖拽一个UIViewController,并添加一个UILabel。做以下操做。
添加类ThirdViewController(Subclass of UITableViewController)和类FourthViewController(Subclass of UIViewController),分别设置为最后两个视图控制器的底层类。
ThirdViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @”cell“;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//这里我很谨慎的对sender和目标视图控制器做了判断
if ([sender isKindOfClass:[UITableViewCell class]]) {
if ([segue.destinationViewController isKindOfClass:[FourthViewController class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
FifthViewController *nextViewController = segue.destinationViewController;
nextViewController.string = [NSString stringWithFormat:@"%d",indexPath.row];
}
}
}
FourViewController.h
@interface FifthViewController : UIViewController
{
IBOutlet UILabel *label;
}
@property (nonatomic,strong) NSString *string;
FourthViewController.m
@synthesize string;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
label.text = self.string;
}
segue包含属性sourceViewController和destinationViewController。segue在触发后但还未执行的时候会调用sourceViewController的prepareForSegue:sender:方法,sender是segue的起始点,能够是button、cell等等。这是你传递参数最好的时机,你能够利用sender参数来找到是哪一个cell被点击,利用segue参数来获取源视图控制器对象和目标视图控制器对象,这样你就有足够的信息来传递参数了。
保存并运行