AppDelegate UIWindow addSubView in different viewController - iphone

I am trying to add a UILabel in UIWindow of AppDelegate from a UIViewController. This is how I am doing this:
AppDelegate code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
[self.window makeKeyAndVisible];
self.window.rootViewController = self.viewController;
return YES;
}
ViewController code :
- (void)viewDidLoad
{
UILabel *abcd=[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 40.0)];
abcd.text=#"loading...";
abcd.backgroundColor=[UIColor clearColor];
[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];
[super viewDidLoad];
}
But all I am seeing is grey screen but no label. Where I might be going wrong?

You must not add UILabel to UIWindow, you should add to UIViewController. Change this line:
[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];
for this:
[self.view addSubview:abcd];

1) I suggest you reverse the order of your last two delegate statements:
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
2) While you should be able to add the label to the window, its somewhat unorthodox to do so. In any case, try adding the label to the viewController's view and see if that works, and if so, and you really want to add it to the window (for some reason), then add a comment here:
[self.view addSubview:abcd];
If you still cannot see the label its likely that there is an issue with the view controller. Did you define anything in the nib - any element that should be visible at launch ? If not then add something just so you can be sure the view is in fact getting loaded. [One trick I use is to set the background color of views to red or blue, so I can see that in fact they got loaded.]

revers the order to
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Then
try adding to the view first not to the window using following code
[self.view addSubview:abcd];
If this does not show your label then the view controller is not getting loaded.
If so then check the property of your xib file.
remove the window alloc line and check the hook up for your window in mainwindow.xib file if the hoockup is not correct then it will not load the view.

Maybe your view controller's view is covering the one you added. Add the new view to the view controller's view instead:
[self.view addSubview:abcd];

Try this after adding UILabel in UIWindow
[[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];

Related

NavigationController not loading view

I'm using xCode 4.3.2 and started a blank application.
I have a navigation controller and a simple logincontroller. I want the login controller to be my root view so it is this first thing that a user does when they login.
I'm using the following code and when I run the application it displays a black screen. I put in logging in the LoginViewController.m->ViewDidLoad and it is being run is there something im doing wrong the LoginViewController.xib is very simple it just contains a button right now that will switch to a tab view controller once I figure this out.
Thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UIViewController *loginController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyWindow];
return YES;
}
This is not right:
[self.window addSubview:navigationController.view];
change it to this:
self.window.rootViewController = navigationController;

View is not displaying after pushing on navigationController stack

I am trying to use a navigation controller to push/pop views, but I don't want the bar at the top with the buttons; I'm doing the navigation UI myself.
So I created a navigationController in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[WSViewController alloc] initWithNibName:#"WSViewController" bundle:nil];
self.window.rootViewController = self.viewController;
self.navController = [[UINavigationController alloc]
initWithRootViewController: self.viewController];
[self.window makeKeyAndVisible];
return YES;
}
and then in my WSViewController, I have an IBAction method that pushes another view on the navigation stack (I've verified that it's doing this correctly; I see it on the stack):
- (IBAction)showInfo:(UIButton *)sender {
if (self.wsInfoViewController == nil) {
WSInfoViewController *wic = [[WSInfoViewController alloc] initWithNibName:#"WSInfoViewController" bundle:nil];
self.wsInfoViewController = wic;
}
[self.navigationController pushViewController:self.wsInfoViewController animated:YES];
}
But I'm not seeing the info view show up when I tap on the info button in my WSViewController (which is showing up just fine).
If I make the navigationController the root controller, then I do see the wsInfoViewController when I tap on the info button, however, I also get the navigation bar at the top, which I don't want!
So... first, am I wrong in thinking I can use a navigation controller this way (i.e. using it for stack purposes but not for any UI at all)?
Second, if I'm not wrong, why isn't the view I'm pushing onto the stack showing up? I'm guessing it's because I'm not hooking the navigation controller up to the window correctly, but I'm not sure how to do that.
Thanks!!!
Elisabeth
So I think I have the answer to my question. Which is, you must set up the navigation controller as the root view controller for the AppDelegate window in order to use it, otherwise, the window doesn't know about it. My WSViewController is still the root view controller for the navigation controller. And then to get rid of the navigation bar, you can hide it.
Here's the updated code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[WSViewController alloc] initWithNibName:#"WSViewController" bundle:nil];
// doesn't work!
//self.window.rootViewController = self.viewController;
self.navController = [[UINavigationController alloc]
initWithRootViewController: self.viewController];
// do this instead
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
To hide the nav bar in the views, in each view where you want it hidden, add the following methods:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
This is working great so far!

Show View Controller Modally on First Startup

I'd like to have a view with instructions on how to use my app show up on the first time the app is opened. I have handled the problem of the only showing this view on the first startup using NSUserDefaults, but I am having trouble getting the view to display the way I want it to modally, not as the rootViewController. Here is my AppDelegate.m code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
BOOL hasShownStartup = [[NSUserDefaults standardUserDefaults] boolForKey:kAppHasShownStartupScreen];
if (!hasShownStartup) {
[self showInstructions];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)showInstructions
{
NSLog(#"showing instructions");
InstructionsViewController *howToView = [[InstructionsViewController alloc] initWithNibName:#"InstructionsViewController"
bundle:nil];
self.instructionsViewController = howToView;
[howToView release];
UINavigationController *howtoNavController = [[UINavigationController alloc]
initWithRootViewController:self.instructionsViewController];
self.instructionsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.viewController presentModalViewController:howtoNavController animated:NO];
[howtoNavController release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAppHasShownStartupScreen];
}
I want my rootViewController to remain self.viewController. I want to be able to click 'Done' on the instructionsViewController nav bar to transition back to the rootViewController. Executing the code as written never shows the instructions view. The only way I can see instructionsViewController is if I change the line [self.viewController presentModalViewController:howtoNavController animated:NO]; to self.window.rootViewController = self.instructionsViewController; but this is obviously not what i want (unless I could modally transition back to viewController).
Hopefully I've made it clear enough what I am trying to accomplish. Thanks in advance.
Try moving [self showInstructions]; to applicationDidBecomeActive: instead.
Try putting the [self showInstructions] after [self.window makeKeyAndVisible]:
[self.window makeKeyAndVisible];
if (!hasShownStartup) {
[self showInstructions];
}

Starting an App up with a different XIB

I want to start my app up with a different Xib. How would I do this?
Thanks
If you're using interface builder:
Under SupportingFiles, in -info.plist, look for a key named "Main nib file base name". Change that to the XIB you want it to load first
You can also take that entry out of the plist altogether an in main.m give it your appDelegate's name:
int retVal = UIApplicationMain(argc, argv, nil, #"HelloViewAppDelegate");
Then in your appDelegate, you can manually load your first view controller based on your code and logic. Personally, I like this better because it's much clearer to - here's my delegate and code to load it. It doesn't have all the bindings in IB I need to remember.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
CGRect windowBounds = screenBounds;
windowBounds.origin.y = 0.0;
// init window
[self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];
// init view controller
_mainViewController = [[MainViewController alloc] init];
[[self window] addSubview:[_mainViewController view]];
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Answering your comment below. You pasted this invalid code:
// init view controller
ViewController = [[ViewController alloc] init];
[[self window] addSubview:[ViewController view]];
That's not valid. you need an instance variable name. By referring to it as "ViewController" your attempting to call class member variables. If your class is called ViewController then it should be:
// notice the instance myviewController is of type ViewController
ViewController *myViewController = [[ViewController alloc] init];
// notice calling view against instance (myViewController)
[[self window] addSubview:[myViewController view]];
At this point, if it's not compiling you need to edit your question and paste your main.m and appDelegate exactly as is into the question.
The MainWindow.xib file is just a shell that provides the application's UIWindow. While you can put a UIViewController in MainWindow.xib, you can also just have an unconnected UIViewController outlet on your app delegate and pick which nib to load at runtime in your application:didFinishLaunchingWithOptions: method, e.g.:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
if (case1)
self.viewController = [[[MyViewController1 alloc] initWithNibName:nil bundle:nil] autorelease];
else if (case 2)
self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
else
self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[window addSubview:self.viewController.view];
[window makeKeyAndVisible];
return YES;
}

How to show UINavigationController on view

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
MainPageDialog *overviewViewController = [[MainPageDialog alloc] initWithNibName:#"MainPage" bundle:nil];
UINavigationController *nav_obj = [[UINavigationController alloc] initWithRootViewController:overviewViewController ];
[self.viewController presentModalViewController:nav_obj animated:YES];
[overviewViewController release];
[self.window makeKeyAndVisible];
return YES;
}
This code shows the blue bar of navigation controller, but no buttons on it.It seems like to be that the UINavigationController allocated as empty.
Who knows what problems is?
UPD:Archive http://www.mediafire.com/?lbjjvl6fcue2q18
Please help me, I'm new in objective-c
You need to create the button for it, for example:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:launcherView action:#selector(endEditing)];
self.navigationItem.leftBarButtonItem = doneButton;
[doneButton release];
The correct way to use a UINavigationController is to push view controllers on to it. That way they will be stacked and the navigation bar will be populated with a back button when it is case (i.e., when you can actually go back to a previous controller). You control the label that appears in the "back" button by defining the title of the controllers you push.
The technique shown in another answer (setting explicitly the button) is useful with defining the right button, if you ever need one.
You could try with this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainPageDialog *overviewViewController = [[MainPageDialog alloc] initWithNibName:#"MainPage" bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc] init];
[navigation pushViewController:overviewViewController animated:NO];
[overviewViewController release];
[window addSubview:[navigation view]];
[self.window makeKeyAndVisible];
return YES;
}
Instead of doing:
UINavigationController* navigation = [[UINavigationController alloc] init];
[navigation pushViewController:overviewViewController animated:NO];
you could also use initWithRootController, but to display the general case of how you push a view controller I preferred this one.
Notice that since you are pushing just a root controller, you should see no back button at the moment, but if you push a second view controller, then it will appear.
EDIT: I gave a look at your project. Summary of what you should try and do:
objects you need in your NIB: File's Owner (UIApplication), First Responder, FBFun App Delegate (iVkAppDelegate), Window (UIWindow); remove the rest;
File's owner delegate outlet is FBFun App Delegate;
FBFun App Delegate window outlet is Window.
With this simple setup (more or less what you have), use this code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController* navigation = [[UINavigationController alloc] init];
//-- MainPageDialog *overviewViewController = [[MainPageDialog alloc] initWithNibName:#"MainPage" bundle:nil];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = #"First";
[navigation pushViewController:overviewViewController animated:NO];
iVkViewController *overviewViewController2 = [[iVkViewController alloc] init];
overviewViewController2.title = #"Second";
[navigation pushViewController:overviewViewController2 animated:NO];
[overviewViewController release];
[window addSubview:[navigation view]];
[self.window makeKeyAndVisible];
return YES;
}
In the code above, as you notice, I instantiated twice your iVkViewController just to have a second controller to push onto the navigator.
Please, delete your existing app from the simulator, and the run this in order to see that the navigation bar is correctly created and you can go back from the second controller to the first one.
I removed usage of MainPageDialog, because the MainPage nib has many problems.
But I hope this skeleton is sufficient for you to go forward with your development.
You had missed the line as you are not adding view to window.Add this line in your code
[window addSubview:nav_obj.view];