iOS Development: How to Load a Different XIB on Start - ios

I was wondering how I could load a different XIB when my application initially loads. Right now when my application loads I just get a black screen because there is no MainWindow.xib. Ideally I want to load a XIB that has a UINavigationController inside of it. Does anyone know how to do this? Thanks in advance!

In applicationDidFinishLaunching you may think to something like
UINavigationController* navigationController = [[UINavigationController alloc] init];
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"First" bundle:nil];
[ navigationController pushViewController:controller animated:NO];
[controller release];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
Of course, it actually depends on what you are planning to do...

Related

Push a ViewController not through Storyboard while working with Storyboard

I searched the whole Internet for my question but obviously was to dumb to find anything.
In my app I'm working with a Storyboard and most of my ViewController are loaded in this style (I'm working with a navigation controller):
MyViewControllerClass *viewController = [storyboard instantiateViewControllerWithIdentifier:#"myViewController"];
[self.navigationController pushViewController:viewController animated:YES];
And now my question: Is it possible to push a VC in code without using storyboard for it? Like in the g'old times, but this does not work for me anymore:
MyViewControllerClass *viewController = [[MyViewControllerClass alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
Please help me, I'm going crazy.
Thanks in advance.
Yes you can do it without using storyboard just you need to add navigation controller in app delegate like this, after that it will work fine
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController=navigationController;
[self.window makeKeyAndVisible];
return YES;
}
You can create xib file and push it. You can't push without visual.
ViewControllernew* vv = (ViewControllernew*)[[[NSBundle mainBundle] loadNibNamed:#"ViewControllernew" owner:self options:nil] objectAtIndex:0];
[self.navigationController pushViewController:vv animated:YES];
Can confirm it do work.
Push will act this way if viewController is nil. Does your alloc init creates actually an instance?

Segueing a view without Storyboard

I'm trying to figure out how to preform transitions between different views just with code, without storyboard. For example, on a button press.
- (IBAction)nextClass
{
// insert transition between views
}
I know this is possible, I was just wondering what the actual code would be to make this happen.
Not 100% sure if I am understanding your question correctly, but I think you can just say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
The name "ViewController" after initWithNibName should be the name of the .xib file that contains the UI for ViewController. Let me know if this is what you are looking for!
EDIT:
Mungbeans brings up a good point. If you are using a navigation controller, you should say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

Sidemenu with tabBarControllers in Storyboard

Hi Fellow iOS Developers, I am a newbie developing a project with 5 tab Views and on the first and second tabs I have slide out menus using Container views from example code by Michael Frederick on his GitHub page Project Link: https://github.com/mikefrederick/MFSideMenu. He is using a nib (.xib) files though I am using Storyboard to achieve the same and struck with defining the container and child views. can kindly some one advice how to modify the below code to accommodate in my storyboard.
the original code in the AppDelegate.m is
- (DemoViewController *)demoController {
return [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
}
- (UINavigationController *)navigationController {
return [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
[self navigationController], nil]];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarController
leftMenuViewController:leftSideMenuController
rightMenuViewController:rightSideMenuController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
#end
how to modify the code to accommodate the container parent view and child views ?
where should i instantiate the code for the parent and child of the 2nd tab view ? in AppDelegate or the View Controller ?
If any other Details are required leave a comment please. Any Help Will be greatly appreciated. thanks in Advance.
I don't know if you still need this, but i had the exactly same problem today, too. What you need to do is:
remove the both methods over your app Delegate
put this in your app Delegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YOUR_STORYBOARD" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"THE_IDENTITY_OF_YOUR_SIDEMENU"];
UITabBarController *centerViewController = [storyboard instantiateViewControllerWithIdentifier:#"IDENTITY_OF_YOUR_TABBARCONTROLLER"];
[container setCenterViewController:centerViewController];
[container setLeftMenuViewController:leftSideMenuViewController]; //for the right Side, its the same way...
[container setPanMode:MFSideMenuPanModeNone]; //remove this line, if you need the pan mode
return YES;
In your Storyboard you have to put a ViewController as a subclass from "MFSideMenuContainerViewController". Mark this View as the "Initial View Controller" in the Attribute Inspector. Now use a Segue from your new Initial View Controller and let it "push" to your TabBarController. To avoid a Warning rename the Segue.
After you have done this, you can add a UIBarButtonItem to every View, you like to add the SideMenu. In the Action Method of this UIBarButtomItem you only need to do this:
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
finally make sure you have a UIViewController or a UITableViewController, that is your "SideMenu" and set the right Storyboard ID.
if you are still need help, comment this...
and sorry for my english :)
You can use https://github.com/ozcanakbulut/VoovilSideMenu. It's easy to embed in a tabBarController. It uses Storyboard and Arc.

UINavigationController shows but without back button

In my delegate class I have this -
BluenibViewController *mvc = [[BluenibViewController alloc] init];
UINavigationController *unvc = [[UINavigationController alloc] init];
[unvc pushViewController:mvc animated:NO];
[mvc release];
[self.window addSubview:unvc.view];
[self.window makeKeyAndVisible];
return YES;
and in my BluenibViewController I have this method -
-(IBAction) BookingsViewController:(id)sender
{
bookingsViewController1 = [[BookingsViewController alloc]
initWithNibName:#"BookingsViewController"
bundle:nil];
UINavigationController *navController = self.navigationController;
[navController pushViewController:bookingsViewController1 animated:YES];
self.title = #"Bookings";
[self.window makeKeyAndVisible];
[self.view addSubview:bookingsViewController1.view];
[navController release];
[bookingsViewController1 release];
}
Ob click of this method I am able to go to the next view but there is no back button on the navigation bar.
Please point me to the silly mistake I am doing.
I see quite a few errors here. Lets see if I can't be of some service.
First, I see that you're overreleasing your navigation controller [navController release];
Second, you should only have to make the window key/visible once in your entire project. You set your navController as the rootViewController for your window, make it key/visible, and then you should never have to do anything with your window again. Don't think it's breaking anything, but you should remove all calls to make key/visible beyond the first.
In the end, pushing a new view controller should look like this:
[self.navigationController pushViewController:[[[BookingsViewController alloc] init] autorelease] animated:YES];
You shouldn't need anything else to get what you're looking for.
You should not add unvc.view as a subview of self.window. You should just assign unvc as self.window.rootViewController:
self.window.rootViewController = unvc;
and you should not add bookingsViewController1.view as a subview of self.view (in your BookingsViewController: method). The navigation controller will take care of getting bookingsViewController1.view onto the screen after you have pushed it.
self.navigationItem.hidesBackButton =YES;
Follow this code . Hope it helps ....
it comes a little bit late but in iOS 8 you can do:
[self.navigationController pushViewController:secondViewController animated:YES];

adding an UINavigationController to a UIControllerView

i was searching this whole afternoon in hope of solution but i didn't manage to find anything helping me solve the problem.
The problem is, i have no intention of using UINavigationController until a button gets taped, and after that i would like to present view controller containing a navigationcontroller, or reverse, modally. in my search i came across this solution:
MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:#"MyExample" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:navController animated:YES];
which i then used it in my code and found out it is not working, to make sure i started a practice project from viewbased project templet and added this to the first view controller
- (IBAction) buttonGotPreseed{
testino *testController = [[testino alloc] initWithNibName:#"testino" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:testController];
testController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:testController animated:YES];
[nav release];
[testController release];
}
didn't work also, im not seeing the navigation bar up there
any help would be greatly appreciated
You're pushing the root view controller, not the navigation controller itself. Line 5 should be:
[self presentModalViewController:nav animated:YES];

Resources