2011年11月30日 星期三

iOS - Training (9) - UINavigationController



方便切換UIViewController。為一個stack型態的容器,使用者視角從stack的入口看下去,所以後來放入的view會蓋過舊的view而被使用者看到,相對的回上一個view時,上層的view會被取消掉而不存在:
使用範例:
新增一個繼承UIViewController的類別Controller_A,連同Xib一起產生;同樣的也新增一個Controller_B。在A的Xib中拉入button,定義button的action
#import "Controller_B.h"

- (IBAction)callController:(id)sender {

    Controller_B* conB = [[Controller_B alloc] initWithNibName:@"Controller_B" bundle:nil];
    
    [self.navigationController pushViewController:conB animated:YES]; // UIViewController中有navigationController這個property,用來將Controller放入UINavigationController的stack中

//    [conB release]; // If no ARC
}
先在Delega.h中加入UINavigationController
@interface AppDelegate : UIResponder <UIApplicationDelegate>

{
    UINavigationController* navController;
}
再定義Delega.m
#import "Controller_A.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
    // Override point for customization after application launch.
    Controller_A* conA = [[Controller_A alloc] initWithNibName:@"Controller_A" bundle:nil];
    navController = [[UINavigationController alloc] initWithRootViewController:conA]; //初始化時將conA物件放入navController

    self.window.rootViewController = navController; //將navController放入主畫面中
    [self.window makeKeyAndVisible];
    
    return YES;
}

自訂UINavigationBar樣式 -----
在採用UINavigateController時,畫面上方會出現條bar,可以自行定義bar裡的內容,分別有leftBarButtonItem、titleView、rightBarButtonItem

titleView:
在UIViewController之中提供navigationItem.title property來設定title
- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Controller A";
}

leftBarButtonItem或rightBarButtonItem:
使用UIBarButtonItem,並將navigationItem.rightBarButtonItem指向UIBarButtonItem
- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Controller A";
    
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStylePlain target:self action:@selector(about:)]; //style參數決定按鈕的樣式,target與action設定呼叫的方法
    
    self.navigationItem.rightBarButtonItem = rightBtn; //將rightBtn指向navigationItem.rightBarButtonItem property
}

-(void) about:(id) sender
{
    NSLog(@"Hello I am Controller A");
}

改變back button:
同樣使用UIBarButtonItem來改變原有的back button
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Controller A";
    
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStylePlain target:self action:@selector(about:)]; //style參數決定按鈕的樣式,target與action設定呼叫的方法
    
    self.navigationItem.rightBarButtonItem = rightBtn; //將rightBtn指向navigationItem.rightBarButtonItem property

    UIBarButtonItem* backBtn = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; //style和target為UINavigationController控制,故就算給予定義也不會有反應

    
    self.navigationItem.backBarButtonItem = backBtn;
}

-(void) about:(id) sender
{
    NSLog(@"Hello I am Controller A");
}
-----

資料出處《Object-C與iOS開發入門》

沒有留言:

張貼留言