學習IOS開發的第8天(1)

今天學習了ios的導航控制器UINavigationController。導航控制器可以管理很多個視圖控制器,是一個棧結構,將一個個視圖控制器壓入棧中。在棧頂的視圖會顯示出來,將它彈棧就會顯示下面一個視圖。

UINavigationController包含一個導航欄UINavigationBar,導航欄可以顯示一個左按鈕,可以顯示一個右按鈕。導航欄上的按鈕都是由navigationItem管理的,而navigationItem是屬於不同的視圖控制器的。

我們創建一個根視圖控制器RootController,然後創建完視圖後,我們創建一個按鈕,點下按鈕後,會創建另一個視圖控制器,加入導航控制器,並顯示出來。

    //創建下一頁按鈕
    UIButton *next = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    next.frame = CGRectMake(100, 100, 100, 50);
    next.backgroundColor = [UIColor whiteColor];
    [next setTitle:@"下一頁" forState:UIControlStateNormal];
    [view addSubview:next];
    [next addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
按下按鈕會執行nextPage方法。在方法中創建第二個視圖控制器,並加入導航控制器中。
//點擊按鈕,下一頁
-(void)nextPage{
    //創建第二個視圖控制器
    SecondController *secondController = [[SecondController alloc] init];
    //加入到導航控制器中
    [self.navigationController pushViewController:secondController animated:YES];
}
我們要創建一個在導航欄上的“提示”按鈕。
    //提示 按鈕
    UIBarButtonItem *hint = [[UIBarButtonItem alloc] initWithTitle:@"提示" style:UIBarButtonItemStyleBordered target:self action:@selector(hint)];
    self.navigationItem.rightBarButtonItem = hint;
點擊後,會執行hint方法,彈出一個提示框。
//提示
-(void)hint{
    //創建提示框
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"李陽製作" delegate:nil cancelButtonTitle:@"好!很好!非常好!耶!" otherButtonTitles:nil];
    [self.view addSubview:alert];
    [alert show];
}
設置好視圖的標題。
    self.navigationItem.title = @"首頁";
在第二個視圖控制器SecondController中,先創建好視圖,然後創建一個“下一頁”按鈕。
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    view.backgroundColor = [UIColor orangeColor];
    self.view = view;
    
    UIButton *next = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    next.frame = CGRectMake(100, 100, 100, 50);
    next.backgroundColor = [UIColor whiteColor];
    [next setTitle:@"下一頁" forState:UIControlStateNormal];
    [view addSubview:next];
    [next addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
點擊按鈕會執行nextPage方法,再創建一個SecondController視圖控制器。
//點擊按鈕,下一頁
-(void)nextPage{
    //創建第二個視圖控制器
    SecondController *secondController = [[SecondController alloc] init];
    //加入到導航控制器中
    [self.navigationController pushViewController:secondController animated:YES];
}

給視圖的導航欄創建標題。

    self.navigationItem.title = [NSString stringWithFormat:@"第%d頁",count++];

我們在導航欄上創建一個返回首頁的按鈕。
    //回到首頁 按鈕
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"首頁" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem = back;
在back方法中,我們讓導航控制器彈出根視圖上的所有視圖,只剩根視圖。
//返回首頁按鈕
-(void)back{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
最後我們在應用程序的委託類中,創建導航控制器,並把根視圖控制器加入其中,然後讓window顯示它。
- (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];
    
    //創建根視圖
    RootController *rootController = [[RootController alloc] init];
    //創建導航控制器
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
    self.window.rootViewController = navigationController;
    
    return YES;
}


運行結果截圖:

        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章