Search code examples
iphoneiosobjective-cuiviewcontrolleruiwindow

changing View from UIWindow class


I would like to know how can i change the view from a UIWindow class..for example, My app has a countdown timer view, when a user starts the timer, they can switch to other screen and the timer runs in the statusbar, what i want is when the user taps on the status bar (i have a custom button on the top of the status bar), it fires this method and change the view from current view to timer's view...

- (void)statusbarButtonTouched
{

    NSLog(@"Button TOuched");

    [self addSubview:??]

}

Solution

  • Create timerView in App delegate with defined property:

    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;
    }
    

    Code to bring this view on front:

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

    That's it. You can pull for running demo from here https://github.com/rptwsthi/TrickTest.

    Cheers.