UINavigationController
7.8.1 添加子控制器进栈
UINavigationController *nav = [[UINavigationController alloc] init];
CTOneViewController *one = [[CTOneViewController alloc] init];
//方法一
[nav pushViewController:one animated:YES];
//方法二
[nav addChildViewController:one];
//方法三
nav.viewControllers = @[one];
//方法四
[nav showViewController:one sender:nil];
7.8.2 子控制器出栈
//方法一:撤销当前栈顶控制器,返回上一个控制器
[self.navigationController popViewControllerAnimated:YES];
//方法二:popToViewController撤销到指定控制器,只能传递栈中控制器
CTTwoViewController *vc = self.navigationController.viewControllers[1];
[self.navigationController popToViewController:vc animated:YES];
//方法三:直接返回到栈底控制器
[self.navigationController popToRootViewControllerAnimated:YES];
7.8.3 navigationItem创建
UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:selfaction:@selector(Back:)];
UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhongguo"] style:UIBarButtonItemStyleDone target:self action:@selector(Back:)];
UIBarButtonItem *buttonItem3 = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:selfaction:@selector(Back:)];
7.8.4 leftBarButtonItems创建
self.navigationItem.leftBarButtonItem = buttonItem1;
self.navigationItem.leftBarButtonItems = @[buttonItem1, buttonItem2, buttonItem3];
7.8.5 backBarButtonItem创建
传递参数
7.9 UIStoryboardSegue
1. Segue的三个属性
@property (nonatomic, readonly) NSString *identifier;//唯一标识
@property (nonatomic, readonly) id sourceViewController;//来源控制器
@property (nonatomic, readonly) id destinationViewController;//目标控制器
2. Segue分为手动型和自动型两种,在storyboard中操作,按钮直接连线到控制器的是自动型,控制器连线到控制器的是手动型。无论如何都需要跳转的,用自动型segue。条件判断后才需要跳转的,用手动型segue。
3. 手动型segue调用
1) 首先需要在storyboady中,将手动segue的identifier命名
2) 然后调用如下方法:
[self performSegueWithIdentifier:@"login2contacts" sender:nil];
4. 无论是手动型segue,还是自动型segue,执行前都会调用prepareForSegue方法,我们在此方法中进行2个控制器间的传值。
//直接赋值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CTContactsTableViewController *contact = segue.destinationViewController;
contact.navigationItem.title = [NSString stringWithFormat:@"%@的通讯录", self.textAccount.text];
}
//在执行跳转前,当前控制器在调用prepareForSegue方法后成为目标控制器的代理
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CTAddViewController * addViewController = (CTAddViewController *)segue.destinationViewController;
addViewController.delegage = self;
}
//实现目标控制器代理中的方法,可获取数据并触发相应的操作
- (void)addViewControllerBtnSave:(CTAddViewController *)addViewController
{
CTContact *contact = addViewController.contact;
[self.contacts addObject:contact];
}
如果需要在跳转页面的时候,做一些操作效果,可以自定义导航控制器重写push或者pop方法(重写这个方法,也可以取消一些不想要的效果),或者作为导航控制器的代理,监听跳转方法。
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated