从UIWindow类更改View

时间:2013-04-24 04:54:57

标签: iphone ios objective-c uiviewcontroller uiwindow

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

- (void)statusbarButtonTouched
{

    NSLog(@"Button TOuched");

    [self addSubview:??]

}

2 个答案:

答案 0 :(得分:0)

根据文档

- (void)removeFromSuperview
  

取消接收器与其超视图及其窗口的链接,并将其删除   来自响应者链。

答案 1 :(得分:0)

在具有已定义属性的App委托中创建timerView

<强> 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提取运行演示。

干杯。