如果我的navigationController返回“(null)”,我该怎么办? [重复]

时间:2021-03-23 03:59:40

Possible Duplicate:
Why can't I push a new view controller onto the current view?
Why is my new view controller not appearing?

可能重复:为什么我不能将新的视图控制器推送到当前视图?为什么我的新视图控制器没有出现?

I need to push my view controller from my navigationController, but after having done an NSLog statement to find out why nothing was showing with the following code, I realised that it was returning null:

我需要从我的navigationController推送我的视图控制器,但在完成一个NSLog语句以找出为什么没有显示以下代码,我意识到它返回null:

-(IBAction)doChangePasscode{

NSLog(@"Change Passcode Screen Loaded!"); 

ChangePasscode *cpscreen = [[ChangePasscode alloc] initWithNibName:@"ChangePasscode" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:cpscreen animated:YES];

NSLog(@"%@",self.navigationController); 

}

Why is this happening? What can I do to return a proper value other than (null)?

为什么会这样?除了(null)之外,我还能做些什么来返回正确的值?

2 个解决方案

#1


1  

Like everyone has explained in this and Why is my new view controller not appearing?, you need to have a navigation controller first. Use that nav controller to push your view controllers and then your view controllers will no longer have null for the navigationController property.

就像每个人都在这里解释的那样,为什么我的新视图控制器没有出现?,你需要先安装一个导航控制器。使用该nav控制器推送视图控制器,然后视图控制器将不再具有navigationController属性的null。

In this example someSecondViewController would be the self in your code above:

在这个例子中,someSecondViewController将是你上面代码中的self:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    self.nav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
    [rootViewController release];
    [self.window addSubview:nav.view];
    [self.window makeKeyAndVisible];
}

- (void)someOtherMethod {
    SecondViewController *someSecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self.nav pushViewController:someSecondViewController animated:YES];
    [someSecondViewController release];
}

Please review these:

请查看以下内容:

View Controller Programming Guide for iOS
UINavigationController Docs
UIViewController Docs

适用于iOS的View Controller编程指南UINavigationController文档UIViewController文档

#2


0  

I am assuming you mean returning nil, that means the viewController does not have a navigation controller, you can solve it by having your viewController be the root of a newly created UINavigationController like this:

我假设你的意思是返回nil,这意味着viewController没有导航控制器,你可以通过让你的viewController成为新创建的UINavigationController的根来解决它,如下所示:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myRootViewController];

Then for myRootViewController self.navigationController will return the navigation Controller.

然后对于myRootViewController,self.navigationController将返回导航控制器。

I am assuming that viewController is the first one in your app so you want to have this on your appDelegate:

我假设viewController是你应用程序中的第一个,所以你想在你的appDelegate上有这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MyViewController *viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
    UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];

    self.window.rootViewController = myNavController;
    [self.window makeKeyAndVisible];

    return YES;
}

Then if that IBAction is on MyViewController self.navigationController will return the navigation Controller instead of nil.

然后,如果IBAction在MyViewController上,则self.navigationController将返回导航控制器而不是nil。

Hope that helps.

希望有所帮助。

#1


1  

Like everyone has explained in this and Why is my new view controller not appearing?, you need to have a navigation controller first. Use that nav controller to push your view controllers and then your view controllers will no longer have null for the navigationController property.

就像每个人都在这里解释的那样,为什么我的新视图控制器没有出现?,你需要先安装一个导航控制器。使用该nav控制器推送视图控制器,然后视图控制器将不再具有navigationController属性的null。

In this example someSecondViewController would be the self in your code above:

在这个例子中,someSecondViewController将是你上面代码中的self:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    self.nav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
    [rootViewController release];
    [self.window addSubview:nav.view];
    [self.window makeKeyAndVisible];
}

- (void)someOtherMethod {
    SecondViewController *someSecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self.nav pushViewController:someSecondViewController animated:YES];
    [someSecondViewController release];
}

Please review these:

请查看以下内容:

View Controller Programming Guide for iOS
UINavigationController Docs
UIViewController Docs

适用于iOS的View Controller编程指南UINavigationController文档UIViewController文档

#2


0  

I am assuming you mean returning nil, that means the viewController does not have a navigation controller, you can solve it by having your viewController be the root of a newly created UINavigationController like this:

我假设你的意思是返回nil,这意味着viewController没有导航控制器,你可以通过让你的viewController成为新创建的UINavigationController的根来解决它,如下所示:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myRootViewController];

Then for myRootViewController self.navigationController will return the navigation Controller.

然后对于myRootViewController,self.navigationController将返回导航控制器。

I am assuming that viewController is the first one in your app so you want to have this on your appDelegate:

我假设viewController是你应用程序中的第一个,所以你想在你的appDelegate上有这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MyViewController *viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
    UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];

    self.window.rootViewController = myNavController;
    [self.window makeKeyAndVisible];

    return YES;
}

Then if that IBAction is on MyViewController self.navigationController will return the navigation Controller instead of nil.

然后,如果IBAction在MyViewController上,则self.navigationController将返回导航控制器而不是nil。

Hope that helps.

希望有所帮助。