iphone開發之導航控制器的使用

來源:互聯網
上載者:User

我們在應用開發時經常需要實現很多功能,這時常常需要構建多視圖應用程式,在ios平台下我們可以使用UINavigationController(導航控制器)來實現。

建立一個基於View的項目,修改AppDelegate.h中的代碼如下:

#import <UIKit/UIKit.h>@class ViewController;@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) ViewController *viewController;@property (nonatomic, retain) UINavigationController *navController;@end

我們定義了一個導航控制器,下面我們為導航控制器添加跟視圖。在AppDelegate.m中修改一下代碼:

@synthesize navController;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.navController = [[UINavigationController alloc] init];        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];    [self.navController pushViewController:self.viewController animated:YES];    [self.window addSubview:self.navController.view];    [self.window makeKeyAndVisible];    return YES;}


這裡主要是初始化我們的導航控制器,然後為導航控制器添加子視圖並在視窗中顯示。

現在我們要在ViewController.h中定義一個按鈕事件,用來響應我們根視圖中的按鈕事件。(中start按鈕)

- (IBAction)startPressed:(id)sender;

然後再ViewController.m中實現該方法:

- (IBAction)startPressed:(id)sender{    FirstViewController *firstViewController = [[FirstViewController alloc] init];    [self.navigationController pushViewController:firstViewController animated:YES];    [firstViewController release];}


我們定義一個新的的視圖控制器firstViewController並添加到導航控制器中。FirstViewController實現了TableView

關於TableView控制項的使用可參考我這篇博文:iphone開發之TableView控制項執行個體

為了程式更直觀,我們在viewDidLoad()方法中添加如下代碼為我們的應用程式添加標題:

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    self.title = @"Cloay";}

在FirstViewController.m 中添加如下代碼:

#pragma mark - Table view delegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // Navigation logic may go here. Create and push another view controller.     SecondViewController *detailViewController = [[SecondViewController alloc] init];    NSUInteger row = [indexPath row];    detailViewController.title = [self.listData objectAtIndex:row];     [self.navigationController pushViewController:detailViewController animated:YES];     }

通過該方法我們實現了從FirstLevel進入SecondLevel如:如果我們選了Kobe

    

如果選了James:



我們看到圖中有一張圖片和一個Next按鈕,你一定想到了我們要在試圖中添加一個ImageView控制項並要為Next按鈕添加事件代碼如下:

- (IBAction)nextPressed:(id)sender{    ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];    [self.navigationController pushViewController:thirdViewController animated:YES];    [thirdViewController release];}

上面我們為按鈕添加響應,我們還要在viewDidLoad中為ImageView設定圖片:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.//    self.title = @"Second Level";    NSLog(@"%@", self.title);    if ([self.title isEqualToString:@"Kobe"]) {        UIImage *portrait = [UIImage imageNamed:@"Kobe-Bryant.jpg"];        self.image.image = portrait;        [portrait release];    }else if([self.title isEqualToString:@"James"]){        UIImage *portrait = [UIImage imageNamed:@"James.jpg"];        self.image.image = portrait;        [portrait release];    }else{        UIImage *portrait = [UIImage imageNamed:@"ladybug.jpg"];        self.image.image = portrait;        [portrait release];    }    }

我們根據title來設定圖片。點擊Next按鈕進入ThirdLevel

ThirdViewController中很簡單只有一個Label控制項,這裡就不在貼代碼了。

怎麼樣多視圖程式就這樣實現了,這隻是一個簡單的demo,你可以實現自己的功能。

好了就寫這麼多,有什麼問題請留言,大家一起學習交流!

說明:轉載請註明出處!

聯繫我們

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