UINavigationController實現導航
在App委託中的.h檔案裡
@property( nonatomic ,strong)UINavigationController *nav;
.m檔案裡
@synthesize nav;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.nav = [ [UINavigationController alloc]initWithRootViewController:self.rootViewController];
[self.window addSubview: self.nav.view ];
return YES;
}
在viewController頁面裡面
-(void)viewDidLoad{
self.title = @"hello World";
}
更深一點的:
想在第一個視圖控制器出現在螢幕上5秒後把第二個視圖控制器拖到它的頂部:
首先在第一個視圖控制器放入第二個視圖控制器:
#import "SecondViewController"
-(void)pushSecondController{
SecondViewController *secondController = [ [ SecondViewController alloc]initWithNibName:nil bundle:NULL];
[ self.navigationController pushViewController:secondController
animated:YES];
}
-(void)viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selecter(pushSecondController) withObject:nil afterDelay:5.0f];
}
既然能夠拖進來,那麼就能移出去:
-(void)goBack{
[ self.navigationController popViewControllerAnimated:YES];
}
-(void)DidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(goBack) withObject:nil afterDelay:5.0f];
}
調整視圖控制器中導航控制器的序列
使用
UINavigationController 類的viewControllers屬性獲得並修改與導航控制器關聯的視圖控制器的排列順序:
-(void)goBack{
NSArray *currentControllers = self.navigationController.viewControllers;
NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
[newControllers removeLastObject];
self.navigationController.viewControllers = newControllers;
//動畫完成
//[self.navigationController setViewControllers:newControllers animated:YES];
}
為了從當前視圖控制器相關聯的導航控制器的階層中推送最後一個視圖控制器,可以在任何視圖控制器內調用此方法。
在導覽列展示一張圖片
要在導航控制器的當前視圖中的標題中用一張圖片代替文本
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [ [UIImageView alloc] initWithFrame:CGRectMake(0.0f,0.0f,100.0f,40.0f);
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed:@"FullSizeLogo.png"];
[imageView setImage:image];
self.navigationItem.titleView = imageView;
}
以上部分demo下載
使用UIBarButtonItem類在導覽列裡面添加按鈕
建立一個導覽按鈕前提:建立一個UIBarButtonItem類執行個體,使用視圖空間的NavigationItem屬性給導覽列添加按鈕。NavigationItem屬性允許我們設定這個導覽列。這個屬性自身有兩個屬性,分別為rightBarButtonItem 和 leftBarButtonItem 。這兩個屬性都屬於UIBarButtonItem類。
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"hello World";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"right" style:UIBarButtonItemStylePlain target:self action:@selector(performRight:)];
}
-(void) performRight:(id)right{
NSLog(@"clicked rightButton");
}
系統按鈕初始化方法一:
initWithBarButtonSystemItem:target:action:初始化方法
self.navigationItem.leftBarButtonItem = [ [UIBarButtonItem alloc] initWithBartButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(performRight:)];
導覽按鈕的初始化按鈕:
typedef enum{
UIBarButtonSystemItem Done/Cancel/Edit/Save/Add/FlexibleSpace/FixedSpace/Compose/Reply/Action/Organize/Bookmarks/Search/Refresh/Stop/Camera/Trash/Play/Pause/Rewind/FastForward/Undo/Redo/PageCurl/
}UIBarButtonSystemItem;
系統按鈕初始化方法二:
initWithCustomView:方法(可以將UISwitch添加)
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"hello World";
UISwitch *mySwitch = [ [UISwitch alloc]init];
mySwitch.on = YES;
[mySwitch addTarget:self action:@selector(SwitchChanged:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:mySwitch];
}
可以了,測試一下吧。
那麼再做一個上下箭頭的demo吧。
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"hello World";
NSArray *items = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"UpArrow.png"],[UIImage imageNamed:@"DownArrow.png"],nil];
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc]initWithItems:items];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
[segmentedControl addTarget:self action:@selector(segmentedControlTapped:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = [ [UIBarButtonItem alloc]initWithCustomView:segmentedControl];
//設定動畫
//UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.navigationItem setRightBarButtonItem:rightBarButton animated:YES];
}
使用UITabBarController顯示多視圖控制器
AppDelegate.h裡面
@class FirstViewController;
@class SecondViewController;
@property( nonatomic,strong)FirstViewController *firstViewController;
@property(non atomic,strong)SecondViewController *secondViewController;
@property(non atomic,strong)UITableBarController *tabBarController;
AppDelegate.m檔案裡
@synthesize firstViewController,secondViewController,tabBarController;
self.firstViewController = [[FirstViewController alloc]initWithNibName:nil bundle:NULL];
self.secondViewController = [[SecondViewController alloc]initWithNibName:nil bundle:NULL];
NSArray *twoViewControllers = [ [NSArray alloc]initWithObjects:self.firstViewController,self.secondViewController,nil];
self.tabBarController = [[UITabBarController alloc]init];
[self.tabBarController setViewControllers:twoViewControllers];
運行程式一看,沒有導航啊。怎麼辦呢?接著往下進行吧。
在AppDelegate.h中
@proterty(non atomic,strong)UINavigationController *nav;
在AppDelegate.m檔案裡
@synthesize nav;
self.firstNavigationController = [[UINavigationController alloc]initWithRootViewController:self.firstViewController];
self.secondNavigationController = [[UINavigationController alloc]initWithRootViewController:self.secondViewController];
NSArray *twoNavigationController = [[NSArray alloc]initWithObjects:self.firstNavigationController,self.secondNavigationController,nil];
self.tabBarController = [[UITabBarController alloc]init];
[self.tabBarController setViewControllers:twoNavigationController];
self.window addSubview:self.tabBarController.view];
tabbarItem屬性:
firstViewController.m
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self!=nil){
self.title = @"First";
self.tabBarItem.image = [UIImage image named:@"FirstTb.png"];
}
return self;
}
secondViewController 同上
下面部分demo下載