gpt4 book ai didi

iphone - AppDelegate UIWindow addSubView 在不同的 viewController

转载 作者:可可西里 更新时间:2023-11-01 04:36:17 25 4
gpt4 key购买 nike

我正在尝试从 UIViewController 在 AppDelegate 的 UIWindow 中添加一个 UILabel。我就是这样做的:

AppDelegate 代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}



[self.window makeKeyAndVisible];

self.window.rootViewController = self.viewController;


return YES;
}

ViewController 代码:

- (void)viewDidLoad
{

UILabel *abcd=[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 40.0)];

abcd.text=@"loading...";

abcd.backgroundColor=[UIColor clearColor];

[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];

[super viewDidLoad];


}

但我看到的只是灰色屏幕,没有标签。我可能哪里出错了?

最佳答案

您不能将 UILabel 添加到 UIWindow,您应该添加到 UIViewController。更改此行:

[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];

为此:

[self.view addSubview:abcd];

关于iphone - AppDelegate UIWindow addSubView 在不同的 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12176666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com