iOS App開發(fā)中導(dǎo)航欄的創(chuàng)建及基本屬性設(shè)置教程
文件目錄如下:基本導(dǎo)航順序: root -> First -> Second -> Third。其中,F(xiàn)irstViewController作為 navigation堆棧的rootview
1、創(chuàng)建navigation
如果是想直接把navigation導(dǎo)航作為項目一開始的跟視圖,把RootViewController.h文件里的nav屬性放到AppDelegate.h里即可,再把RootViewController.m文件里的action的代碼復(fù)制到 AppDelegate.m里的didFinishLaunchingWithOptions 方法里,最后把 self.window.rootViewController 設(shè)置 UINavigationController類型的屬性nav即可
在RootViewController.h文件
#import <UIKit/UIKit.h>
@class FirstViewController;
@interface RootViewController : UIViewController
@property (strong, nonatomic) UINavigationController *nav;
- (IBAction)btnClick:(UIButton *)sender;
@end
在RootViewController.m 文件里的隨意一個自定義action里:
- (IBAction)btnClick:(UIButton *)sender {
//創(chuàng)建一個viewcontroller
FirstViewController *fristview =[[[FirstViewController alloc] init] autorelease];
//初始化UINavigationController(方式一)
self.nav = [[[UINavigationController alloc] initWithRootViewController:fristview] autorelease];
//初始化UINavigationController(方式二)
// self.nav = [[[UINavigationController alloc] init] autorelease];
// [self.nav pushViewController:fristview animated:YES];
//初始化UINavigationController(方式三,失敗,xib文件加載失敗,原因暫時不明)
// self.nav = [[[UINavigationController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
//跳轉(zhuǎn)到FirstView頁面
[self presentViewController:self.nav animated:YES completion:nil];
//這種寫法一般用于往view里添加一些小控件,如button label textField之類的,不適宜用于頁面跳轉(zhuǎn)
// [self.view addSubview:self.nav.view];
}
2.navigation的常用屬性設(shè)置例子
我們的navigation就加載上去了以后,下面我們來設(shè)置navigation的屬性:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController.navigationBar setTranslucent:NO];//設(shè)置navigationbar的半透明
self.title = @"navigationcontroller";//設(shè)置navigationbar上顯示的標(biāo)題
[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//設(shè)置navigationbar的顏色
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil];//設(shè)置navigationbar左邊按鈕
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];//設(shè)置navigationbar右邊按鈕
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//設(shè)置navigationbar上左右按鈕字體顏色
}
效果圖如下:
這里還有一個屬性常用,就是:
NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil nil];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
self.navigationItem.titleView = segment;//設(shè)置navigation上的titleview
效果如下:
對,我們看到中間的字變成了兩個可選的按鈕,這就是navigation的另一個屬性:navigationitem.titleview。
下面我們再建立一個視圖,看一下兩個視圖之前是怎樣通信的。
在第二個視圖中,我添加了一個button來顯示,并加了一個成員變量來接收從第一個視圖中穿過來的值:
@interface SecondViewController : UIViewController
@property (copy,nonatomic) NSString *str;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"second";
UIButton *aBUTTON = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 50, 30)];
[aBUTTON setTitle:_str forState:UIControlStateNormal];
[aBUTTON addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aBUTTON];
}
然后我將第一個視圖的右邊按鈕添加一個事件,點擊按鈕,就會推出第二個視圖,并顯示我們傳過來的值:
- (void)clicked{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
second.str = @"hello!!";
[second release];
}
下面,我們來運行一下:
點進按鈕以后,我們的第二個視圖推出,button顯示了傳過來的值。
然后我們點擊回button,還有navigation另外一個方法:
- (void)clicked{
[self.navigationController popViewControllerAnimated:YES];
}
這樣就可以回到第一個視圖。
相關(guān)文章
MAUI模仿iOS多任務(wù)切換卡片滑動的交互實現(xiàn)代碼
這篇文章主要介紹了[MAUI]模仿iOS多任務(wù)切換卡片滑動的交互實現(xiàn),使用.NET MAU實現(xiàn)跨平臺支持,本項目可運行于Android、iOS平臺,需要的朋友可以參考下2023-05-05

IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法