added subview won't show after the app relaunch - iphone

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

Related

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

RootViewController's view is not beeing resized with auto layout

Hi when using autolayout on iOS 6, my "rootView" of my rootViewController doesnt seem to resize properly if at all.
My rootViewController is loaded from a nib and i am adding it to the view-hierarchy like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
return YES;
}
But when i rotate the simulator the view of rootVC doesnt resize. Since you cant set any constraints to the "rootview" in a xib, I also tried this but without any effect:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
NSArray *vConst = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
NSArray *hConst = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
[self.window addConstraints:vConst];
[self.window addConstraints:hConst];
return YES;
}
When i log-out the frame-size of the rootvc's view, it's always the portrait-format (w:768, h:1024)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
NSLog(#"%f, %f", self.view.frame.size.width, self.view.frame.size.height);
}
could someone please tell me what i'm missing here? i've wasted hours with this problem and got nowhere^^
thx
It seems that a lot of things can go wrong with the autolayout feature iOS 6. Ambiguities in the constraints and failing to disable the translatesAutoresizingMaskIntoConstraints in your view controllers can cause much conflict. So ensure there is no ambiguities in the constraints by calling the autolayoutTrace method in lldb: po [[UIWindow keyWindow] _autolayoutTrace]
If there are ambiguities, you must resolve them.

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

How do I get the rotation animation using the ViewController class?

I'm not getting the rotation animation provided by the ViewController class. I thought I set it up correctly but something is wrong because its not happening and in the debugger I only hit the breakpoint at the function shouldAutorotateToInterfaceOrientation only once at startup but not when I rotate the device in the simulator. Does anyone know what I might be doing wrong?
GLViewController.m
#import "GLViewController.h"
#import "GLView.h"
#implementation GLViewController
- (void)loadView {
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
GLView *glView = [[[GLView alloc] initWithFrame:applicationFrame] autorelease];
self.view = glView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// We can run in landscape mode
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize m_window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.m_window = [[UIWindow alloc] initWithFrame: screenBounds];
//m_view = [[GLView alloc] initWithFrame: screenBounds];
m_viewController = [GLViewController alloc];
[m_window addSubview:m_viewController.view];
// Override point for customization after application launch.
[self.m_window addSubview: m_viewController.view];
[self.m_window makeKeyAndVisible];
}
You're not calling init on your GLViewController instance. This could cause all kinds of strange behaviour, including (perhaps) the rotation callbacks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.m_window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
m_viewController = [[GLViewController alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.m_window.rootViewController = m_viewController;
[self.m_window makeKeyAndVisible];
return YES;
}
I think you should try to create UINavigationController's object and push your view controller(GLViewController) on it in "didFinishLaunchingWithOptions". Add UINavigationController.view as a subview instead of view controller's view.

How to switch from one UIViewController to another?

I have a problem with switching from one viewcontroller to another.
Here is what I have :
In my "AppDelegate_iPad.m" :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
[window addSubview:loginView.view];
[self.window makeKeyAndVisible];
return YES;
}
That works. Now I have a login button on that screen and when pressed it calles this :
- (IBAction)doPressLoginButton
{
DebugLog(#"doPressLoginButton");
MenuViewController_iPad *menuView = [[MenuViewController_iPad alloc] initWithNibName:#"MenuViewController_iPad" bundle:nil];
[self.navigationController pushViewController:menuView animated:YES];
[menuView release];
}
The problem is that nothing happends. I assume I am missing the actual navigationconroller ?!
Hope that anyone can help.
Thank you
You should create a UINavigationController instance variable in your app delegate. Then, make sure you synthesize it and release it in your dealloc method.
Your implementation of application:didFinishLaunchingWithOptions: could look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// window is usually an IBOutlet
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginView];
[loginView release];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Now you'll be able to use self.navigationController.
I assume I am missing the actual navigationconroller ?!
you are right. :D
self.navigationController returns nil if you don't set up an NavigationController.
And any messages send to nil object will be ignored.
If you only need switch from one to another.
using
[self presentModalViewController:modalViewController animated:YES];
instead.