应用重新启动后,添加的子视图不会显示

时间:2011-12-24 05:00:03

标签: iphone window subview

我在应用程序重新启动时添加了一个子视图,在applicationDidBecomeActive被调用,但它没有显示,可能是什么原因?

- (UIView *)mySubView {
    if (_mySubView == nil) {
       _mySubView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       _mySubView.opaque = NO;
       _mySubView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
       _mySubView.exclusiveTouch = YES;
       [_mySubView setUserInteractionEnabled:NO];
    }

    return _mySubView;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    [window addSubview:[self mySubView]];
}

更有趣的是 - 它会显示我是否启动应用程序,关闭设备,然后重新打开设备。但如果我关闭应用程序,然后重新启动它,我将无法看到子视图。

2 个答案:

答案 0 :(得分:1)

请添加以下这样的行,

- (void)applicationDidBecomeActive:(UIApplication *)application     
{

   UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

   [window addSubview:[self mySubView]];

   [window makeKeyAndVisible];

 }

并更改颜色[UIColor blueColor];

你会看到效果确定

享受!

答案 1 :(得分:1)

如果你在你的App代表上并且你有一个窗口属性,就像你应该的那样,你可以简单地这样做:

[[self window] addSubview:[self mySubView]];

要设置窗口属性,请执行以下操作:

在AppDelegate.h中

@property (strong, nonatomic) UIWindow *window;

在AppDelegate.m - (BOOL)应用程序中:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

.
.
.
//Create your main controller
.
.
.

[self.window setRootViewController:mainController];
[self.window makeKeyAndVisible];