changing View from UIWindow class - iphone

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:??]
}

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.

as per docs
- (void)removeFromSuperview
Unlinks the receiver from its superview and its window, and removes it
from the responder chain.

Related

UIButton created programmatically but doesn't show up on view (addSubview was called)

Have an AppDelegate and a MainViewController.
In MainViewController the UIButton was created, AppDelegate sets a background image and should show it on MainViewController. That's pretty much it but I can't see the UIButton.
Just for alignment purpose I created an IBOutlet and connected the button in IB.
MainViewController.h
#property (strong, nonatomic) IBOutlet UIButton *searchBtn;
MainViewController.m
#synthesize searchBtn;
AppDelegate.m
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
self.window.backgroundColor = [UIColor whiteColor];
// Buttons > Set background images:
[self.mainVC.searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
[self.mainVC.view addSubview:self.mainVC.searchBtn];
[self.window makeKeyAndVisible];
return YES;
}
First you don't need IBOutlet if you are creating it programmatically.
Second you probably want to move the creation of the button in viewDidLoad of your view controller rather than in your AppDelegate. It's your view controller's business.
Third you should probably alloc init your button if you create it programmatically:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:#selector(something:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
something like that.
You should be adding buttons in your MainViewController class, not the AppDelegate first off.
You should also set the background of the view in MainViewController, not the AppDelegate.
It should look like this:
AppDelegate.m
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
[self.window makeKeyAndVisible];
return YES;
}
In your MainViewController.m's viewDidLoad put this
- (void)viewDidLoad {
[super viewDidLoad];
// Buttons > Set background images:
[searchBtn setBackgroundImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
//If you adding the button in Interface Builder you don't need to add it again
//[self.view addSubview:self.mainVC.searchBtn];
}

Hiding the UINavigationController on the first .xib file only

This may be a stupid question but I programmatically added a UINavigationController to my app. If possible, I wanted to just add it to the top of all my windows except for the very first .xib. Maybe even just hide it on my first .xib. Is it possible to even do that? I think of my first .xib file that opens up to the rest of my app like a cover page and I rather that blue bar not show up at the top of that. I wish I could show you pictures but don't have enough reps yet. Thanks!
Below is the code I believe helps me to provide each page of app with the back bar:
#import "McCormick_TaylorViewController.h"
#implementation McCormick_TaylorAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (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 = [[[McCormick_TaylorViewController alloc]
initWithNibName:#"McCormick_TaylorViewController" bundle:nil] autorelease];
UINavigationController * navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
in your McCormick_TaylorViewController's viewWillApper: method
just use bellow code...
[self.navigationController setNavigationBarHidden:NO animated:YES];
and in other view controller in navigationbar ot display then in another viewController's viewWillAppear just use bellow code..
[self.navigationController setNavigationBarHidden:NO animated:NO];
Use this method:
- (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 = [[[McCormick_TaylorViewController alloc]
initWithNibName:#"McCormick_TaylorViewController" bundle:nil] autorelease];
UINavigationController * navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
[navController.navigationBar setHiden:YES]; // hides navigation bar
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

added subview won't show after the app relaunch

I have a subview added on app relaunch, on the applicationDidBecomeActive got called, but it's not shown, what could be the reason?
- (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]];
}
What's more interesting is that - it'll shown if I leave the app launched, turn off the device, then turn the device back on. But if I close the app, then relaunch it, I won't be able to see the subview.
Please add the following line like this,
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
[window addSubview:[self mySubView]];
[window makeKeyAndVisible];
}
and also change the color [UIColor blueColor];
You will see the effect sure
Enjoy!
IF you're on your App delegate and you have a window property, as you should have, you could just simply do this:
[[self window] addSubview:[self mySubView]];
For setting the window property do this:
In your AppDelegate.h
#property (strong, nonatomic) UIWindow *window;
In your AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
.
.
.
//Create your main controller
.
.
.
[self.window setRootViewController:mainController];
[self.window makeKeyAndVisible];

Why I can't release the navigation controller of the view I added to the window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
// Create First view
FirstViewController *firstViewCTL = [[FirstViewController alloc] init];
// Create UINavigationController
UINavigationController *navCTL = [[UINavigationController alloc] init];
[[navCTL navigationBar] setBarStyle:UIBarStyleBlack];
[navCTL pushViewController:firstViewCTL animated:NO];
[firstViewCTL release];
[[self window] addSubview:navCTL.view];
[navCTL release];
[[self window] makeKeyAndVisible];
return YES;
}
I understand that adding subview (addSubview:) will retain the view to be aded. But why I can't now release my navigation controller (navCTL) that own the views that has already been retained
-addSubview: retains the view, not the view controller.
You can use UIWindow's rootViewController property (iOS 4 and higher) to retain the view controller, it also saves you adding the view as a subview yourself.
...
[window setRootViewController:navCTL];
[navCTL release];
...

Correct way to show Login Screen followed by UITabbarController menu

I need to implement the following and I wanted to know the correct way to do it.
when the iPhone application launches, I need to show a logo image for 2 seconds followed by showing a login screen that allows the person to login or create an account. Once the person logs in, i need to show a tabbarcontroller menu options.
This is how I'm currently doing it:
In the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
LoginViewController *viewController0 = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *aNavigationController0 = [[UINavigationController alloc] initWithRootViewController:viewController0];
self.window.rootViewController = aNavigationController0;
// I also implement an iVar of the UITabBarController here...
// ....
}
The #implementation:
#implementation LoginViewController
- (IBAction)createNewAccountButtonClicked:(id)sender {
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = delegate.tabBarController;
}
So, my questions are:
Is this the correct way to show the tabbar for my purpose?
In this scheme of things, I cannot show the logo animated. Any pointers on how to do this?
The code below assumes you're using ARC, if you're not then you'll need to do your MRC.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = self.tabBarController;
LoginViewController *loginViewController= [[LoginViewController alloc] initWithNibName:nil bundle:nil];
loginViewController.delegate = self;
UINavigationController *loginNavCont = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[self.tabBarController presentModalViewController:loginNavCont animated:NO];
UIImageView *splashScreen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default"]];
[self.window addSubview:splashScreen];
[UIView animateWithDuration:0.5
delay:2.0
options:0
animations:^{
splashScreen.alpha = 0.0;
}
completion:^(BOOL finished) {
[splashScreen removeFromSuperview];
}];
[self.window makeKeyAndVisible];
return YES;
}
- (void)loginViewControllerShouldBeDismissed:(UIViewController *)viewController
{
[self.tabBarController dismissModalViewControllerAnimated:YES];
}