How do I push many Views from one View using UINavigationController - objective-c

I code many transition App using UINavigationController.
This is my transition image
"Main"-->"Second"-->"Third"-->"Fourth"
"Second"-->"A"-->"B"
"Second"-->"C"
"Third"-->"D"
"Main" is RootViewController.
At No2 and No3, if (["boo" isEqual:"foo"]){transit "A"} else if ("boo"...){transit "C"}.
I could make "Main" to "Forth", No.1 transition.
like this
ThirdViewController *thirdView = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdView animated:YES];
But I can't make "Second" to "A"and "B".
I thought to set UIRootViewController to "Second".
But "Second" needs to transit "A" and "C".
at "Second" like this...
SecondViewController.m
AViewController *aView = [[AViewController alloc] init];
[self.navigationController pushViewController:aView animated:YES];
How can I code?
Thank you! And sorry in poor English.
Please help me!

NSInteger index = 0;
for (UIViewController *view in self.navigationController.viewControllers) {
if([view.nibName isEqualToString:#"RootViewController"])//put any `XIB name` where u want to navigate
break;
index = index + 1;
}
[[self navigationController] popToViewController:[[self.navigationController viewControllers] objectAtIndex:index] animated:YES];

Try this code to push to a controller
DrawImgViewcontroller *ObjImageViewController = [[DrawImgViewcontroller alloc] initWithNibName:#"DrawImgViewcontroller" bundle:nil];
[self.navigationController pushViewController:ObjImageViewController animated:YES];
[ObjImageViewController release];
and use below code to go back
[self.navigationController popViewControllerAnimated:YES];
and use this to any controller you like
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:NO];//Instead of one you can use the currect index you need

Related

How to drill down and show detail uitableview with splitview without storyboard

I implemented a splitview using 2 UITableViews and a UIViewController. Showing both on the screen with their own data is working fine.
Now in my DidSelectRowForIndexPath I did the following:
DetailViewController *nextController = [[DetailViewController alloc] initWithStyle:UITableViewStylePlain];
NSMutableArray *objects;
objects = [[NSMutableArray alloc] initWithObjects:#"A", #"B", nil];
nextController.title = #"Filter";
[nextController SetArray:objects];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:nextController];
[self presentModalViewController:nc animated:YES];
//used this before [self.navigationController pushViewController:nextController animated:YES];
[FilterView deselectRowAtIndexPath:indexPath animated:YES];
Maybe you know a better method as what I did to present the nextController
The nextController is always showing up from the bottom and moving to the top. How can I accomplish this default sliding animation where the detail view comes into the view from the right side?
If you have started the app from the master/detail template while creating the project. Then it will have the navigation controller set automatically in the appDelegate didFinishLaunching method so in the didSelect methods you have to just use
`[self.navigationController pushViewController:vc animated:YES];`
instead of
[self presentModalViewController:nc animated:YES];
Here is a sample code which uses split view with navigation to and from root view.
First you need to confirm that you have UINavigationController at base then you can pushViewController:nextController
[FilterView deselectRowAtIndexPath:indexPath animated:YES];
DetailViewController *nextController = [[DetailViewController alloc] initWithStyle:UITableViewStylePlain];
NSMutableArray *objects;
objects = [[NSMutableArray alloc] initWithObjects:#"A", #"B", nil];
nextController.title = #"Filter";
[nextController SetArray:objects];
[self.navigationController pushViewController:nextController animated:YES];

NavigationBar not displaying properling when pushViewController

The problem is pretty simple to understand with pictures.
I have a UINavigationController that allow the user to switch between to views.
The first view contains a search bar and a table view like so :
The second is a basic view where information about the cell are display
When I click on the search bar, the navigation controller gets hidden and the search bar is now at the top.
Now, if I click on a cell, it goes to the second views, but the navigation bar is first hidden like below :
And then, it automatically appears like that :
I have tried a couple of things like show the navigation bar before pushing the next view controller but it is quite ugly..
Does anyone know how to have the show the navigation bar directly on the second view (like in the contact application)?
[UPDATE] : Code
AppDelegate.m (I'm talking about navigationcontroller2)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController1.managedObjectContext = [self managedObjectContext];
viewController2.managedObjectContext = [self managedObjectContext];
viewController1.viewController2 = viewController2;
UINavigationController *navigationcontroller1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[navigationcontroller1.navigationBar setTintColor:[UIColor lightGrayColor]];
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[navigationcontroller2.navigationBar setTintColor:[UIColor lightGrayColor]];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller1, navigationcontroller2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
FirstView.m
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!noResultsToDisplay) {
PinDetailsViewController *pinDetailsViewController = [[PinDetailsViewController alloc] initWithNibName:#"PinDetailsViewController" bundle:nil];
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
Pin *pin = (Pin *) managedObject;
[self.navigationItem setTitle:#"Pins"];
[self.navigationController pushViewController:pinDetailsViewController animated:YES];
[pinDetailsViewController updateWithPin:pin];
}
}
If you need anything else, just ask but I think it's all there.
Try to use this code in each viewcontroller.
- (void) viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
Before you push the new view controller, you should unhide the navigation bar:
[self.navigationController setNavigationBarHidden:NO animated:YES];
I had a similar problem with the position of my navbar. Mine was moving up behind the status bar, and I fixed the issue by manually setting the navbar frame:
-(void)adjustNavBarOrigin
{
CGRect r = self.navigationController.navigationBar.frame;
r.origin = CGPointMake(0, 20); // 20 is the height of the status bar
self.navigationController.navigationBar.frame = r;
}
I had to call this method in a number of places, including viewWillAppear: and didRotateFromInterfaceOrientation:, but it worked a treat :)
Hiding the UINavigationBar can disturb the properties sometimes. Try using the property alpha instead of hidden.

ModalViewController with embedded nav controller - Unable to dismiss

I present a modalViewController that is actually a navigation controller with one view, and a custom navigation bar. The modal view appears fine as expected, but when I attempt to remove it from view using [self dismissModalViewControllerAnimated:YES], I am hitting a "-[UINavigationController modalViewController]: message sent to deallocated instance". Can't seem to figure this out. Any ideas?
Instantiating the ModalViewController:
// Make a navigation controller and add the view inside it
MyViewController *evc=[[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
//UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:evc];
UINib *nib = [UINib nibWithNibName:#"UINavigationBarWithBackgroundImage" bundle:nil];
UINavigationController *nvc = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[nvc setViewControllers:[NSArray arrayWithObject:evc]];
evc.delegate=self;
[evc release];
[self presentModalViewController:nvc animated:YES];
[nvc release];
and trying to remove it. This is where the error comes in:
[self dismissModalViewControllerAnimated:YES];
Not sure about this, but try it anyway:
Remove
[nvc release]
and see if
[self dismissModalViewControllerAnimated:YES];
now works.
Is there a reason you are loading two seperate nibs to show this modal? You do not need to load a nib containing a navigation controller to get this working.
Try something like this:
// Make a navigation controller and add the view inside it
MyViewController *evc= [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:evc];
evc.delegate=self;
[self presentModalViewController:navController animated:YES];
[evc release];
[navController release];

Is it Possible to call PopViewControllerAnimated and PushViewController for single click event

On a buttonclick action I want to destroy the current screen at the same time I want to navigate it to home screen instead of previous screen.
I tried to do that by Putting this code but didn't work.
-(IBAction) sel_goback
{
[self.navigationController popViewControllerAnimated:YES];
Home1ViewController *HM = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[self.navigationController pushViewController:HM animated:YES];
[HM release];
}
If self is the top view controller, then by the time popViewControllerAnimated: has returned, self.navigationController has been set to nil. Try this:
-(IBAction) sel_goback
{
UINavigationController *nav = self.navigationController;
[nav popViewControllerAnimated:YES];
Home1ViewController *HM = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[nav pushViewController:HM animated:YES];
[HM release];
}
However, "home screen" sounds like something that would already be on your navigation controller's stack. Maybe what you really want is something like this:
-(IBAction) sel_goback
{
UINavigationController *nav = [self.navigationController];
while (nav.viewControllers.count > 1 && ![nav.topViewController isKindOfClass:[Home1ViewController class]])
[nav popViewControllerAnimated:YES];
if (![nav.topViewController isKindOfClass:[Home1ViewController class]]) {
Home1ViewController *home = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[nav setViewControllers:[NSArray arrayWithObject:home] animated:YES];
}
}

Close UIViewController

I'm trying to close soon after a UIViewController call another. It should be simple, but I'm not succeeding. I am using the following method:
- (IBAction)bClose:(id)sender {
iTest *test = [[iTest alloc] initWithNibName:#"iTest" bundle:[NSBundle mainBundle]];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
[test release];
[self dismissModalViewControllerAnimated:YES];
}
I believe it's not simple. I have done something similar but instead I present the second in the first, then dismiss the first when the second wants to dismiss (dismissing both at the same time). To explain what I mean better:
main -> present controller1
controller1 -> present controller2
controller2 -> dismiss controller1
I'm sure there is a better solution to it though.
Something like this:
inside first controller:
UIViewController *c1 = [[UIViewController alloc] init];
[self presentModalViewController: c1 animated:YES];
inside c1:
UIViewController *c2 = [[UIViewController alloc] init];
c2.c1 = self;
[self presentModalViewController: c2 animated:YES];
inside c2:
[c1 dismissModalViewControllerAnimated:YES];
i also had to try something similar..but ran into much of the same problem..
My solution was to initiate a timer with time 0.5 sec and then dismiss the view in the selector
Worked for me.