UIKit之UINavigationControl
2017-07-20 本文已影响0人
MI移动
UINavigationControl
// UINavigationController 以栈的形式 管理 各个Controller 显示的是位于栈顶的Controller
// 推出也是后入栈的先推出
// 1. 在AppDelegate.m中,初始化rootViewController
RootViewController *rootVC = [[RootViewController alloc]init];
// 初始化navigationController
UINavigationController *rootNC = [[UINavigationController alloc]initWithRootViewController:rootVC];
// self.window 的主 controller 设置成 rootNC
self.window.rootViewController = rootNC;
// 2. 界面交换方法
// 进入下一个controller
FirstViewController *firstVC = [[FirstViewController alloc]init];
[self.navigationController pushViewController:firstVC animated:YES];
// 推出当前 controller 回到上一个 controller
//[self.navigationController popViewControllerAnimated:YES];
//2. 回到根视图
//[self.navigationController popToRootViewControllerAnimated:YES];
//3. 可以拿到 navigationController 下面管理的UIViewControllers
NSArray *temp = self.navigationController.viewControllers;
[self.navigationController popToViewController:temp[1] animated:YES];
UINavigationBar
// navigationItem
// 1.设置标题
self.navigationItem.title = @"rootView";
// 标题还可以设置一个view,view里面可以设置imageView,button之类的
//self.navigationItem.titleView =
// 2.设置左button
//可以用图片初始化
//self.navigationItem.leftBarButtonItem = [UIBarButtonItem alloc]initWithImage:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>
// 可以用文字初始化
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"呵呵" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
// 3.设置右button
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"firstView" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
//4.设置barbutton 的背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
// 设置背景图
// self.navigationController.navigationBar setBackgroundImage:<#(UIImage *)#> forBarMetrics:<#(UIBarMetrics)#>
// 5.设置做按钮和右按钮的字体颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
// 6.可以把自带的backButton(返回按钮)隐藏
self.navigationItem.hidesBackButton = YES;
界面间传值
正向传值(属性传值)
@interface FirstViewController : UIViewController
// 1. 正向传值可以通过属性,,但是属性必须写在本类的.h文件中 不能写在延展里面
@property(nonatomic,retain)NSString *passStr;
@end
#import "FirstViewController.h"
@interface FirstViewController ()
@property(nonatomic,retain)UILabel *showLabel;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.showLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 200, 50)];
self.showLabel.backgroundColor = [UIColor grayColor];
// 2. 接受上一个页面传递过来的String
self.showLabel.text = self.passStr;
[self.view addSubview:self.showLabel];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"rootView" style:UIBarButtonItemStyleDone target:self action:@selector(firstViewAction:)];
self.navigationItem.title = @"firstView";
}
-(void)firstViewAction:(UIBarButtonItem *)sender{
[self.navigationController popViewControllerAnimated:YES];
}
#import "RootViewController.h"
#import "FirstViewController.h”
@interface RootViewController ()<UITextFieldDelegate>
@property(nonatomic,retain)UITextField *textField;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
self.textField.backgroundColor = [UIColor grayColor];
[self.view addSubview: self.textField];
self.textField.delegate = self;
self.navigationItem.title = @"rootView";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"firstView" style:UIBarButtonItemStyleDone target:self action:@selector(rootViewAction:)];
}
- (void)rootViewAction:(UIBarButtonItem *)sender{
FirstViewController *firstView = [[FirstViewController alloc]init];
[self.navigationController pushViewController:firstView animated:YES];
// 3. 将textField中 的文本,赋值给下一个controller的属性
firstView.passStr = self.textField.text;
}
2.反向传值(代理传值)
// 1. 写协议
@protocol PassValueDelegate <NSObject>
- (void)passValue:(NSString *)aString;
@end
// 2. 在.h中 声明一个代理的属性
@interface FirstViewController : UIViewController
@property(nonatomic,assign)id<PassValueDelegate>delegate;
@end
// 3. 在.m中 让代理先执行协议里的方法
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"rootView" style:UIBarButtonItemStyleDone target:self action:@selector(firstViewAction:)];
- (void)firstViewAction:(UIBarButtonItem *)sender{
[self.delegate passValue:self.textField.text];
[self.navigationController popViewControllerAnimated:YES];
}
// 4. 找一个Controller遵循协议
@interface RootViewController ()<PassValueDelegate>
// 5. 设置当前Controller为代理
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"firstView" style:UIBarButtonItemStyleDone target:self action:@selector(RootViewAction:)];
- (void)RootViewAction:(UIBarButtonItem *)sender{
FirstViewController *firstVIew = [[FirstViewController alloc]init];
// 设置代理
firstVIew.delegate = self;
[self.navigationController pushViewController:firstVIew animated:YES];
}
// 6. 让Controller执行协议里的方法
- (void)passValue:(NSString *)aString{
self.label.text = aString;
}