如果习惯使用纯代码编程的开发者,会感觉使用StoryBoard会非常不舒服,但是看完本教程能让你快速使用StoryBoard开发。StoryBoard和StoryBoard、StoryBoard和Code视图控制器、StoryBoard和Xib视图控制器之间切换和传值。
// 根据StoryBoard名字获取StoryBoard
+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(nullabßle NSBundle *)storyboardBundleOrNil;
// 获取指定StoryBoard的第一个视图控制器
- (nullable __kindof UIViewController *)instantiateInitialViewController;
// 获取指定StoryBoard的标识符为identifier的视图控制器
- (__kindof UIViewController *)instantiateViewControllerWithIdentifier:(NSString *)identifier;
// 标识符
@property (nullable, nonatomic, copy, readonly) NSString *identifier;
// 源视图控制器
@property (nonatomic, readonly) __kindof UIViewController *sourceViewController;
// 目的视图控制器
@property (nonatomic, readonly) __kindof UIViewController *destinationViewController;
// 处理标识符为identifier的StoryBoardSegue
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
AppDelegate.m 动态加载StoryBoard,TARGETS设置中Deployment Info 项Main Interface删除main
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
self.window.rootViewController = vc;
return YES;
}
方法一:
方法二:
// StoryBoard 跳转到 StoryBoard
- (IBAction)StoryBoardCodePushButton:(id)sender
{
// 已经连线的seg
[self performSegueWithIdentifier:@"Detail" sender:self];
}
// UIViewController 方法 用于处理视图控制器之间的传值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
// 方法一,使用KVC给B 也就是目标场景传值
UIViewController *destinationController=[segue destinationViewController];
[destinationController setValue:@"StoryBoard Segue Push" forKey:@"name"];
}
else
{
// 方法2,使用属性传值,需导入相关的类.h
DetailViewController *bController=[segue destinationViewController];
bController.name=@"StoryBoard Code Push";
}
}
self视图控制器是用StoryBoard创建的
// StoryBoard 跳转到 Code
- (IBAction)StoryBoardtoCodeVCPush:(id)sender
{
CodeViewController *vc = [[CodeViewController alloc] init];
vc.name = @"StoryBoard to CodeVC Push";
[self.navigationController pushViewController:vc animated:YES];
}
self视图控制器是用Code(Xib)创建的
- (void)barButton
{
// 从Code视图控制器跳转到StoryBoard中的视图控制器 (Xib原理一样)
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
DetailViewController *vc = [story instantiateViewControllerWithIdentifier:@"VC"];
vc.name = @"Code to StoryBoard Push";
[self.navigationController pushViewController:vc animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//第一个 首页
UINavigationController *homeNav = [[UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
UITabBarItem *homeItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
homeNav.tabBarItem = homeItem;
homeNav.title = @"首页";
//第二个 主页
UINavigationController *searchNav = [[UIStoryboard storyboardWithName:@"Search" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
UITabBarItem *searchItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
searchNav.tabBarItem = searchItem;
searchNav.title = @"搜索";
//第三个 关于
UINavigationController *aboutNav = [[UIStoryboard storyboardWithName:@"About" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
UITabBarItem *aboutItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
aboutNav.tabBarItem = aboutItem;
aboutNav.title = @"关于";
// 将视图控制器添加至数组中
NSArray *viewControllers = @[homeNav,searchNav,aboutNav];
self.viewControllers = viewControllers;
}
运行工程时发现报错那是因为没有为每个Storyboard添加Storyboard Entry Point,按照下面的步骤操作即可。
- (IBAction)storyboardToAbout:(id)sender
{
UIStoryboard *story = [UIStoryboard storyboardWithName:@"About" bundle:nil];
AboutViewController *vc = [story instantiateViewControllerWithIdentifier:@"VC"];
[self.navigationController pushViewController:vc animated:YES];
}
在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的。在iphone中,segue有:push,modal,和custom三种不同的类型,这些类型的区别在与新页面出现的方式。而在ipad中,有push,modal,popover,replace和custom五种不同的类型。
// 这个类用于处理跳转动画
- (instancetype)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination
{
self = [super initWithIdentifier:identifier source:source destination:destination];
if (self)
{
}
return self;
}
- (void)perform
{
// 如果使用系统的转场动画则不注释,如果使用自定义转场动画则重写该放法。
// [super perform];
// 自定义转成动画代码部分
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;
// Create a UIImage with the contents of the destination
UIGraphicsBeginImageContext(destination.view.bounds.size);
[destination.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *destinationImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Add this image as a subview to the tab bar controller
UIImageView *destinationImageView = [[UIImageView alloc] initWithImage:destinationImage];
[source.parentViewController.view addSubview:destinationImageView];
// Scale the image down and rotate it 180 degrees (upside down)
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(0.1, 0.1);
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(M_PI);
destinationImageView.transform = CGAffineTransformConcat(scaleTransform, rotateTransform);
// Move the image outside the visible area
CGPoint oldCenter = destinationImageView.center;
CGPoint newCenter = CGPointMake(oldCenter.x - destinationImageView.bounds.size.width, oldCenter.y);
destinationImageView.center = newCenter;
// Start the animation
[UIView animateWithDuration:0.5f
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^(void) {
destinationImageView.transform = CGAffineTransformIdentity;
destinationImageView.center = oldCenter;
}
completion: ^(BOOL done) {
// Remove the image as we no longer need it
[destinationImageView removeFromSuperview];
// Properly present the new screen
[source.navigationController pushViewController:destination animated:nil];
}];
}