gpt4 book ai didi

iphone - uiview 不显示在应用程序委托(delegate)中

转载 作者:行者123 更新时间:2023-12-03 19:07:14 25 4
gpt4 key购买 nike

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

- (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 的通用项目后,我决定删除项目中自动创建的 iPad/iPhone xib 文件和 iPhone/iPad 应用程序委托(delegate)文件,取而代之的是单个应用程序委托(delegate)时,我的麻烦就开始了使用 View Controller ,根据设备以编程方式构建 View 。现在我似乎无法显示在应用程序委托(delegate)中创建的简单 View 。

编辑:我删除了添加 View ,现在将窗口的背景颜色设置为红色。这没有帮助,但如果我进入模拟器中的桌面并重新打开正在运行的应用程序,我现在会看到红屏。我再次感到困惑,为什么我第一次启动应用程序时没有看到红色。

最佳答案

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

  1. 删除应用程序属性列表中与 .xib 文件的所有关联。 Delete the .xib file references from your application plist

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

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

    //main.mint 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. 更新您的应用程序委托(delegate)代码。

    //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;
    }

关于iphone - uiview 不显示在应用程序委托(delegate)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4415807/

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