UINavigationController
- UINavigationController的子控制器
UINavigationController以栈的形式保存子控制器
@property(nonatomic,copy) NSArray *viewControllers;
@property(nonatomic,readonly) NSArray *childViewControllers;
使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
使用pop方法可以移除控制器
将栈顶的控制器移除
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
回到指定的子控制器
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
回到根控制器(栈底控制器)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
- 如何修改导航栏的内容
导航栏的内容由栈顶控制器的navigationItem属性决定
UINavigationItem有以下属性影响着导航栏的内容
左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;
中间的标题视图
@property(nonatomic,retain) UIView *titleView;
中间的标题文字
@property(nonatomic,copy) NSString *title;
左上角的视图
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;
UIBarButtonItem *rightBarButtonItem 右上角的视图
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
segue
- Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue)
每一个Segue对象,都有3个属性
唯一标识
@property (nonatomic, readonly) NSString *identifier;
来源控制器
@property (nonatomic, readonly) id sourceViewController;
目标控制器
@property (nonatomic, readonly) id destinationViewController;
Segue的类型
根据Segue的执行(跳转)时刻,Segue可以分为2大类型
自动型:点击某个控件后(比如按钮),自动执行Segue,自动完成界面跳转
按住Control键,直接从控件拖线到目标控制器
- 点击“登录”按钮后,就会自动跳转到右边的控制器
如果点击某个控件后,不需要做任何判断,一定要跳转到下一个界面,建议使用“自动型Segue”
手动型:需要通过写代码手动执行Segue,才能完成界面跳转
按住Control键,从来源控制器拖线到目标控制器
手动型的Segue需要设置一个标识
在恰当的时刻,使用perform方法执行对应的Segue
[self performSegueWithIdentifier:@"login2contacts" sender:nil]; // Segue必须由来源控制器来执行,也就是说,这个perform方法必须由来源控制器来调用- 如果点击某个控件后,需要做一些判断,也就是说:满足一定条件后才跳转到下一个界面,建议使用“手动型Segue”
performSegueWithIdentifier:sender:
- 利用performSegueWithIdentifier:方法可以执行某个Segue,完成界面跳转
接下来研究performSegueWithIdentifier:sender:方法的完整执行过程
[self performSegueWithIdentifier:@“login2contacts” sender:nil];
这个self是来源控制器
根据identifier去storyboard中找到对应的线,新建UIStoryboardSegue对象
设置Segue对象的sourceViewController(来源控制器)
新建并且设置Segue对象的destinationViewController(目标控制器)
调用sourceViewController的下面方法,做一些跳转前的准备工作并且传入创建好的Segue对象
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
// 这个sender是当初performSegueWithIdentifier:sender:中传入的sender
- 调用Segue对象的- (void)perform;方法开始执行界面跳转操作
- 如果segue的style是push
- 取得sourceViewController所在的UINavigationController
- 调用UINavigationController的push方法将destinationViewController压入栈中,完成跳转
- 如果segue的style是modal
- 调用sourceViewController的presentViewController方法将destinationViewController展示出来
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建导航控制器的跟控制器,也属于导航控制器的子控制器
UIViewController *vc = [[OneViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
// 导航控制器也需要一个根控制器
// 默认导航控制器把根控制器的view添加到导航控制器的view上
UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
NSLog(@"%@",navVc);
// 设置窗口的跟控制器
self.window.rootViewController = navVc;
[self.window makeKeyAndVisible];
- 如果导航控制器调用push,就会把vc添加为导航控制器的子控制器
// 跳转
[self.navigationController pushViewController:vc animated:YES];
- 回到上一个界面
// pop不是马上把控制器销毁,
[self.navigationController popViewControllerAnimated:YES];
// 返回到导航控制器的跟控制器
- (IBAction)back2Root:(id)sender {
// 注意:只能返回到栈里面的控制器
[self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
// [self.navigationController popToRootViewControllerAnimated:YES];
}
控制器的数据传递
逆传
控制器的跳转方向: A C
数据的传递方向 : C A
数据的传递方式 : 让A成为C的代理, 在C中调用A的代理方法,通过代理方法的参数传递数据给A
- 控制器之间传值:一定要注意控制器的子控件有没有加载,一定要在子控件加载完成的时候才去给子控件赋值,一般在viewDidLoad给控件赋值。
1.顺传
来源控制器传递给目的控制器:顺传
- 数据传值:
- 1.接收方一定要有属性接收
- 2.传递方必须要拿到接收方
- 步骤
1.[self performSegueWithIdentifier]
2.创建segue
3.设置来源控制器segue.sourceViewController = self
4.创建目的控制器,segue.destinationViewController = 目的控制器
5.[self prepareForSegue]跳转之前的准备操作
6.[segue perform]
7.判断下segue的类型,如果是push,拿到导航控制器push
[self.navigationController pushViewController:segue.destinationViewController animated:YES];
- 数据传值:
// 在执行跳转之前的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// 拿到下一个控件
UIViewController *vc = segue.destinationViewController;
// 可以在这里设置下一个控制器的标题
vc.title = [NSString stringWithFormat:@"%@的联系人列表", _accountField.text];
NSLog(@"%@--%@",segue.sourceViewController,segue.destinationViewController);
}
2.逆传
// 跳转之前的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// 给添加控制器传递联系人控制器属性
XMGAddViewController *addVc = segue.destinationViewController;
addVc.delegate = self;
}
@class XMGAddViewController,XMGContact;
// 代理设计
@protocol XMGAddViewControllerDelegate <NSObject>
@optional// 可选方法
- (void)addViewController:(XMGAddViewController *)addVc didClickAddBtnWithContact:(XMGContact *)contact;
@end
@interface XMGAddViewController : UIViewController
@property (nonatomic, weak) id<XMGAddViewControllerDelegate> delegate;
// 点击添加的时候调用
- (IBAction)add:(id)sender {
// 0.把文本框的值包装成联系人模型
XMGContact *c = [XMGContact contactWithName:_nameField.text phone:_phoneField.text];
// 1.通知代理做事情
// _delegate = _contactVc
if ([_delegate respondsToSelector:@selector(addViewController:didClickAddBtnWithContact:)]) {
[_delegate addViewController:self didClickAddBtnWithContact:c];
}
// 2.回到联系人控制器
[self.navigationController popViewControllerAnimated:YES];
}
- 代理实现方法
- (void)addViewController:(XMGAddViewController *)addVc didClickAddBtnWithContact:(XMGContact *)contact
{
// 把添加界面的联系人模型传递到联系人界面
// 把联系人模型保存到数组
[self.contacts addObject:contact];
// 刷新表格
[self.tableView reloadData];
}
- 私人通讯的登录逻辑
- 自动登录及记住密码
(IBAction)rmbPwdChange:(id)sender {
// 如果取消记住密码,自动登录也需要取消勾选
if (_rmbPwdSwitch.on == NO) { // 取消记住密码
// 取消自动登录
[_autoLoginSwitch setOn:NO animated:YES];
}
}
// 自动登录开关状态改变的时候调用
- (IBAction)autoLoginChange:(id)sender {
// 如果勾选了自动登录,记住密码也要勾选
if (_autoLoginSwitch.on == YES) {
[_rmbPwdSwitch setOn:YES animated:YES];
}
}
- 设置非根控制器时,返回按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// 判断是否为根控制器
if (self.viewControllers.count != 0 ) {
// 隐藏底部控件
viewController.hidesBottomBarWhenPushed = YES;
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}
[super pushViewController:viewController animated:animated];
}
- 设置导航条的内容
// 获取当前应用下所有的导航条 [UINavigationBar appearance]
// 获取哪个类下面的导航条
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
// 通过setTintColor设置导航条文字的颜色
[bar setTintColor:[UIColor whiteColor]];
// 可以跳转返回按钮文字的偏移量
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
// self -> 导航控制器
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count != 0) { // 非跟控制器hi
viewController.hidesBottomBarWhenPushed = YES;
// 设置导航条左边按钮的内容,把系统的返回按钮给覆盖,导航控制器的滑动返回功能就木有啦
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
}
[super pushViewController:viewController animated:animated];
}
/**
* 可以在这个方法中拦截所有push进来的控制器
*/
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
[super pushViewController:viewController animated:animated];
}
>