2014-05-06 112 views
0

嗨,我想使UINavigationController但不是在mainViewController(第一viewControllerClass),我需要把UINavigationController放在第二類。但是,如果我將這些代碼寫入appDelegate.mUINavigationController在任何其他類

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 


     // Override point for customization after application launch. 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    // [window addSubview:[navigationController view]]; 


    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navigation; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

然後UINavigationController出現在mainView中。我試圖把它放在其他類一樣,

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    // strWhichTaleOnScreen=[masivTaleNames objectAtIndex:indexPath.row]; 
    NSString *selectDay; 
    [email protected]"first string"; 
    NSLog(@"selecDay=%@",selectDay); 
     classDetailOfMessagesViewController *nesneDetailOfMessagesViewController = [[classDetailOfMessagesViewController alloc] initWithNibName:@"classDetailOfMessagesViewController" bundle:nil]; 
    nesneDetailOfMessagesViewController.selectDay = selectDay; 
    [navigation pushViewController: nesneDetailOfMessagesViewController animated:YES]; 
    nesneDetailOfMessagesViewController = nil; 





} 

但它不工作,我想我在這第二視圖中創建RootViewController的,但我不知道怎麼辦。 如果有人能指示我解決問題,我會很高興。

回答

1

我看到的第一個錯誤是你試圖設置窗口rootviewcontroller兩次。視圖層次結構需要像窗口 - >導航控制器 - >視圖控制器。所以我對你的代碼做了一些修改。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 


     // Override point for customization after application launch. 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navigation; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

在第二個代碼示例中,我無法找到對導航的引用。而且,如果你將(視情況initWithRootViewController)視圖控制器push到navigationcontroller棧,你可以通過self.navigationController訪問導航控制器,這樣你的代碼的第二部分應該是這樣的;

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *selectDay; 
    [email protected]"first string"; 
    NSLog(@"selecDay=%@",selectDay); 
     classDetailOfMessagesViewController *nesneDetailOfMessagesViewController = [[classDetailOfMessagesViewController alloc] initWithNibName:@"classDetailOfMessagesViewController" bundle:nil]; 
    nesneDetailOfMessagesViewController.selectDay = selectDay; 
    [self.navigationController pushViewController: nesneDetailOfMessagesViewController animated:YES]; 

} 
+0

是的,感謝您的糾正。我添加了導航=(UINavigationController *)self.window.rootViewController;在「viewDidLoad」方法,然後我可以達到「classDetailOfMessagesViewController」的「initWithNibName」方法,但無法達到「viewDidLoad」,你有一個想法,爲什麼? –

+0

好吧,我解決了這個問題,我的錯誤是與presentModalView去二等。據我瞭解,我必須去每個xib與pushViewController UINavigationController。 –

+0

推送和現在模態有不同的用法。如果你想展示相關的東西(例如新聞的細節),你應該推動detailsviewcontroller。現在的模式更適合不合情理的東西。 – cekisakurek