使用storyboards管理views学习笔记
1 StoryBorads和scenes
1.1 stroyBorads
在ios5之前,每一个view controll都有一个nib文件,通过nib文件我们可以可视化编程我们的view。 一些开发者选择自己手写代码还定义视图的布局。
到ios5以后,apple提供一个全新的方式,简历和管理,并且把视图包含到一个文件中。 storyboard 还提供通过segue管理视图的切换。
使用storyboard,即使我们没有运行程序,我们也可以清晰的理解应用程序。
1.2 scenes
storyboard中一个设备屏幕中的内容。通常对于一个ViewController
1.3 使用storyboard例子
UIStoryboard *newStoryboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *vc1 = [newStoryboard instantiateViewControllerWithIdentifier:@"vc"];
2 使用segues在scenes中传输数据。
2.1 segues类型
-
Modal segues从底部向上滑出,再父scene上面。相当于MFC中模式对话框 ,退出代码
[self dismissViewControllerAnimated:YES completion:nil] ;
-
Push segues从右边向左边画出, 退出时从左向右消失。一般结合navigationController使用,退出时代码
[self.navigationController popViewControllerAnimated:YES];
- Popover segues 在IPad中,在父屏幕弹出部分的屏幕
-
custom segues,可以屏幕转换时候,自定义动画
@interface CKMySegue : UIStoryboardSegue @end @implementation CKMySegue -(void) perform { UIViewController *source = self.sourceViewController ; UIViewController *destination =self.destinationViewController ; [UIView transitionFromView:source.view toView:destination.view duration:0.50f options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) { NSLog(@"Transition is finished") ; }]; } @end
2.2 界面控件触发,使用prepareForSegue传输数据
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destination = segue.destinationViewController ;
//显示task的
if ([segue.identifier isEqualToString:@"CKMySegue"]) {
[destination setValue:sender forKey:@"task"] ;
}
else //创建 //书上代码有错误
// destination = [segue.destinationViewController topViewController] ;
;
[destination setValue:self forKey:@"delegate"] ;
}
2.3 直接使用performSegueWithIdentifier
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"CKMySegue" sender:self.tasks[indexPath.row]] ;
}
2.4 shouldPerformSegueWithIdentifier使用时机
- 在使用prepareForSegue,在执行一次检查,决定是否通过执行,返回FALSE。
-
在使用performSegueWithIdentifier,不会调用此函数。
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { NSLog(@"[shouldPerformSegueWithIdentifier] -- %@", identifier) ; return TRUE; }