1.UINavigationController
1⃣️:初始
UINavigationController *rootNC = [[UINavigationController alloc]initWithRootViewController:rootVC];
2⃣️:self.window设置主controller
self.window.rootViewController = rootNC;
3⃣️:RETURN
退出当前controller 回到上个controller
[self.navigationController popViewControllerAnimated:YES];
回到根视图
[self.navigationController popToRootViewControllerAnimated:YES];
利用下标
NSArray *temp = self.navigationController.viewControllers;// 拿到navigationController下面管理的UIControllers
[self.navigationController popToViewController:temp[1] animated:YES];
4⃣️:NEXT
SecondViewController *secondRV = [[SecondViewController alloc]init];
UINavigationController 是以栈的形式管理各个controller的 显示的是位于栈顶的controller
push退出是后入栈的先出
[self.navigationController pushViewController:secondRV animated:YES];
2.UINavigationBar
代码:
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"黄页";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"上一个" style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonAction:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBarButtonAction:(UIBarButtonItem *)sender{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
}
3.界面间传值
1⃣️:正向传值(全局属性)
①:
@property (nonatomic,retain)NSString *passStr;
self.label.text = _passStr;
②:
FirstViewController *first = [[FirstViewController alloc]init];
// 将textField中文本赋值给下一个 controller 的属性
first.passStr = self.textfield.text;
[self.navigationController pushViewController:first animated:YES];
2⃣️:反向传值(全局代理)
①:
// 1.先写协议
// 2.在.h里面声明一个代理属性
// 3.在 .m让代理限执行协议里的方法
// 4.找一个controller遵循协议
// 5.设置当前controller为代理
// 6.让controller执行协议里的方法
②:
@protocol PassValueDelegate <NSObject>
// 写一个协议 找个代理进行传值
- (void)passVale:(NSString *)astring;
@end
@interface FirstViewController : UIViewController
@property (nonatomic , assign)id<PassValueDelegate>delegate;
@end
- (void)leftAction:(UIBarButtonItem *)sender{
[self.delegate passVale:_textField.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@interface RootViewController ()<PassValueDelegate>
- (void)rightAction:(UIBarButtonItem *)sender{
FirstViewController *first = [[FirstViewController alloc]init];
first.delegate = self;
[self.navigationController pushViewController:first animated:YES];
}
- (void)passVale:(NSString *)astring{
self.label.text = astring;
}