Nit*_*ish 5 iphone uiwindow ios
我试图从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;
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
但我所看到的只是灰色屏幕,但没有标签.哪里可能会出错?
1)我建议你颠倒最后两个委托声明的顺序:
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
2) 虽然您应该能够将标签添加到窗口,但这样做有点不正统。无论如何,尝试将标签添加到 viewController 的视图中,看看是否有效,如果有效,并且您确实想将其添加到窗口中(出于某种原因),则在此处添加注释:
[self.view addSubview:abcd];
Run Code Online (Sandbox Code Playgroud)
如果您仍然看不到标签,则视图控制器可能存在问题。您是否在笔尖中定义了任何在启动时应该可见的元素?如果没有,则添加一些内容,以便您可以确定视图实际上已加载。[我使用的一个技巧是将视图的背景颜色设置为红色或蓝色,这样我就可以看到它们实际上已加载。]
| 归档时间: |
|
| 查看次数: |
13926 次 |
| 最近记录: |