Starting an App up with a different XIB - iphone

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

Related

App not navigating to second view

The code I am using
NSLog(#"before Navigate to second activity");
Signature *temp = [[Signature alloc]initWithNibName:#"Signature" bundle:nil];
[self.navigationController pushViewController:temp animated:YES];
NSLog(#"after Navigate to second activity");
On console,both the log statements are getting printed, but my app is not navigating me to the next View.Please correct me.
if you are not using a UINavigationController on your app you can not call pushViewController, use presentViewController:animated:completion: instead:
[self presentViewController:temp animated:YES completion:nil];
for more information check the documentation
The log messages would show anyway, so no surprise there. Add a callback to the delegate for that navigation controller (you must first set a delegate of course) for
navigationController:didShowViewController:animated:
There you can make sure that the viewController passed (make sure Signature is a ViewController instance).
i think your application is not with navigationController so in AppDelegate.m file assign rootViewController like this..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RootViewController *viewController1 = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
UINavigationController *navviewController=[[UINavigationController alloc]initWithRootViewController:viewController1];
self.window.rootViewController = navviewController;
[self.window makeKeyAndVisible];
return YES;
}
after that check its working..
CreditsViewController *viewController = [[CreditsViewController alloc] init];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
you can try with above code

Crash in the didFinishLaunchingWithOptions method

self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[self.window addSubview:self.rootViewController.view]; //App will not crash without this line
self.navigationController = [[UINavigationController alloc] initWithRootViewController: self.rootViewController];
[self.window addSubview:self.navigationController.view];
I run it in the simulator and it crash, why?
Error message:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency',
reason: 'adding a root view controller <RootViewController: 0x6871620> as a child of
view controller:<UINavigationController: 0x6a86dc0>'
still have no idea
yours logic is wrong. Either add rootViewController or add navigationController as window's subview. You can't add two viewcontrollers at the same time. Here your navigationController will overwrite your rootviewcontroller. If possible then add your rootviewcontroller into navigationController or add navigationController into rootviewcontroller
Since You are setting RootViewController nib name to nil i hope you are managing your view in RootViewController.m inside method -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil or some other way.
to fix your error message you have given, change your posted code following way
self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController: self.rootViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
You are not specifying your problem clearly,so please add error message what you got on the console.Without error message we are unable to tell where the problem occur.Any how it crashes due to nibfile name,you specifying nib file name as nil.Please specify nib file name. Try this code once.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.navigationController=[[UINavigationController alloc] initWithRootViewController:self.rootViewController];
self.window.rootViewController = self.navigationController;
// [self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I hope this code will solve your problem

AppDelegate UIWindow addSubView in different viewController

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

View in empty template

I need to create my own view with controller on empty template without help of Interface Builder. What i need to write in AppDelegate ?
XCode -> New project -> Select Window Based Template. Then Remove MainWindow.xib from resources, from info.plist remove this key "Main Nib file base name".In your main.m file
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate"); // 4th parameter is name of your AppDelegate. In your AppDelegate.h remove property/synthesize for your IBOutlet UIWindow as you are not using IB at all..
// In your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window makeKeyAndVisible];
// In your AppDelegate.h AnotherViewController* mainViewController;
mainViewController = [[AnotherViewController alloc] init];
[[mainViewController view] setFrame:[UIScreen mainScreen].applicationFrame];
[window addSubView:[mainViewController view]];
}
// In your AnotherViewController implement
- (void)loadView
{
// Lets say you have a UITableView
UITableView* tableView = [[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain] autorelease];
[self setView:tableView];
}

Loading a view controller & view hierarchy programmatically in Cocoa Touch without xib

It seems like all of the Cocoa Touch templates are set up to load a nib.
If I want to start a new project that's going to use a view controller, and load its view(hierarchy) programatically, not from a nib/xib, what are the steps to setting that up or adjusting a template.
I though all I had to do was implement -loadView, but I have trouble every time I try to do this.
It's reasonably simple to do completely programmatic user interface generation. First, you need to edit main.m to look something like the following:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
UIApplicationMain(argc, argv, nil, #"MyAppDelegate");
[pool release];
return 0;
}
where MyAppDelegate is the name of your application delegate class. This means that an instance of MyAppDelegate will be created on launch, something that is normally handled by the main Nib file for the application.
Within MyAppDelegate, implement your applicationDidFinishLaunching: method similar to the following:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (!window)
{
[self release];
return;
}
window.backgroundColor = [UIColor whiteColor];
rootController = [[MyRootViewController alloc] init];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
[window layoutSubviews];
}
where MyRootViewController is the view controller for the primary view in your window. This should initialize the main window, and add the view managed by MyRootViewController to it. rootController is kept as an instance variable within the delegate, for later reference.
This should let you programmatically generate your user interface through MyRootViewController.
UIViews themselves do not have a hierarchy, UINavigationControllers do. So init one of those, and pop a UIViewController onto it's stack. This should do it, in the most basic way possible, with no XIB files at all. You should be able to build on this.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UINavigationController *navController = [[UINavigationController alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
// set the properties of viewController here, to make it look like you want
[navController pushViewController:viewController animated:NO];
[window addSubview:navController.view];
// Don't forget memory management
[navController release];
[viewController release];
[window makeKeyAndVisible];
}