uiview 不显示在应用程序委托中
当我在模拟器中执行下面的代码时,我希望看到红色填满屏幕,但实际上全黑,为什么?
- (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 中创建基于 Window 的通用项目后,我决定删除在项目,而是有一个带有视图控制器的应用程序委托,该视图控制器基于设备以编程方式构建视图。现在我似乎无法显示我在应用程序委托中创建的简单视图。
编辑:我删除了添加视图,现在将窗口的背景颜色设置为红色。这没有帮助,但如果我进入模拟器中的桌面并重新打开正在运行的应用程序,我现在会看到红屏。我再次感到困惑,为什么我第一次启动应用程序时没有看到红色。
When I execute the code below in the simulator I expect to see red fill the screen, but instead it's all black, why?
- (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;
}
If I add initialization of window, that doesn't help:
window = [[[UIWindow alloc] init] initWithFrame:[[UIScreen mainScreen] bounds]];
My troubles begun when after creating a Window-based Universal project in Xcode I decided to delete the iPad/iPhone xib files and the iPhone/iPad app delegate files that were automatically created in the project, and instead have a single app delegate with a view controller that builds the view programmatically based on the device. Now I can't seem to display a simple view I create in the app delegate.
Edit: I removed adding a view, and now set the background color of the window to red. That doesn't help, but if I go to desktop in the simulator and reopen the running app I get now a red screen. Again, I'm puzzled as to why I don't see red the first time I launch the app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是设置非 InterfaceBuilder (.xib) 通用 iOS 项目的步骤。
删除应用程序属性列表中与 .xib 文件的所有关联。

从项目中删除所有 .xib 文件
更改 main.m 文件以命名应用程序委托。我选择修改并使用 AppDelegate_iPhone 作为示例。请参阅文档: UI应用程序参考
// main.m
int main(int argc, char *argv[]) {
}
更新您的应用程序委托代码。
// AppDelegate_iPhone.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Here are the steps to setup a non-InterfaceBuilder (.xib) Universal iOS project.
Delete all associations to the .xib files in your property list for the application.

Delete all the .xib files from the project
Change your main.m file to name the application delegate. I chose to modify and use the AppDelegate_iPhone for example purposes. See the documentation: UIApplication Reference
// main.m
int main(int argc, char *argv[]) {
}
Update your application delegate code.
// AppDelegate_iPhone.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
我尝试运行你的代码,它对我有用。我看到的是红色的空景。您是否尝试过添加断点并检查代码是否实际执行?否则我唯一能想到的是它与你的 UIWindow 有关。
I tried to run your code and it worked for me. I got en empty view that was coloured red. Have you tried adding breakpoints and checking that your code actually gets executed? Otherwise the only thing I can come up with is that it is something with your UIWindow.