xcode 不为 rootViewController 调用 viewDidLoad
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18178177/
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
viewDidLoad is not called for rootViewController
提问by MrCarder
This is my first iOS project. I'm just following the tutorial from Try iOS over at codeschool in my XCode Application.
这是我的第一个 iOS 项目。我只是在我的 XCode 应用程序中按照 Codeschool 的 Try iOS over 教程进行操作。
I've added a button in my viewDidLoad
method. I don't think this is where it would be added normally, but it should still work. The problem is, the method is never called. Here's my code so far:
我在我的viewDidLoad
方法中添加了一个按钮。我不认为这是正常添加的地方,但它应该仍然有效。问题是,该方法从未被调用。到目前为止,这是我的代码:
AppDelegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Set window size & background color
CGRect viewRect = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:viewRect];
self.viewController = [[ViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:viewRect];
view.backgroundColor = [UIColor yellowColor];
self.viewController.view = view;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
NSLog(@"Screen is %f tall and %f wide", viewRect.size.height, viewRect.size.width);
return YES;
}...
AppDelegate.h:
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstButton.frame = CGRectMake(100, 100, 100, 44);
[firstButton setTitle:@"Don't Click!" forState:UIControlStateNormal];
[self.view addSubview:firstButton];
}...
When the app is up and running, my background remains yellow, and there is no button visible.
当应用程序启动并运行时,我的背景保持黄色,并且没有可见的按钮。
Stepping through the application, didFinishLaunchingWithOptions:
hits as I expected. When the view is initializes, I expect the viewDidLoad to run. I may just be misunderstanding the way C "sends messages".
逐步完成应用程序,didFinishLaunchingWithOptions:
如我所料。当视图初始化时,我希望 viewDidLoad 运行。我可能只是误解了 C“发送消息”的方式。
If any additional information might help please let me know. Thanks for any help :)
如果任何其他信息可能有帮助,请告诉我。谢谢你的帮助 :)
EDIT
编辑
Does this have something to do with storyboarding? I'm in storyboard mode but I never added any button using the GUI
这与情节提要有关吗?我处于故事板模式,但我从未使用 GUI 添加任何按钮
采纳答案by rdelmar
viewDidLoad is not being called because of the non-standard way that you're creating its view. Usually, the view is loaded from a xib or storyboard, or created in code in the loadView method of the view controller -- in all these cases, viewDidLoad will be called. If you move your view creation to loadView in the controller's .m file, it will work, and viewDidLoad will be called.
viewDidLoad 未被调用,因为您创建其视图的方式非标准。通常,视图是从 xib 或故事板加载的,或者在视图控制器的 loadView 方法中的代码中创建的——在所有这些情况下,都会调用 viewDidLoad。如果您将视图创建移动到控制器的 .m 文件中的 loadView,它将起作用,并且将调用 viewDidLoad。
After Edit:
编辑后:
If you're using a storyboard, your view should be created there, and you shouldn't be doing what you're doing in the app delegate. In fact, when you use a storyboard, you don't normally have any code in the application:didFinishLaunchingWithOptions: method.
如果您使用的是故事板,您的视图应该在那里创建,并且您不应该在应用程序委托中执行您正在执行的操作。实际上,当您使用故事板时,您通常在 application:didFinishLaunchingWithOptions: 方法中没有任何代码。
回答by Nickolas
...
self.viewController.view = view;
...
...
self.viewController.view = view;
...
You shouldn't set viewController's view before viewDidLoad, which cause viewDidLoad not called. View should be set in viewDidLoad, and the viewController's will manage its view automatically for you.
你不应该在 viewDidLoad 之前设置 viewController 的视图,这会导致 viewDidLoad 不被调用。View 应该在 viewDidLoad 中设置,viewController 会自动为你管理它的视图。
回答by Jonathan
I had this issue, and the initWithCoder function was called when I added some code in the AppDelegate's didFinishLaunchingWithOptions function.
我遇到了这个问题,当我在 AppDelegate 的 didFinishLaunchingWithOptions 函数中添加一些代码时调用了 initWithCoder 函数。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];(NSDictionary *)launchOptions
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
UIViewController *initViewController = [storyboard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
回答by Jsdodgers
Looking at the code, this is exactly what should be happening. When you create a viewController, it automatically creates a view so you do not want to create a new one for it. Your button is not showing up because it is on the original view created with the viewController but you overwrote it with your yellow view.
查看代码,这正是应该发生的事情。当你创建一个 viewController 时,它会自动创建一个视图,所以你不想为它创建一个新的视图。您的按钮没有显示,因为它位于使用 viewController 创建的原始视图上,但您用黄色视图覆盖了它。
Instead what you want to do is go into ViewController.m
's viewDidLoad:(BOOL)animated
and add:
相反,您想要做的是进入ViewController.m
'sviewDidLoad:(BOOL)animated
并添加:
[self.view setBackgroundColor:[UIColor yellowColor]];
Also, make sure that you do not set viewController.view in appDelegate. If you do not set that, a view is automatically created and you can add your button or any other views to it in the viewDidLoad.
另外,请确保您没有在 appDelegate 中设置 viewController.view。如果您没有设置,则会自动创建一个视图,您可以在 viewDidLoad 中向其添加按钮或任何其他视图。