How to navigate From one view controller to other - ios

I have problem in navigating from one view controller to other.
what I'm doing is If suppose there are 3 view controllers A,B,C then from A I'm pushing to B.
But in B view did load I'm hitting one api and if status code is not 200 I'm pushing to C.
But my problem is only navigation title of C is appearing and every thing is B's view.
Here is code:
NSLog(#"~~~~~ Status code: %d", [urlResponse statusCode]);
NSLog(#"Error: %#", [NSHTTPURLResponse localizedStringForStatusCode: urlResponse.statusCode]);
if([urlResponse statusCode]!=200)
{
TermsAndConditions * view = [self.storyboard instantiateViewControllerWithIdentifier:#"TermsAndConditions"];
self.navigationController.navigationBar.topItem.title = #"";
[self.navigationController pushViewController:view animated:YES];
}

Just drag from a button or any other object to another viewController in stroyboard. it is the simple way.
and the another method is
Import The viewController example: #include "SecondViewController"
then define it in .m file where u want to take the navigation
SecondViewController *tempVC =[[SecondViewController alloc]
initWithNibName:#"homeViewController" bundle:nil];
[self.navigationController pushViewController:tempVC animated:YES];
but here pushViewController does not carry the navigation properties if u need navigation properties in SecondViewController write like this
SecondViewController *tempVC =[[SecondViewController alloc]
initWithNibName:#"homeViewController" bundle:nil];
[self.navigationController presentedViewController:tempVC animated:YES]
it works for your definitely.

Related

Xcode Can't Call/Dismiss viewcontroller from PopUp

This is going to take some explaining... So I'll do it in order:
I have a navigation controller where the rootViewController is called TipsCollectionViewController.
I have a UserViewController that's loaded as a popup:
UserViewController * userView = [[UserViewController alloc] initWithNibName:#"UserView" bundle:[NSBundle mainBundle]];
[userView setUEmail:email];
[self presentPopupViewController:userView animationType:MJPopupViewAnimationSlideTopBottom];
I then have another popup that loads on top of THAT popup:
Place *p = [placeArray objectAtIndex:indexPath.row];
DetailPlaceViewController *pvc = [[DetailPlaceViewController alloc] init];
[pvc setPlace:p];
NSLog(#"%#", p.PName);
[self presentPopupViewController:pvc animationType:MJPopupViewAnimationSlideTopBottom];
Now there's a reason I've done this: the AppDelegate features a Navigation controller and previously I loaded the UserView like this:
[self.navigationController presentViewController:userView animated:YES completion:nil];
But this meant that the UserView would load on the iPhone but not on the iPad for some odd reason. But when I switched it to the popup view it worked fine.
So now I load both the UserView and the DetailPlaceView in a popup... but now it CLOSES on the iPad but not on the iPhone.
Here's the code for closing the detail view:
- (void) didTapBackButton:(id)sender {
NSLog(#"View Controller Number: %lu", (unsigned long)self.navigationController.viewControllers.count);
if(self.navigationController.viewControllers.count > 1) {
[self.navigationController popViewControllerAnimated:YES];
NSArray *stack = self.navigationController.viewControllers;
TipCollectionViewController *tipsVC = stack[stack.count-1];
[tipsVC.collectionView reloadData];
}
I know there's a better way to handle this whole thing... but what should I be doing differently?
UPDATE
If I switch it back to:
[self.navigationController pushViewController:userview animated:YES];
...for the UserView on the iPhone and:
[self.navigationController pushViewController:pvc animated:YES];
...for the view that loads after that, then the UserView will load... but the next viewcontroller (the DetailPlaceViewController) won't load. I think that's my main problem. I could probably dismiss the second view controller at that point if I could get it to load. Any ideas?
[self presentPopupViewController:pvc animationType:MJPopupViewAnimationSlideTopBottom];
if you are using this,presentPopupViewController is used to display like modal view which cant be dismissed without any custom cancellation action.
To use popOverViewController method, use pushViewController. Then that one you can dismiss by this technique.
Ultimately all I had to do was switch:
[self.navigationController pushViewController:pvc animated:YES];
to:
[self presentViewController:pvc animated:YES];
And it works. It opens and closes the view and everything works as it should.
I'm getting a warning on the DetailView once you click an item:
Presenting view controllers on detached view controllers is discouraged <UserViewController: 0x16e02020>
So I'll wait a couple of days to accept my answer. Otherwise, despite the warning, it seems to be working.

How to Dismiss Model View Controller before push?

Currently my scenario like this ,
View Controller(VC1) --> Push Segue --> View Controller(VC2)
| ^
| Model Segue |
| | Push Segue
|-----> View Controller(VC3) ------|
Now in VC3 when i push in to VC2 i want to dismiss VC3 view Controller and
on after VC2 back button it should be VC1's back view controller i want to pop VC3
What i want is i want to remove VC3 when i push the VC2 from VC3 when we back then it VC1 should appear
Check Example
I have done like this can anyone help me out
View Controller (VC3) :
- (void)callWebservice {
[self dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"showVC2" sender:self];
}];
}
Thanks in advance
I think you can implement in that way:
when you at screen 3 you want push screen 2. You will dismiss screen 3 and call a delegate back screen 1.
And from screen1 you will push view to screen2.
You can see my demo for my idea: Demo Pushview Swift
DemoPushViewObj
I think you are looking for https://stackoverflow.com/a/21415225/1320305 if you are using segue, to do this manually try following:
you can try this
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
// Pop this controller and replace with another
[navController popViewControllerAnimated:NO];//not to see pop
[navController pushViewController:aViewController animated:YES];
//to see push or u can change it to not to see.
OR try this
UIViewController *newVC = [[UIViewController alloc] init]; // Replace the current view controller
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeLastObject];
[viewControllers addObject:newVC];
[[self navigationController] setViewControllers:viewControllers animated:YES];

How to navigate to some other View Controller when Navigation Controller back button is pressed?

I have a viewController in which, on the click of back button, I need to go to a specific viewController and call its method.So I created a barButton and added as a BACK button in navigation bar.When its selector is called I can see only see a black screen, nothing else.
Here how I am doing it.
In viewDidLoad
//Back Button in navigation Bar.
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(navigationBackButtonClicked:)];
self.navigationItem.leftBarButtonItem=newBackButton;
The selector below executes and shows a black screen.
-(void)navigationBackButtonClicked:(UIBarButtonItem *)sender {
SharedManager *sharedManagerObject = [SharedManager sharedInstance];
NSString *source = sharedManagerObject.toCityString;
NSString *destination = sharedManagerObject.fromCityString;
NSString *dateStr = sharedManagerObject.dateSelected_String;
BusListViewController *buslist_VC = [self.storyboard instantiateViewControllerWithIdentifier:#"BusListViewController"];
[buslist_VC getBusListForSource:source destination:destination date:dateStr];
[self.navigationController popToViewController:buslist_VC animated:YES];
}
You need to add your buslist_VC to the view hierarchy of your navigation controller before using [popToViewController:animated:]. This is used to display some viewcontrollers already in the stack of your navigation Controller.
Either way what you're asking might be a weird behaviour for your user but you can use :
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 1] animated:NO];
[self.navigationController pushViewController:buslist_VC animated:NO];
You cann't back to a new ViewController, you can push a new ViewController, in your code change this:
[self.navigationController pushViewController:buslist_VC animated:YES];
But keep in mind, you are putting this new viewController inside the other (where you are created a newBackButton), and the default back button come back to this. If you want to come back to the root use this:
[self.navigationController popToRootViewControllerAnimated:YES];
Or, now you can come back to any viewController on the navigation stack, this array:
NSArray *navStacks = self.navigationController.viewControllers;
Using popToViewController: be sure is in the stack.
You should use
NSArray *navStacks = self.navigationController.viewControllers;
select needed view controller and do
[self.navigationController popToViewController:selectedVC animated:YES];

Pushing multiple UIViewControllers in NavigationController

I have 6 subclassed UIViewControllers connected with push segues with identifiers.
They go A>B>C>D>E>F. I am unable to find a way how to implement button in the controller A which would automatically stack all controllers up to controller F and display F controller. Stacking should be done in UINavigationController instance, not through(void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated, because if I use setViewControllers method, then I lose segue identifiers. Ty!
You should be able to do it with pushViewController:animated:, like this:
// This method belongs to view controller A class
-(void)pushToF {
// I am assuming that A is already in the navigation controller
UINavigationController *nav = self.navigationController;
UIViewController *b =[self.storyboard instantiateViewControllerWithIdentifier:#"B"];
[nav pushViewController:b animated:NO];
UIViewController *c =[self.storyboard instantiateViewControllerWithIdentifier:#"C"];
[nav pushViewController:c animated:NO];
... // And so on, until F
UIViewController *f =[self.storyboard instantiateViewControllerWithIdentifier:#"F"];
// You can push the last one with animation, so that end users would see it
[nav pushViewController:f animated:YES];
}

not able to move to another view controller

I'm trying to move from 1st view controller -> 2nd view controller. However, it seems nothing is happening.
It is mainly from a sign-in page, so I don't need to get back to that page once I have a successful login. I'm trying right now from a button just to check.
I have 2 classes.
signInPage
optionsScreen
I need to go from 1 > 2
Inside signInPage.m
#import "optionsScreen.h"
- (IBAction)moveToNext:(id)sender {
optionsScreen *aSecondPageController =
[[optionsScreen alloc]
initWithNibName:#"optionsScreen"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
// [aSecondPageController release];
}
Screenshot of story board
https://drive.google.com/file/d/0B1y7ao4cGAYKaU0yRlVxcVg2bzA/edit?usp=sharing
Solution:
- (IBAction)moveToNext:(id)sender {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"optionsScreen"];
[self presentViewController:vc animated:YES completion:nil];
}
In storyboard add navigation view controller. As root view controller - perhaps by default it is table view controller, so remove it - set up 1st view controller. Adding navigation controller you can push to navigation stack as many view controllers as you want.
I think what you're trying to do is present the view controller modally, not push it onto the stack (which doesn't exist). Instead you should just do this. This will not show a back button by default so you'll have to add one manually:
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self presentViewController:aSecondPageController animated:YES completion:nil];
To go back, in your optionsScreen view controller, you'll need some sort of back button right? So you can do this in your viewDidLoad:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:);
Then you'll have to create a new method called backPressed:
-(void)backPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Edit: it turns out you were trying to do what I explained above. I will leave the navigation controller bit for future reference.
If you are trying to use a navigation controller to push your options, which will give you a back button by default, then first, in your storyboard, click your sign in view controller, then at the menu bar at the top go to Editor > Embed In... > Navigation Controller. Then in your view controller switch method, you can do what you were trying before
optionsScreen *aSecondPageController = [[optionsScreen alloc] initWithNibName:#"optionsScreen" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];

Resources