0

我想知道如何从 UIWindow 类更改视图。例如,我的应用程序有一个倒数计时器视图,当用户启动计时器时,他们可以切换到其他屏幕并且计时器在状态栏中运行,什么我想要的是当用户点击状态栏(我在状态栏顶部有一个自定义按钮)时,它会触发此方法并将视图从当前视图更改为计时器视图...

- (void)statusbarButtonTouched
{

    NSLog(@"Button TOuched");

    [self addSubview:??]

}
4

2 回答 2

0

timerView使用定义的属性在 App 委托中创建:

AppDelegate.h

@property (nonatomic, retain) UIView *timerView;

AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[FLViewController alloc] initWithNibName:@"FLViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];


    self.timerView = [[UIView alloc] initWithFrame:self.window.frame];
    [self.timerView setBackgroundColor:[UIColor greenColor]];
    [self.window addSubview:self.timerView];
    [self.timerView setHidden:YES];


    return YES;
}

将此视图放在前面的代码:

- (IBAction)shouTimerViewTouched:(id)sender {
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.timerView setHidden:NO];
    [delegate.window bringSubviewToFront:delegate.timerView];
}

而已。您可以从这里https://github.com/rptwsthi/TrickTest拉来运行演示。

干杯。

于 2013-04-24T06:51:51.177 回答
0

根据文档

- (void)removeFromSuperview

取消接收者与其父视图和窗口的链接,并将其从响应者链中移除。

于 2013-04-24T05:11:27.917 回答