#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" #import "FirstViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (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]; //初始化控制器 UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]]; self.window.rootViewController = navi; [self.window makeKeyAndVisible]; return YES; } @end
#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @end
#import "FirstViewController.h" #import "SecondViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.title = @"第一个控制器"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(70, 100, 180, 45); button.backgroundColor = [UIColor whiteColor]; [button setTitle:@"跳到第二个控制器" forState:0]; [button setTitleColor:[UIColor greenColor] forState:0]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonAction:(UIButton *)sender{ [self.navigationController pushViewController:[[SecondViewController alloc] init] animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
#import "SecondViewController.h" #import "ThirdViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor]; self.title = @"第二个控制器"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(70, 100, 180, 45); button.backgroundColor = [UIColor whiteColor]; [button setTitle:@"跳到第三个控制器" forState:0]; [button setTitleColor:[UIColor greenColor] forState:0]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonAction:(UIButton *)sender{ [self.navigationController pushViewController:[[ThirdViewController alloc] init] animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface ThirdViewController : UIViewController @end
#import "ThirdViewController.h" #import "FourthViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"第三个控制器"; self.view.backgroundColor = [UIColor yellowColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(70, 100, 180, 45); button.backgroundColor = [UIColor whiteColor]; [button setTitle:@"跳到第四个控制器" forState:0]; [button setTitleColor:[UIColor greenColor] forState:0]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonAction:(UIButton *)sender{ [self.navigationController pushViewController:[[FourthViewController alloc] init] animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface FourthViewController : UIViewController @end
#import "FourthViewController.h" #import "FifthViewController.h" @interface FourthViewController () @end @implementation FourthViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"第四个控制器"; self.view.backgroundColor = [UIColor greenColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(70, 100, 180, 45); button.backgroundColor = [UIColor whiteColor]; [button setTitle:@"跳到第五个控制器" forState:0]; [button setTitleColor:[UIColor greenColor] forState:0]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonAction:(UIButton *)sender{ [self.navigationController pushViewController:[[FifthViewController alloc] init] animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> @interface FifthViewController : UIViewController @end
#import "FifthViewController.h" #import "ThirdViewController.h" @interface FifthViewController () @end @implementation FifthViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"第五个控制器"; self.view.backgroundColor = [UIColor blueColor]; [self setupViews]; } /** * 初始化视图 */ - (void)setupViews{ [self initializeButtonWithFrame:CGRectMake(70, 100, 180, 45) tag:1000 title:@"返回第一个控制器"]; [self initializeButtonWithFrame:CGRectMake(70, 160, 180, 45) tag:1001 title:@"返回第二个控制器"]; [self initializeButtonWithFrame:CGRectMake(70, 220, 180, 45) tag:1002 title:@"返回第三个控制器"]; [self initializeButtonWithFrame:CGRectMake(70, 280, 180, 45) tag:1003 title:@"返回第四个控制器"]; } /** * 初始化button * * @param frame 尺寸 * @param tag 标签 * @param title button的标题 */ - (void)initializeButtonWithFrame:(CGRect)frame tag:(NSInteger)tag title:(NSString *)title { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame =frame; button.tag = tag; button.backgroundColor = [UIColor orangeColor]; [button setTitle:title forState:0]; [button setTitleColor:[UIColor whiteColor] forState:0]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } /** * button触发的事件 */ - (void)buttonAction:(UIButton *)sender{ // 在self.navigationController.viewControllers数组(的栈)中找到相应的控制器 UIViewController *viewController = self.navigationController.viewControllers[sender.tag - 1000]; [self.navigationController popToViewController:viewController animated:YES]; //跳到某一控制器 //遍历导航控制器(栈)中的控制器 // for (UIViewController *controller in self.navigationController.viewControllers) { // // 找到相应的控制器 // if ([controller isKindOfClass:[ThirdViewController class]]) { // //跳转到该控制器 // [self.navigationController popToViewController:controller animated:YES]; // } // } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end