2013-10-19 165 views
0

請幫助瞭解導航。我正在與xibs合作。該計劃是:https://www.dropbox.com/s/o82fxpte0hmyxcq/Scheme_Simple.jpg。 這裏是我的代碼:在iOS應用程序中使用xib導航應用程序

@implementation AppDelegate 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.backgroundColor = [UIColor whiteColor]; 
    FirstViewController *firstViewController = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil]; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; 
    self.window.rootViewController = navigationController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

@implementation FirstViewController 
- (IBAction)button1Tapped:(id)sender { 
    SecondViewController *secondViewController = [[SecondViewController alloc] init]; 
    secondViewController.title = @"View2"; 
    [self.navigationController pushViewController:secondViewController animated:YES]; 
} 

@implementation SecondViewController 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] init]; 
    thirdViewController.title = @"View3"; 

    if (indexPath.row == 0) {  
     [self.navigationController pushViewController:thirdViewController animated:YES]; 
     [secondViewTableView deselectRowAtIndexPath:indexPath animated:YES]; 
    } 
} 

所以,我有疑問:

  1. 我應該在哪裏創建下一個看法?在我的代碼視圖中創建了「前一個」類:在FirstViewController中創建的view2,在SecondViewController中創建的view3等。所有新的ViewController都在啓動導航的方法中,是正確的方式嗎?我認爲這是錯誤的,但導航正在工作。

  2. 導航欄中標題的問題。事實證明,view2的標題僅在從view1移動到view2時顯示,但是當從view3返回到view2時 - 標題消失。我使用Google搜索,試圖添加self.title = @"name"viewDidLoad,initWithNibName,viewWillAppear - 這些都不起作用。

+0

要回答點1.我會說這是正確的。 要點2.從您粘貼的代碼中,它應該按預期工作。您正在使用導航控制器中的內置後退按鈕? –

+0

是的,我不使用自定義導航項目或導航欄。其餘的代碼是默認的,什麼都不做。也許原因是我應該在界面構建器中創建導航欄,創建出口,屬性等? –

+0

不,您不需要在界面構建器中創建它,如果您不想。我真的無法找到你發佈的任何內容。 –

回答

0

所以我解決了導航欄消失的問題。問題出在我的自定義後退按鈕上:self.navigationItem.title = @""; 它正在工作,從後退按鈕的標題「返回」消失了,導航欄的標題也消失了。正確的方法,使後面的按鈕無標題是:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil]; 
[self.navigationItem setBackBarButtonItem:backButton];