建立一個多頁面,基於UINavigationController的控制的跳轉的iOS應用程式

來源:互聯網
上載者:User

從android轉到iOS上開發,剛開始很不習慣,總是喜歡用android的想法來用iOS上的控制項,結果是到處碰壁,並且是一直碰,現在閑著沒事就寫點剛開始的一些簡單程式,加深下對iOS開發的認識。

1.首先建立一個空的iOS工程(我預設使用了arc機制,所以後面的代碼中不會出現釋放的代碼);工程目錄如下

2.修改VSAppDelegate.h,添加一個UINavigationController類型的屬性naviController,然後在VSAppDelegate.c中添加上屬性的get/set方法的聲明,在這個例子中我們只修改用

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 方法,其它的方法你可以手動刪除掉,用的時候再添加就可以了,下面修改這個方法,將naviController添加到程式中,在這之前你需要先建立一個程式啟動時的畫面,在這個例子中是VSLoginViewController,原因是在初始化naviController的時候需要用一個ViewController來作為RootViewController,具體實現的代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    VSLoginViewController *loginView = [[VSLoginViewController alloc]initWithNibName:@"VSLoginViewController" bundle:nil];    self.naviController = [[UINavigationController alloc]initWithRootViewController:loginView];    self.window.rootViewController = self.naviController;    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

  啟動後的畫面如下:

也許你會很奇怪,為什麼你的程式會帶有一個導航條呢,而我的這個怎麼沒有的呢,原因很簡單,我是在VSLoginViewController的(void) viewWillAppear:(BOOL)animated方法中將導航條隱藏掉了,代碼如下:

-(void) viewWillAppear:(BOOL)animated{    [self.navigationController setNavigationBarHidden:YES animated:YES];    [super viewWillAppear:animated];}

在做跳轉邏輯之前,我們還要做一個設定,就是設定下一個頁面中導航條的回退按鈕的標題,是的,下一個頁面的回退按鈕的標題是在本版面設定的,這裡有2種方法:1.設定本畫面的title屬性;2.修改下一個頁面的導航條的回退按鈕,這個的設定是在ViewController的init方法中實現的,代碼如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];        backItem.title=@"登出";        self.navigationItem.backBarButtonItem = backItem;//        self.title = @"登出";    }    return self;}

3.下面來實現頁面的跳轉邏輯,首先在VSLoginViewController.h中聲明一個方法login,然後將這個方法綁定到xib的按鈕上,然後實現login方法,代碼如下:

-(void) login:(id)sender{    VSMainViewController *mainViewController = [[VSMainViewController alloc]initWithNibName:@"VSMainViewController" bundle:nil];    [self.navigationController pushViewController:mainViewController animated:YES];}

設定完成後點擊這個按鈕就會跳轉到下一個頁面了:

導航條有顯示回來了,這也是在viewWillAppear方法中設定的,如果不設定則預設是沿用上一個的導航條設定。

相關文章

聯繫我們

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