iPhone在Windows实践中添加视图

时间:2009-11-06 21:08:05

标签: iphone uiview uiviewcontroller

我正在学习iPhone上的Objective-C如何将视图层叠到主窗口上。

我尝试重新制作一个基本程序,在应用程序的主UIView上绘制两个不同颜色的矩形视图,但它似乎不起作用。

我当前的输出是白屏。 然而,当我关闭应用程序时,它会短暂显示两个矩形,因为它们正在关闭,让我相信它们已经被绘制但是由于某种原因没有显示。

假设下面的伪代码(为了StackOverflow而缩短了原始文档,但直接复制到我的项目中)很好,我还需要做什么才能让我的项目显示这些矩形?

我的TestAppDelegate.m文件中来自Apple的代码:

 - (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Create the window object and assign it to the
    // window instance variable of the application delegate.
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor = [UIColor whiteColor];

    // Create and initialize a simple red square

    // Create and initializesimple blue square

    // Add the square views to the window
    [window addSubview:redView];
    [window addSubview:blueView];

    // Once added to the window, release the views to avoid the
    // extra retain count on each of them.
    [redView release];
    [blueView release];

    // Show the window.
    [window makeKeyAndVisible];
}

Apple文档中的原始链接:http://tinyurl.com/y8alvgt

1 个答案:

答案 0 :(得分:0)

您可能需要在窗口上调用setNeedsDisplay

[window setNeedsDisplay];

这会告诉CocoaTouch您的窗口内容已经更改并需要重新绘制。

以下是documentation的链接。