uiview 不显示在应用程序委托中

发布于 2024-10-07 00:07:54 字数 733 浏览 7 评论 0原文

当我在模拟器中执行下面的代码时,我希望看到红色填满屏幕,但实际上全黑,为什么?

- (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 技术交流群。

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

发布评论

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

评论(2

拥抱影子 2024-10-14 00:07:54

以下是设置非 InterfaceBuilder (.xib) 通用 iOS 项目的步骤。

  1. 删除应用程序属性列表中与 .xib 文件的所有关联。
    从应用程序 plist 中删除 .xib 文件引用

  2. 从项目中删除所有 .xib 文件

  3. (可选)创建一个公共应用程序委托类,然后删除 AppDelegate_iPad.* 和 AppDelegate_iPhone.* 文件。 (删除之前从现有文件中复制粘贴任何代码)
  4. 更改 main.m 文件以命名应用程序委托。我选择修改并使用 AppDelegate_iPhone 作为示例。请参阅文档: UI应用程序参考

    // main.m
    int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // 使用 .xib 文件启动 iPhone/iPad 应用程序(默认值在 .plist 中设置)
    //int retVal = UIApplicationMain(argc, argv, nil, nil);
    
    // 以编程方式启动 iPhone/iPad 应用程序
    // 将第三个参数设置为 nil,以使用默认的 UIApplication
    // 将第四个参数设置为 AppDelegate 类的字符串名称
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate_iPhone");
    
    [矿池释放];
    返回retVal;
    

    }

  5. 更新您的应用程序委托代码。

    // AppDelegate_iPhone.m
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     // 应用程序启动后覆盖自定义点。
        NSLog(@"启动我的 iPhone 应用程序");
    
        // 创建窗口,没有笔尖就不会创建
        window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]]; 
    
        // 创建一个红色的子视图
        UIView *myView = [[UIView alloc] initWithFrame:[窗口边界]];
        [myView setBackgroundColor:[UIColor redColor]];
    
        // 添加子视图并释放内存,因为窗口现在拥有它
        [self.window addSubview:myView];
        [myView发布]; 
    
        [self.window makeKeyAndVisible];
    
        返回是;
    }
    

Here are the steps to setup a non-InterfaceBuilder (.xib) Universal iOS project.

  1. Delete all associations to the .xib files in your property list for the application.
    Delete the .xib file references from your application plist

  2. Delete all the .xib files from the project

  3. (Optional) Create a common Application Delegate class, and then delete the AppDelegate_iPad.* and the AppDelegate_iPhone.* files. (Copy paste any code from the existing files before deleting)
  4. 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[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // Start an iPhone/iPad App using a .xib file (Defaults set in .plist)
    //int retVal = UIApplicationMain(argc, argv, nil, nil);
    
    // Start the iPhone/iPad App programmatically
    // Set the 3rd argument to nil, to use the default UIApplication
    // Set the 4th argument to the string name of your AppDelegate class
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate_iPhone");
    
    [pool release];
    return retVal;
    

    }

  5. Update your application delegate code.

    // AppDelegate_iPhone.m
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // 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;
    }
    
只有影子陪我不离不弃 2024-10-14 00:07:54

我尝试运行你的代码,它对我有用。我看到的是红色的空景。您是否尝试过添加断点并检查代码是否实际执行?否则我唯一能想到的是它与你的 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.

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