How to create view hierarchy programmatically? - ios

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

Related

how to add New UIViewCOntroller Over UiView

I have added subview in a UIViewController and I want a new UIViewController Should be loaded on click of button.
Challenge is I am not able to use button in new UIViewController.
Code which I have write:
DetailsOfDayViewController *aViewController =[[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
aViewController.dateParsing = dateparsing;
[self addSubview:aViewController.view];
This will fix your issue. This is happening because your view controller is being deallocated when you don't have any strong pointers on it.
DetailsOfDayViewController *aViewController =[[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
aViewController.dateParsing = dateparsing;
[self addChildViewController:aViewController];
[self addSubview:aViewController.view];
Another fix will be connecting this view controller to a property:
#property(nonatomic,strong) DetailsOfDayViewController *aViewController;
And change your code like this:
self.aViewController = [[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
self.aViewController.dateParsing = dateparsing;
[self addSubview:self.aViewController.view];
For showing another controller you can following options
1. Add SubView
newViewControllerObj.view.frame = oldViewControllerObj(self).view.frame
[oldViewControllerObj(self).view.newViewControllerObj.view];
2. Present
[oldViewControllerObj(self) presentViewController: newViewControllerObj animated:YES completion:nil];
3. Push using Navigation (For this you already have to set navigation controller as root controller)
[oldViewControllerObj(self).navigationController pushViewController:newViewControllerObj animated:YES];

iOS - how to set a navigation bar item in view controller?

I'm new to Objective-C and I want to add a UINavigationBar on my CatrgoryBIDViewController. I have proceed UIButton in InstructionBIDViewController.m file that should navigate to CatrgoryBIDViewController. Here is the function code:
- (IBAction)proceed:(id)sender {
viewControllercat =
[[CatrgoryBIDViewController alloc]
initWithNibName:#"CatrgoryBIDViewController"
bundle:nil];
UINavigationController *nav =
[[UINavigationController alloc]
initWithRootViewController:self.viewControllercat];
//[self.navigationController pushViewController:viewControllercat animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
But it is not setting the UINavigationBar.
You should read the documentation here to understand the way a NavigationController is working. For your case:
If your current ViewController (where your proceed-method is implemented) has a NavigationController (is child of it), you can push another ViewController onto the stack of that NavigationController with
[self.navigationController pushViewController:viewControllercat animated:YES];
In this case you do not need to initialize another NavigationController, but in your CatrgoryBIDViewController in viewWillAppear you need to make the NavigationBar visible if it was not before already with
[self.navigationController setNavigationBarHidden:NO animated:YES];
If your current ViewController does not have a NavigationController, you can not push another ViewController on top of it and can not show the NavigationBar of it (although you can create your own NavigationBar and add it to the View of the ViewController, but without the usual Navigation-behaviour embedded).
If you open your ViewController programmatically (e. g. from the AppDelegate) you are correct to do so by your call:
viewControllercat = [[CatrgoryBIDViewController alloc] initWithNibName:#"CatrgoryBIDViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewControllercat];
My apologies - After having reread your question, I am going to tweak my answer some.
-(IBAction)proceed:(id)sender
{
viewControllercat = [[CatrgoryBIDViewController alloc] init];
UINavigationController * nc =
[[UINavigationController alloc] initWithRootViewController:vc];
// We now need to display your detail view but cannot push it.
// So display modally
[self presentViewController:nc animated:YES completion:Nil];
}
The above should result in your DetailViewController - CategoryBIDViewController being displayed on top of your InstructionBIDViewController and it should have a UINavigationController in it.

getting two navigation bar when clicking tabbar more button

Having one main navigation controller in whole application and also one main tabbarcontroller , problem is that when i click om 'more' tab of tabbarcontroller at that time it's showing two navigation bars
to solve this problem i tried to hide my main navigation controllers navigationbar using following code :
self.tabbar.navigationController.navigationBarHidden =YES;
but doing this gives me unexpected result in the form of half navigationbar with half black background.
if any one knows the solution then please help me.
thanks in advance.
make viewController With separate UINavigationController,
put this code in Appdelegate
ViewController *a = [[ViewController alloc] initWithNibName:#"a" bundle:nil];
ViewController *b= [[CreateMeetingViewController alloc] initWithNibName:#"b" bundle:nil];
ViewController *c = [[SettingsViewController alloc] initWithNibName:#"c" bundle:nil];
UINavigationController *nav_1 = [[UINavigationController alloc] initWithRootViewController:a];
UINavigationController *nav_2 = [[UINavigationController alloc] initWithRootViewController:b];
UINavigationController *nav_3 = [[UINavigationController alloc] initWithRootViewController:c];
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate = self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:nav_1,nav_2,nav_3,nil]];
MainTabBar.view.frame=self.view.frame;
[self.view addSubview:MainTabBar.view];
You can check your .Xib you check the option Top Bar this should be 'None' in the identity inspector.
Write in viewWillAppear, i hope it will be helpful for you
[self.navigationController setNavigationBarHidden:YES animated:YES];

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

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