ios學習:UINavigationController導航

來源:互聯網
上載者:User

 

檔案目錄如下:基本導航順序: root -> First -> Second -> Third。其中,FirstViewController作為 navigation堆棧的rootview

 

1、建立navigation。

如果是想直接把navigation導航作為項目一開始的跟視圖,把RootViewController.h檔案裡的nav屬性放到AppDelegate.h裡即可,再把RootViewController.m檔案裡的action的代碼複製到 AppDelegate.m裡的didFinishLaunchingWithOptions 方法裡,最後把 self.window.rootViewController 設定 UINavigationController類型的屬性nav即可

在RootViewController.h檔案

 1 #import <UIKit/UIKit.h> 2 @class FirstViewController; 3  4 @interface RootViewController : UIViewController 5  6 @property (strong, nonatomic) UINavigationController *nav; 7  8 - (IBAction)btnClick:(UIButton *)sender; 9 10 @end

 

在RootViewController.m 檔案裡的隨意一個自訂action裡:

 1 - (IBAction)btnClick:(UIButton *)sender { 2      3     //建立一個viewcontroller 4     FirstViewController *fristview =[[[FirstViewController alloc] init] autorelease]; 5     6      7     //初始化UINavigationController(方式一) 8     self.nav = [[[UINavigationController alloc] initWithRootViewController:fristview] autorelease]; 9     10     11     //初始化UINavigationController(方式二)12   //  self.nav = [[[UINavigationController alloc] init] autorelease];13   //  [self.nav pushViewController:fristview animated:YES];14 15     16     //初始化UINavigationController(方式三,失敗,xib檔案載入失敗,原因暫時不明)17    // self.nav = [[[UINavigationController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];18     19     20     //跳轉到FirstView頁面21     [self presentViewController:self.nav animated:YES completion:nil];22     23     24     //這種寫法一般用於往view裡添加一些小控制項,如button  label textField之類的,不適宜用於頁面跳轉25     // [self.view addSubview:self.nav.view];26     27     28 }

2、設定viewcontroller的navigationItem屬性和 toolbar、toolbarItem

(1)firstViewcontroller的navigation屬性和toolbarItem屬性。代碼在firstviewcontroller.m的viewdidLoad方法裡

每個viewcontroller都有一個navigationItem屬性(只在被載入到導航堆棧裡才有效),navigationItem屬性裡又有5個屬性:UIBarButtonItem類型的leftBarButtonItem、rightBarButtonItem和backBarButtonItem,NSString類型的title(或者viewcontroller屬性的titleView)、prompt。

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4     // Do any additional setup after loading the view from its nib. 5      6     //下面兩句代碼的效果一樣,但貌似設定了navigationItem.title 後 title就會被不起作用了 7     self.navigationItem.title =  @"首頁123"; 8     self.title = @"首頁"; 9     10     //在title上面再加多一行,但這樣子就會造成導覽列的寬度變寬11   //  self.navigationItem.prompt = @"prompt";12     13     //設定rightbarbuttonitem14     UIBarButtonItem *rightbar = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(btnClick:)] autorelease];15 16     self.navigationItem.rightBarButtonItem = rightbar;17 18     //修改backbarbuttonitem的title.這個backbarbutton是顯示在下一個push進來的view的tabbar的左邊的19     //action可以設定為nil,這是的動作就是預設的動作,也就是返回到這個view20     self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:nil] autorelease];21     22     23     24     //設定toolbar是否可見(對整個navigation堆棧裡的view起作用,起全域作用)25     self.navigationController.toolbarHidden = NO;26     27     UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];28     UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];29     UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];30     31     //toolbar是整個navigation堆棧裡的view共同的,但toolbar上面的items卻是每個view單獨擁有的32     //現在只是設定了當前view的toolbaritem,與其他view的toolbaritme是沒有關係的33     [self setToolbarItems:[NSArray arrayWithObjects:flexItem, three, flexItem, four, flexItem, nil]];34     35     [three release];36     [four release];37     [flexItem release];38    39     40 }

頁面效果如下:

 

(2)、secondviewcontroller的navigationItem和toolbarItem

代碼在viewdidLoad方法裡

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4     // Do any additional setup after loading the view from its nib. 5  6     //設定左右barbutton 7     UIBarButtonItem *rightbar = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(gotoThird:)] autorelease]; 8      9     UIBarButtonItem *leftbar = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(gotoThird:)] autorelease];10     11     self.navigationItem.rightBarButtonItem = rightbar;12     self.navigationItem.leftBarButtonItem = leftbar;13     14     //當前view在navigation堆棧裡不是rootview,所以把該viewpush到navigation堆棧裡時,tabbar左邊會顯示backbarbutton15     //如果給當前view設定了leftbarbutton,會把把backbarbutton覆蓋掉,想同時顯示兩者,需如下設定:16     self.navigationItem.leftItemsSupplementBackButton = YES;17 18     19     20     //設定當前view的toolbar可見.因為有可能從一個toolbar不可見的view導航到當前view21     self.navigationController.toolbarHidden = NO;22     23 }

效果如下:

 

(3)、thirdviewcontroller的navigationItem和toolbaritem

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4     self.title = @"第三頁"; 5     // Do any additional setup after loading the view from its nib. 6      7     //隱藏當前view的toolbar 8     self.navigationController.toolbarHidden = YES; 9     10     //設定toolbar11     UIBarButtonItem *rightbar = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goToRootView)] autorelease];12     13     self.navigationItem.rightBarButtonItem = rightbar;14     15    // NSArray *array = @[@"排骨", @"牛排"];16     17     //設定titleview,會覆蓋title18     UISegmentedControl *segment = [[[UISegmentedControl alloc] initWithItems:@[@"排骨", @"牛扒"] ] autorelease];19     segment.segmentedControlStyle = UISegmentedControlSegmentCenter;20     21     self.navigationItem.titleView = segment;22 }

效果如下:

 

barButtonItem的style及效果對應如下:

相關文章

聯繫我們

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