代码列表
效果图:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *root = [[RootViewController alloc] init];
//创建导航器
UINavigationController * nac = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController=nac;
//导航器颜色
nac.navigationBar.backgroundColor = [UIColor yellowColor];
//半透明
nac.navigationBar.translucent = NO;
return YES;
}
RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()<chuanZhiDalegate>
{
UITextField *tf;
}
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
//把控制导航栏内容的代码写到对应的页面中来
self.navigationItem.title = @"首页";
// self.navigationItem.titleView =
//左右侧的按钮
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"右侧" style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)];
//图片右侧按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"settings" ] style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)];
//属性传值代理传值
// 第一个页面中的输出框
tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 150, 150, 40)];
tf.backgroundColor = [UIColor redColor];
[self.view addSubview:tf];
}
-(void) touchRight {
NSLog(@"点了右侧");
SecondViewController * secondVC= [[SecondViewController alloc] init];
//跳转到第二页面
[self.navigationController pushViewController:secondVC animated:YES];
//传值
secondVC.inputValue = tf.text;
secondVC.delegate = self;
}
//代理传回的值
- (void)chuanZhi:(NSString *)str {
NSLog(@"代理传回的值:%@",str);
}
SecondViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
//第二个页面左上角的返回按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(touchLeftBack)];
//第二个页面中的Label
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
label.text = self.inputValue;
label.enabled = YES;
[self.view addSubview:label];
// 代理传值
}
-(void) touchLeftBack {
NSLog(@"左侧返回按钮");
//返回时触发代理方法 将值传递回去
[self.delegate chuanZhi:@"这个是返回的值"];
// 返回上级视图
[self.navigationController popToRootViewControllerAnimated:YES];
// 返回到根视图
//[self.navigationController popToRootViewControllerAnimated:YES];
//指定到某个页面 少用到
// [self.navigationController popToViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>
}