以编程方式将 UIButton 添加到 rootview,我做错了什么?

发布于 2024-12-05 21:54:31 字数 1191 浏览 1 评论 0原文

我有新设置的应用程序,只有一个窗口和一个根视图(来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单调的奢华 2024-12-12 21:54:31

问题是,即使在分配 viewController 之前,您也将 button0 添加为 self.viewController.view 的子视图。

当您调用 addSubview: 方法时,self.viewControllernil。因此,该按钮不会添加到 viewController 中。您应该在分配viewController后添加按钮。

self.viewController = [[ViewController alloc]...
[self.viewController.view addSubview:button0];

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.

self.viewController = [[ViewController alloc]...
[self.viewController.view addSubview:button0];
倾城花音 2024-12-12 21:54:31

首先,创建根视图控制器,然后向其添加子视图。你却反其道而行之。

At first, create the root viewcontroller, later add subviews to it. You did it the other way round.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文