iOS 導航控制器返回棧中的某一控制器

來源:互聯網
上載者:User

標籤:

                                      

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.