Iphone uiview没有';不显示应用程序内代理
当我在模拟器中执行下面的代码时,我希望看到红色填充屏幕,但实际上它全是黑色的,为什么Iphone uiview没有';不显示应用程序内代理,iphone,uiview,ipad,uiwindow,Iphone,Uiview,Ipad,Uiwindow,当我在模拟器中执行下面的代码时,我希望看到红色填充屏幕,但实际上它全是黑色的,为什么 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIView * myView = [[UIView alloc] initWithFrame:[window bounds]]; [myView setBackgroundColor
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIView * myView = [[UIView alloc] initWithFrame:[window bounds]];
[myView setBackgroundColor:[UIColor redColor]];
[window addSubview:myView];
[window makeKeyAndVisible];
return YES;
}
如果我添加了窗口的初始化,则没有帮助:
window = [[[UIWindow alloc] init] initWithFrame:[[UIScreen mainScreen] bounds]];
在Xcode中创建了一个基于窗口的通用项目后,我决定删除项目中自动创建的iPad/iPhone xib文件和iPhone/iPad应用程序委托文件,而是使用一个带有视图控制器的应用程序委托,该视图控制器基于设备以编程方式构建视图。现在,我似乎无法显示我在应用程序代理中创建的简单视图
编辑:我删除了添加视图,现在将窗口的背景色设置为红色。这没有帮助,但如果我在模拟器中转到桌面并重新打开运行应用程序,我现在会看到一个红色屏幕。再一次,我很困惑,为什么我第一次启动应用时没有看到红色。我试着运行你的代码,它对我有效。我看到了一个红色的空视图。您是否尝试过添加断点并检查代码是否已实际执行?否则,我唯一能想到的就是它与您的UIWindow有关。以下是设置非InterfaceBuilder(.xib)通用iOS项目的步骤
// Override point for customization after application launch.
NSLog(@"Launch my application iphone");
// Create the window, it's not created without a nib
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create a subview that is red
UIView *myView = [[UIView alloc] initWithFrame:[window bounds]];
[myView setBackgroundColor:[UIColor redColor]];
// Add the subview and release the memory, sicne the window owns it now
[self.window addSubview:myView];
[myView release];
[self.window makeKeyAndVisible];
return YES;
}
我发现问题出在哪里了,我错误地初始化了窗口,我做了如下的双重初始化:[[UIWindow alloc]init]initWithFrame:bounds]。一旦我删除了第一个初始化,我就让它工作了。我在这个LOL上花了6个多小时。谢谢!有时你只需要离开键盘一段时间,然后再回来。很高兴听到你解决了它。这是一个很好的答案,并概述了我采取的步骤,但更详细。结果表明,我的错误是初始化了两次UIWindow(见上文)。现在一切正常。谢谢你的出色回答。不客气,我发现这在我刚开始时很令人沮丧,因为有很多步骤和地方可以编辑项目设置/代码。我不知道为什么,但在我这方面,代码的格式不正确,即使我试图将其标记为代码。
// Override point for customization after application launch.
NSLog(@"Launch my application iphone");
// Create the window, it's not created without a nib
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create a subview that is red
UIView *myView = [[UIView alloc] initWithFrame:[window bounds]];
[myView setBackgroundColor:[UIColor redColor]];
// Add the subview and release the memory, sicne the window owns it now
[self.window addSubview:myView];
[myView release];
[self.window makeKeyAndVisible];
return YES;
}