Struggling with Navigation Controller basics - ios

I've been pulling my hair out trying to get a basic NavigationController working that will allow me to switch back and forth between views easily. I feel like I'm making progress, but I'm clearly missing something critical. I now have my template app pushing views, but only by adding the initWithNibName pointing to the target NIB. Attempting to add any functionality to these secondary views causes the app to crash with a SIGABRT error. I can't imagine this is right.. If I simply have a plain NIB, the switch works fine. The only thing I've added to the secondViewcontroller is a label and a button to populate the label with some garbage text. Yet the instant I hit the switch button to shift push this view i get the SIGABRT. I'd like to be able to put functionality within the different view controllers. I feel like i'm so close, but tis is so aggravating. Can anyone point out where i've gone wrong?
#import "mainViewController.h"
#implementation mainViewController
-(void)switchView {
UIViewController *secondViewController = [[UIViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
secondViewController.title = #"My First View";
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}
-(void)switchViewTwo {
UIViewController *thirdViewController = [[UIViewController alloc] initWithNibName:#"thirdViewController" bundle:nil];
thirdViewController.title = #"My second View";
thirdViewController.view.backgroundColor = [UIColor redColor];
[self.navigationController pushViewController:thirdViewController animated:YES];
[thirdViewController release];
}

Instead of
UIViewController *secondViewController = [[UIViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
Put this:
MySecondViewController *secondViewController = [[MySecondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
My MySecondViewController should be the name of your UIViewController, also check that the XIB's name is really called secondViewController. Finally:
Go to your XIB
Select the File's Owner file.
Select the 3rd tab in the inspector.
Check the name of the class. It should be MySecondViewController instead of UIViewController.

Related

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

How to connect UIPopoverViewController in StoryBoard

I am trying to implement UIPopoverController. I have seen some sample code but still confused on how to connect the popover view controller to storyboard. Help please.
if (!patientPopover) {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
MyViewController * addPacientController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
UINavigationController * myNavController = [[UINavigationController alloc] initWithRootViewController:MyViewController];
patientPopover = [[UIPopoverController alloc] initWithContentViewController:myNavController];
patientPopover.delegate = self; // optional
}
[patientPopover presentPopoverFromBarButtonItem:yourInstanceBarButtonItemOrMethodArgumentAttachedToTheBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
It's good to have your popover in an instance variable so that you avoid allocating it each time you press the button. I am using here presenting the popover from a UIBarButtonItem, but you can also present it from any view, like :
[patientPopover presentPopoverFromRect:yourView.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You don't need to visually see the actual popover in the storyboard or nib file, because it is only a container. You just need to see what goes inside it, and that is the view controller, and obviously, you can do that.

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.

how to add uinavigationcontroller as a second view not as rootviewcontroller

i know how to add uinavigationcontroller as root view and also how to push view in uinavigationcontroller.
What i am confused in the first view which is a root view is a simple view with button when this button is tapped it will show second view.
I want this second view as uinavigationview.
any ideas how to do it, i tried to search for the solution but didn't get any nor any similar question is asked before.
basically i am trying to work without interface builder to learn things in depth.
assuming you are presenting the 2nd view controller modally
instead of
UIViewController *vc = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:vc animated:YES];
do:
UIViewController *vc = [[[UIViewController alloc] init] autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:nc animated:YES];

How to create view hierarchy programmatically?

I usually create my view hierarchy's in IB but this time I need to do it in my code. Basically I already have my custom view but I want it to be contained inside a UINavigationController. So how can I do that?
If you wish to nest it in a Navigation controller you should use :
UIViewController * myViewController = [[GameController alloc] init];
myViewController.view = yourCustumeView;//if you are trying to add a UIView
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:myViewController];
[self.navigationController pushViewController:navigationController animated:YES];
[navigationController release];
[myViewController release];
Good luck
EDIT
add this code (before the release):
navigationController.navigationItem.leftBarButtonItem=nil;//change it to rightBarButtonItem if the button is on the right.
If you want to create several ViewControllers then you can allocate them in code and then in order to show them just push it like this:
RegistrationViewController * regView= [[RegistrationViewController alloc] init];
[self.navigationController pushViewController:regView animated:YES];

Resources