標籤:
#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
iOS 導航控制器返回棧中的某一控制器