ios的Navigation Controller的學習使用

導航控制器使用

創建

用xcode帶的功能進行拖拽

  1. 拖拽Navigation Controller
  2. 或者選中原有的,點擊上面的Editor-> embed in-> Navigation Controller

使用代碼的方式,在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
   
        // 1. 創建一個現實的UIWindow,大小與屏幕一樣
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        
        // 2. 創建一個顯示的控制器,使用三種方式
        UIViewController* vc;
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController * controll = [[UINavigationController alloc]initWithRootViewController:vc];
        // 3. 將控制器與window關聯
        self.window.rootViewController = controll;
        
        // 4. 將window顯示出來
        [self.window makeKeyAndVisible];
    
    return YES;
}

返回

  1. 彈出一個棧
[self.navigationController popViewControllerAnimated:YES];

  1. 彈出根棧
[self.navigationController popToRootViewControllerAnimated:YES];
  1. 彈出到指定棧
UIViewController* targetVc = self.navigationController.childViewControllers[1];
[self.navigationController popToViewController: targetVc animated:YES];

一些導航控制器的屬性

  1. xcode設置屬性
    在View Controller下面的View下面有個Navigation Item設置title
  2. 在代碼中設置
    // 設置導航標題
    self.navigationItem.title = @"hahaha";
    // 設置導航圖片
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"9_daye"]];
  1. 導航欄左右放置的是Bar Button Item
  2. 如果代碼中設置了leftBarButtonItem則返回點擊事件會失效
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章