以编程方式将 UIButton 添加到 rootview,我做错了什么?
我有新设置的应用程序,只有一个窗口和一个根视图(来自 XCode 提供的 iOS 单视图应用程序模板)。
现在我尝试向其添加一个按钮。
相应的代码如下所示:
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIButton* button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button0.frame = CGRectMake(140, 230, 40, 20);
[button0 setTitle:@"Exit" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:button0];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
else
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
当我启动应用程序时,我只能看到空视图。
但是,当注释掉该行(将视图作为根添加到窗口中,而不是直接将按钮添加到窗口中)时,我可以很好地看到该按钮。
那么为什么这不适用于视图呢?
I have freshly setup app, just with a window an a root view (from the iOS single view application template, provided by XCode).
Now I try to add a button to it.
The according code looks like this:
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIButton* button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button0.frame = CGRectMake(140, 230, 40, 20);
[button0 setTitle:@"Exit" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:button0];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
else
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
When I start the app, I can only see the empty view.
However, when comment out the line, which adds the view as root to the window and instead directly add the button to the window, then I can see the button just fine.
So why isn't this working with the view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,即使在分配 viewController 之前,您也将 button0 添加为 self.viewController.view 的子视图。
当您调用 addSubview: 方法时,self.viewController 为nil。因此,该按钮不会添加到 viewController 中。您应该在分配viewController后添加按钮。
The problem is that, you are adding button0 as subview of self.viewController.view even before allocating the viewController.
By the time you call addSubview: method, self.viewController is nil. So, the button is not added to the viewController. You should add the button after you allocate the viewController.
首先,创建根视图控制器,然后向其添加子视图。你却反其道而行之。
At first, create the root viewcontroller, later add subviews to it. You did it the other way round.