UIView在应用程序代理中未显示。

4
当我在模拟器中执行下面的代码时,我期望看到屏幕上填充了红色,但实际上全是黑色,为什么?
- (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应用程序代理文件,而是只有一个应用程序代理和一个视图控制器,根据设备以编程方式构建视图。现在我似乎无法显示我在应用程序代理中创建的简单视图。
编辑:我移除了添加视图,并将窗口的背景颜色设置为红色。这并没有帮助,但如果我去模拟器中的桌面并重新打开运行中的应用程序,我现在会得到一个红色屏幕。再次,我困惑为什么我第一次启动应用程序时看不到红色。
2个回答

8
以下是设置非InterfaceBuilder (.xib) 通用 iOS 项目的步骤。
  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;
    }
    

这是一个很好的答案,详细地概述了我所采取的步骤。结果发现我的错误在于初始化UIWindow两次(请参见上文)。现在一切都正常了。感谢您的出色回答。 - andrewz
不用谢,我刚开始的时候也觉得这很令人沮丧,因为有许多步骤和需要编辑项目设置/代码的地方。我不确定为什么,但在我的端上,即使我尝试将其标记为代码,代码格式也不正确。 - Paul Solt

1

我尝试运行了你的代码,它对我来说是有效的。我得到了一个红色的空视图。你是否尝试添加断点并检查你的代码是否真正被执行?否则,我能想到的唯一可能是与你的UIWindow有关。


我找到了问题所在,是因为我错误地初始化了窗口,我进行了双重初始化,像这样:[[[UIWindow alloc] init] initWithFrame:bounds]。一旦我删除了第一个初始化,它就可以正常工作了。我花了超过6个小时来解决这个问题,哈哈。谢谢! - andrewz
有时候你只需要离开键盘一段时间,稍后再回来。很高兴听到你解决了它。 - ehenrik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接