IOS開發(7)之UINavigationController導航

來源:互聯網
上載者:User

1 前言
UINavigationController會讓App從一個試圖控制器切換到另一個,在開發中十分常見,今天我們來學習一下這個控制項。

2 UINavigation簡介
delegate代碼

.h檔案:


[plain]
#import <UIKit/UIKit.h> 
#import "ZYRootViewController.h" 
 
@interface ZYAppDelegate : UIResponder <UIApplicationDelegate> 
 
@property (strong, nonatomic) UIWindow *window; 
@property(nonatomic,strong) ZYRootViewController *zYRootViewController; 
@property(nonatomic,strong) UINavigationController *navigationController; 
 
@end 

#import <UIKit/UIKit.h>
#import "ZYRootViewController.h"

@interface ZYAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,strong) ZYRootViewController *zYRootViewController;
@property(nonatomic,strong) UINavigationController *navigationController;

@end.m檔案:

 

[plain]
@synthesize navigationController; 
@synthesize zYRootViewController; 
 
- (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]; 
    self.zYRootViewController = [[ZYRootViewController alloc] initWithNibName:nil bundle:nil]; 
    //執行個體化導覽列 
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.zYRootViewController]; 
    //向當前視圖添加導覽列 
    self.window.rootViewController = self.navigationController; 
//    [self.window addSubview:self.navigationController.view]; 
    return YES; 

@synthesize navigationController;
@synthesize zYRootViewController;

- (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];
    self.zYRootViewController = [[ZYRootViewController alloc] initWithNibName:nil bundle:nil];
    //執行個體化導覽列
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.zYRootViewController];
    //向當前視圖添加導覽列
    self.window.rootViewController = self.navigationController;
//    [self.window addSubview:self.navigationController.view];
    return YES;
}


ZYRootViewController代碼


.m檔案:


[plain]
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.title = @"First";//設定標題 
    [self performSelector:@selector(pushSecondController) withObject:nil afterDelay:3.0f];//3秒後調用自身pushSecondController方法 

 
-(void)pushSecondController{ 
    ZYSecondViewController *zYSecondViewController = [[ZYSecondViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController pushViewController:zYSecondViewController animated:YES];//嚮導航欄堆棧推入zYSecondViewController視圖 

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
    self.title = @"First";//設定標題
    [self performSelector:@selector(pushSecondController) withObject:nil afterDelay:3.0f];//3秒後調用自身pushSecondController方法
}

-(void)pushSecondController{
    ZYSecondViewController *zYSecondViewController = [[ZYSecondViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:zYSecondViewController animated:YES];//嚮導航欄堆棧推入zYSecondViewController視圖
}ZYSecondViewController代碼


.m檔案:


[plain]
- (void)viewDidLoad 

    [super viewDidLoad]; 
    self.title = @"Second"; 
    // Do any additional setup after loading the view. 
    [self performSelector:@selector(goBack) withObject:nil afterDelay:3.0f];//3秒後調用自身goBack方法 

 
-(void)goBack{ 
    [self.navigationController popViewControllerAnimated:YES];//將自身視圖彈出導覽列堆棧 

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Second";
 // Do any additional setup after loading the view.
    [self performSelector:@selector(goBack) withObject:nil afterDelay:3.0f];//3秒後調用自身goBack方法
}

-(void)goBack{
    [self.navigationController popViewControllerAnimated:YES];//將自身視圖彈出導覽列堆棧
}運行結果:

最初的效果

 



3秒以後跳到第二個視圖:

 
 


又3秒以後彈出本視圖,顯示初始視圖:

 

 
 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.