iOS UINavigationController

来源:互联网 发布:sql增加数据 编辑:程序博客网 时间:2024/06/04 19:12

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;//导航条上面的子控件位置由系统决定,我们自己只能决定控件的尺寸UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 2000, 200, 35)];view.backgroundColor = [UIColor redColor];//导航条的标题可以是一个自定义的UIView.self.navigationItem.titleView = view;//设置导航条左边的内容为标题.//Title:设置的标题//style:样式,从ios7之后,这个地方设置什么都没有用了, 所以让它默认,它是一个枚举,直接可以写0.//target: action: 点击时调用哪个对象的哪个方法.UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:0 target:self action:@selector(back)];//设置左边的内容为图片//initWithImage:要显示的图片.//style:样式,从ios7之后,这个地方设置什么都没有用了, 所以让它默认,它是一个枚举,直接可以写0.//target: action: 点击时调用哪个对象的哪个方法.UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:nil action:nil];//显示多张图片,不同状态,用按钮UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];//设置按钮正常状态下显示的图片[btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];//设置按钮高亮状态下显示的图片[btn setImage[UIImageimageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];//按钮自适应,根据当中的图片标题自动计算尺寸[btn sizeToFit];//让导航条左侧或者右侧显示一个UIView.//initWithCustomView:要显示的View.UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];//可以设置左右的内容为多个Item.self.navigationItem.rightBarButtonItems = @[item,item1,item2];

控制器的数据传递

控制器之间的数据传递主要有2种情况:顺传和逆传

顺传
控制器的跳转方向: A –> C
数据的传递方向 : A –> C
数据的传递方式 : 在A的prepareForSegue:sender:方法中根据segue参数取得destinationViewController, 也就是控制器C, 直接给控制器C传递数据
(要在C的viewDidLoad方法中取得数据,来赋值给界面上的UI控件)

逆传
控制器的跳转方向: A –> C
数据的传递方向 : C –> A
数据的传递方式 : 让A成为C的代理, 在C中调用A的代理方法,通过代理方法的参数传递数据给A