网上很多人写的都很烦,要么太理论,要么不好实现.写个demo帮助理解
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor whiteColor];
RootViewController *rootView=[[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootView];
[_window setRootViewController:nav];
return YES;
}
我的RootView.ViewController
//
// ViewController.m
// First
//
// Created by zcw on 2017/12/6.
// Copyright © 2017年 zcw. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
{
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
}
@end
@implementation ViewController
@synthesize mystr = _myStr;
- (void)viewDidLoad {
[super viewDidLoad];
//Nav
[self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
self.title = @"RootViewController";//设置navigationbar上显示的标题
[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil];//设置navigationbar左边按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];//设置navigationbar右边按钮
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色
// self.navigationItem.rightBarButtonItem setTarget:<#(id)#>
// 动态添加一个按钮
// Do any additional setup after loading the view, typically from a nib.
UIButton * button = [[UIButton alloc] init];
[button setTitle:@"点击我跳转" forState:UIControlStateNormal];
[button setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
UIImage *imgNormal = [UIImage imageNamed:@"btn_01"];
UIImage *imgHighted = [UIImage imageNamed:@"btn_02"];
[button setBackgroundImage:imgNormal forState:(UIControlStateNormal)];
[button setBackgroundImage:imgHighted forState:(UIControlStateHighlighted)];
button.frame = CGRectMake(50, 50, 299, 126);
[button addTarget:self action:@selector(buttonPrint) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void)buttonPrint
{
printf("测试打印");
SecondViewController *nextVc = [[SecondViewController alloc]init];
// [self presentViewController:nextVc animated:YES completion:nil];
[self.navigationController pushViewController:nextVc animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondVIewController
//
// SecondViewController.m
// First
//
// Created by zcw on 2017/12/6.
// Copyright © 2017年 zcw. All rights reserved.
//
#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UILabel *label = [[UILabel alloc] init];
label.text = @"second page";
[label setTextColor:[UIColor yellowColor]];
label.frame = CGRectMake(200, 200, 50, 200);
UIButton * button = [[UIButton alloc] init];
[button setTitle:@"-----点击我跳转" forState:UIControlStateNormal];
[button setTitle:@"-----摸我干啥" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
UIImage *imgNormal = [UIImage imageNamed:@"btn_01"];
UIImage *imgHighted = [UIImage imageNamed:@"btn_02"];
[button setBackgroundImage:imgNormal forState:(UIControlStateNormal)];
[button setBackgroundImage:imgHighted forState:(UIControlStateHighlighted)];
button.frame = CGRectMake(50, 50, 299, 126);
[button addTarget:self action:@selector(buttonPrint) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonPrint
{
printf("测试打印2222");
// [self dismissViewControllerAnimated:true completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];//回到根视图界面
}
@end