目前IOS创建视图UI主要分为三种:纯代码、XIB和StoryBoard。
下面结合这三种创建UI的方式为大家介绍在IOS开发过程中的视图跳转。
一、StroyBoard内跳转
1. 在故事面板建立视图之间的连接,如下图所示:
2.跳转
// identifier 在故事的连接处设置
[self performSegueWithIdentifier:@"identifier" sender:self];
3.传参数
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// 方式一
if ([[segue identifier] isEqualToString:@"identifier"])
{
DetailViewController *controller = segue.destinationViewController;
}
// 方式二
if ([segue.destinationViewController isKindOfClass:[DetailViewController class]])
{
DetailViewController *controller = segue.destinationViewController;
}
}
二、StroryBoard外跳转
1.获取UIViewController
// 获取UIStoryboard内视图
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"migration"];
// 获取XIB(主要是UITableViewCell)内视图
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil];
UIViewController *vc = [nibs firstObject];
// 纯代码获取,也可获取xib(UIViewController)创建的视图
UIViewController *vc = [[UIViewController alloc] init];
2. 跳转
// modal开启视图
[self.navigationController presentViewController:vc animated:NO completion:nil];
// push跳转
[self.navigationController pushViewController:vc animated:YES];
三、关闭视图
// modal开启视图关闭
[self dismissViewControllerAnimated:YES completion:nil];
// push跳转
[self.navigationController popViewControllerAnimated:YES];
// push跳转,多级页面回退
NSInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self] ;
[self.navigationController popToViewController:self.navigationController.viewControllers[currentIndex - 2] animated:YES];