Iphone用UINavigationController實現在兩個頁之間導航(上)

來源:互聯網
上載者:User

要求:1、從基於Xcode空項目模板開始
      2、兩頁之間能相互傳遞資料
      3、在導航控制欄上添加系統按鈕和左右按鈕
實現方法分析
1、根據MVC模式,iphone的程式都是有 view-model-controller 組成的。所以,在兩個頁面就是兩個視圖控制器和其管理的View。因此:
第一頁定義為:cityViewController
第二頁定義為:CityDetailViewController
導航控制器在這兩個視圖控制器之間切換!工程項目名稱為:lvyou
2、開發步驟:
第一步:建立一個工程項目。項目名稱:lvyou、基於空模板(Windows-based Application)
第二步:建立cityViewController視圖控制器。
>>請選擇File > New File。在New File視窗中,請選擇Cocoa Touch Classes,然後選擇UIViewController-subclass。同時,請勾選Options地區中標題為With XIB for user interface的選擇框。為檔案起個名字,本例視圖控制器名稱為cityViewController,(類名稱習慣以一個大寫字母開頭)。請務必建立.m和.h檔案,並將二者都添加到工程。此時,系統已經產生三個檔案(*.h、*.m、*.xib), 實際實現了四項任務:
1)系統為你建立了一個視圖控制器(名叫File’sOwner對象),類名為cityViewController
2)系統也為你建立了一個視圖view(點*.xib上開啟)
3)在視圖控制器類上,系統也為你定義了一個輸出口IBOulet變數view
4)這個變數(輸出口IBOulet變數view)已經關聯了視圖view
第三步:同樣方法建立CityDetailViewController視圖控制器。
>>請選擇File > New File。在New File視窗中,請選擇Cocoa Touch Classes,然後選擇UIViewController-subclass。同時,請勾選Options地區中標題為With XIB for user interface的選擇框。為檔案起個名字,視圖控制器名稱為CityDetailViewController。此時,系統已經產生三個檔案(*.h、*.m、*.xib),同樣也實際實現了上述的四項任務。
第四步:添加(註冊)一個導航控制器。
(註冊cityViewController是第一個要顯示視圖控制器)只能用代碼方法!(當然,如果你建立工程項目時選擇“基於導航的應用”。就會為你自動建立一個名為RootViewController的根視圖控制器,並自動產生了一些代碼,比如把導航控制器的視圖放到視窗的視圖內)
方法:在lvyouAppDelegate.h上聲明(添加)一個UINavigationController類對象
[plain] 
//lvyouAppDelegate.h原代碼開始 
#import <UIKit/UIKit.h> 
@interface lvyouAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    UINavigationController *navController;//此行新加,對應的實現檔案.m上必須要有代碼! 
    UITabBarController *tabBarController; // 聲明一個標籤控制器,下一節講 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@end 
//lvyouAppDelegate.h原代碼結束 
  
//lvyouAppDelegate.m原代碼開始 
#import "lvyouAppDelegate.h" 
#import "cityViewController.h"//此處要加上 
#import "MeiShiTianDi.h" 
@implementation lvyouAppDelegate 
@synthesize window; 
#pragma mark - 
#pragma mark Application lifecycle 
  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    tabBarController = [[UITabBarController alloc] init];//初始化標籤欄控制器 
    MeiShiTianDi *viewController =[[MeiShiTianDi alloc] init];//初始化控制器 
    viewController.title=@"美食天地"; 
    navController = [[UINavigationController alloc] init];//初始化導航控制器 
    //加入標籤欄控制器的控制器數量矩陣 
    tabBarController.viewControllers = 
    [NSArray arrayWithObjects:navController, viewController,nil]; 
    
    [viewController release]; 
    //定義名稱為“所有城市”的返回按鈕。該按鈕無需target和action 
//因為系統已經實現了返回功能。Style是顯示風格 
    UIBarButtonItem *backButton = 
       [[UIBarButtonItem alloc] initWithTitle:@"所有城市" 
           style:UIBarButtonItemStyleBordered 
           target:nil action:nil]; 
    //設定啟動應用時第一個要顯示的視圖控制器,這裡是cityViewContrl 
    cityViewController *cityViewContrl = [[cityViewController alloc] init]; 
    cityViewContrl.title = @"旅遊指南";//設定第一個視圖控制器的標題 
    //設定返回按鈕 
    cityViewContrl.navigationItem.backBarButtonItem= backButton; 
    [backButton release]; 
    //把第一個視圖控制器推push到堆棧中 
    [navController pushViewController:cityViewContrl animated:NO]; 
    //這裡假定是導覽列的第一個視圖,所以animated:NO不要動畫化。 
    [cityViewContrl release]; 
    //把導航(標籤欄)控制器放到Window下 
    //[window addSubview:navController.view]; 
    [window addSubview:tabBarController.view]; 
    
    // Override point for customization after application launch. 
    [window makeKeyAndVisible]; 
    return YES; 

- (void)applicationWillResignActive:(UIApplication *)application { 
    

- (void)applicationDidEnterBackground:(UIApplication *)application { 
    

- (void)applicationWillEnterForeground:(UIApplication *)application { 
    

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    

- (void)applicationWillTerminate:(UIApplication *)application { 
    

#pragma mark - 
#pragma mark Memory management 
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { 
    

- (void)dealloc { 
    [tabBarController release];//釋放記憶體 
    [navController release]; 
    [window release]; 
    [super dealloc]; 

@end 
//lvyouAppDelegate.m原代碼結束 

第五步:設計完善cityViewController視圖控制器。
首先在cityViewController.xib上添加一個按鈕,修改按鈕標題為“北京”。在cityViewController.h中聲明一個方法selectCity。將按鈕與方法建立串連!
[plain] 
//cityViewController.h原代碼開始 
#import <UIKit/UIKit.h> 
@interface cityViewController : UIViewController { 
    } 
- (IBAction) selectCity : (id) sender;//自訂一個方法,點擊“北京”按鈕,進入下一頁 
@end 
//cityViewController.h原代碼結束 
  
//cityViewController.m原代碼開始 
#import "cityViewController.h" 
#import "CityDetailViewController.h"//新加上的,聲明註冊CityDetailViewController 
  
@implementation cityViewController  //定義屬性 
//實現自訂的方法(函數) 
-(IBAction) selectCity : (id) sender{ 
    CityDetailViewController *cityDetailContrl 
            = [[CityDetailViewController alloc] init];//初始化CityDetailViewController 
    cityDetailContrl.title = @ "北京歡迎您";//設定第二個視圖控制器標題 
    cityDetailContrl.city = @"北京";//設定要傳到下一個控制器的資料——北京 
    //把第二視圖控制器推入堆棧中 
[self.navigationController pushViewController:cityDetailContrl animated:YES]; 
    [cityDetailContrl release];//釋放記憶體 

  
  
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
//建立一個有邊框的文體按鈕,按下後,調用視圖控制器上的discount方法 
UIBarButtonItem *discountButton = [[UIBarButtonItem alloc] 
                    initWithTitle:@"折扣資訊" style:UIBarButtonItemStyleBordered 
                                target:self action:@selector(discount:)]; 
    self.navigationItem.leftBarButtonItem = discountButton;//設定為左邊按鈕 
    [discountButton release];//釋放記憶體 
    //設定視圖控制器在標籤欄上的標題和映像 
    // 文字是:旅遊指南。映像是:GoldenGateBridge.png 
    UITabBarItem *item = [[UITabBarItem alloc] 
                          initWithTitle:@"旅遊指南" 
                          //initWithTabBarSystemItem:UITabBarSystemItemBookmarks 
                          image:[UIImage imageNamed:@"GoldenGateBridge.png"] 
                          tag:0]; 
    self.tabBarItem = item; 
    [item release]; 
    
    [super viewDidLoad]; 

  
  
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
    
    // Release any cached data, images, etc that aren't in use. 

  
- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 

- (void)dealloc { 
    [super dealloc]; 

@end 
//cityViewController.m原代碼結束 

第六步:設計完善CityDetailViewController視圖控制器。
首先在CityDetailViewController.xib上添加兩個標籤Label,其中第一個標籤Label修改文字為“北京的介紹資訊”(實際應用中城市的介紹資訊應該從模型類中讀取),第二個標籤Label修改文字“城市名稱”,因為從前一個頁面傳遞一個資料(城市名稱)過來,在第二個頁面上要讀取,並顯示出來。在CityDetailViewController.h中聲明一個IBOutlet(命名為cityName),用於關聯View上剛剛建立的“城市名稱”Label,另外,建立一個屬性“city”用於接收從前一個頁面傳遞過來的資料(城市名稱)。

[plain] 
//CityDetailViewController.h原代碼開始 
#import <UIKit/UIKit.h> 
@interface CityDetailViewController : UIViewController { 
    IBOutlet UILabel *cityName;// 第二個標籤Label建立介面IBOutlet 
    NSString *city;            //建立一個屬性 

@property (copy) NSString *city; //建立一個屬性set/get方法 
@end 
//CityDetailViewController.h原代碼結束 
  
//CityDetailViewController.m原代碼開始 
#import "CityDetailViewController.h" 
@implementation CityDetailViewController //實現控制器類 
@synthesize city; //屬性city變數 
  
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    cityName.text = city;//擷取從前一個頁面傳來的資料 
    //建立一個系統添加按鈕,按下後,調用視圖控制器上的add方法 
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
       initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
       target:self action:@selector(add:)];// 調用視圖控制器上的add方法 
    //設定為導航控制器控制條上的右邊按鈕 
self.navigationItem.rightBarButtonItem = rightButton; 
    [rightButton release]; 
    
    [super viewDidLoad]; 

  
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 

- (void)dealloc { 
    [super dealloc]; 

@end 
//CityDetailViewController.m原代碼結束

相關文章

聯繫我們

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