任何其他类中的UINavigationController

时间:2014-05-06 14:58:05

标签: ios objective-c uinavigationcontroller

您好我正在尝试制作UINavigationController而不是在mainViewController(第一个viewControllerClass)中,我需要将UINavigationController放在第二个类中。但是,如果我将在appDelegate.m中编写这些代码

- (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;
    selectDay=@"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 个答案:

答案 0 :(得分:1)

我看到的第一个错误是你试图将窗口rootviewcontroller设置两次。视图层次结构需要像window->导航控制器 - >视图控制器。所以我用你的代码做了一些修改。

- (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)视图控制器推送到navigationcontroller堆栈,您可以通过self.navigationController访问导航控制器,因此您的代码的第二部分应该是这样的;

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

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

}