上一篇:/kf/201301/185018.html
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.nav pushViewController:secondController animated:YES];
}
-(void)viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selecter(pushSecondController) withObject:nil afterDelay:5.0f];
}
既然能夠拖進來,那麼就能移出去:
-(void)goBack{
[ self.nav 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.nav.viewControllers;
NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
[newControllers removeLastObject];
self.nav.viewControllers = newControllers;
//動畫完成
//[self.nav 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;
}
使用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 同上