xcode viewDidLoad 中的 UINavigationController pushViewController 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6880551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UINavigationController pushViewController in viewDidLoad not working
提问by user559142
I have the following code in my AppDelegate
:
我的代码中有以下代码AppDelegate
:
#import <UIKit/UIKit.h>
@class PersonalDiarySystemViewController;
@interface PersonalDiarySystemAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PersonalDiarySystemViewController *viewController;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet PersonalDiarySystemViewController *viewController;
@property (nonatomic, retain) UINavigationController *navigationController;
@end
#import "PersonalDiarySystemAppDelegate.h"
#import "PersonalDiarySystemViewController.h"
@implementation PersonalDiarySystemAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize navigationController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
self.window.rootViewController = self.viewController;
navigationController = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
navigationController.navigationBar.tintColor = [UIColor
colorWithRed:217.0/255
green:33.0/255
blue:0
alpha:1];
navigationController.navigationBarHidden = YES;
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
My rootviewcontroller tries to load another viewcontroller into the navigation controllers stack in its viewDidLoad
method but for some reason the view is not getting pushed:
我的 rootviewcontroller 尝试在其viewDidLoad
方法中将另一个视图控制器加载到导航控制器堆栈中,但由于某种原因,视图没有被推送:
-(void) viewDidLoad{
lvc = [[LoginViewController alloc] init];
//lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[lvc setDelegate:self];
//[self presentModalViewController:lvc animated:YES];
[self.navigationController pushViewController:lvc animated:YES];
}
I'm getting no errors so not sure whats going on...using presentModalViewController
works...so really am confused!!
我没有收到任何错误,所以不确定发生了什么......使用presentModalViewController
作品......所以真的很困惑!
回答by WrightsCS
You need to assign lvc
to LoginViewController
.
您需要分配lvc
给LoginViewController
.
- (void) viewDidAppear
{
[self performSelector:@selector(loginCheck:) withObject:nil afterDelay:0.5];
}
- (void) loginCheck:(id)sender
{
LoginViewController * lvc = [[LoginViewController alloc] init];
//lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[lvc setDelegate:self];
//[self presentModalViewController:lvc animated:YES];
[self.navigationController pushViewController:lvc animated:YES];
}
回答by Flippinjoe
Put your
把你的
[self.navigationController pushViewController:loginViewController];
Into the
进入
- (void)viewDidAppear:(BOOL)animated
method. The viewControllers navigationController doesn't get loaded until then
方法。viewControllers navigationController 直到那时才被加载
回答by Lewen
There are two things that might go wrong.
First, you alloc the navigation controller in applicationDidFinishLaunching
, I'm not quite sure which goes first, applicationDidFinishLaunching
or viewDidLoad
.
有两件事可能会出错。首先,您将导航控制器分配到 中applicationDidFinishLaunching
,我不太确定哪个先进行,applicationDidFinishLaunching
或者viewDidLoad
.
As you've seen, you first set your root view controller, then alloc the navigation controller, then maybe viewDidLoad
launched right after you set the root view controller, then the navigation controller is allocated. so the words in viewDidLoad
may not work because at that time, the navigation controller hasn't been born yet.
如您所见,您首先设置根视图控制器,然后viewDidLoad
分配导航控制器,然后可能在设置根视图控制器后立即启动,然后分配导航控制器。所以 in 的话viewDidLoad
可能行不通,因为那时导航控制器还没有诞生。
But I don't' quite thing the previous explanation works. it's just a possibility.
但我不完全认为之前的解释有效。这只是一种可能性。
There's another strange thing, you set the navigation bar of your navigation controller hidden
还有一个奇怪的事情,你把你的导航控制器的导航栏设置为隐藏
navigationController.navigationBarHidden = YES;
Then it seems that the user can't pop back to the root view controller, so the navigation controller don't push the login view controller. Meanwhile, the modal view controller can be dismissed with the navigation bar hidden or the navigation bar not allocated, so it works when you present it as a modal view controller.
然后好像用户无法弹回根视图控制器,所以导航控制器不推送登录视图控制器。同时,模态视图控制器可以在导航栏隐藏或导航栏未分配的情况下关闭,因此当您将其呈现为模态视图控制器时它可以工作。
but i'm still not quite sure about it since i'm now having some issues with Xcode, so i can't test the previous two ideas, sorry about that. but i still recommend that you set navigationBarHidden
to NO
.
但我仍然不太确定,因为我现在在使用 Xcode 时遇到了一些问题,所以我无法测试前两个想法,抱歉。但我仍然建议您设置navigationBarHidden
为NO
.
回答by ram
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.navController = navigationController;
[mainViewController release];
[navigationController release];
// Configure and display the window.
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
- (void)viewDidLoad{
lvc = [[LoginViewController alloc] init];
[self.navigationController pushViewController:lvc animated:YES];
}