not able to move to another view controller - ios

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

Related

How to pop back viewController from SWRevealViewController?

In my app after pressing the login button in my loginViewController, it push to front viewController of my SWRevealViewController. In my RearViewController I have the sign out button. When I press it it should pop to the back viewController again (LoginViewController). But even though I set the button click event like this it doesnt navigate to the loginViewController.
-(void)signOutClick :(id)sender
{
[self performSelectorInBackground:#selector(moveToLoginView) withObject:nil];
}
-(void)moveToLoginView
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
But when I swipe the view it gose to the back view.I want to disable that swipe feature to the back view. And I want to go to the back view when click on the sign out button in my RearViewController. How can I do that? Please help me.
Thanks
I am not sure but instead of using
[self.navigationController popToRootViewControllerAnimated:YES];
try using,
[self.navigationController popViewControllerAnimated:YES];
And to stop swipe gesture you can use code like,
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.revealViewController.panGestureRecognizer.enabled = NO;
}
You need to reset the top view controller if you want to perform these kind of actions off of a click. E.g.
get an instance of the ECSlidingViewController
ECSlidingViewController *slidingController = self.slidingViewController;
get the view controller you wish to be on the top
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"mainViewController"];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
set the topViewController property
slidingController.topViewController = nc;
[slidingController resetTopViewAnimated:NO];

Difficulties changing between viewControllers in xcode

I have 3 different viewControllers. first one is the main page where you will see many thumbnail of images. Second controller will only open when an image is clicked. Last ViewController is the login which opens when the user is not logged in but trying to view the second controller or clicked login from the main controller. In the login controller I have a back button to go to main page. Remember the user can access login from two different ways. by clicking on the image or clicking login in the main controller directly. So the issue is the back button does work properly. When the login page is accessed from the main controller directly, then the back button works fine it takes you to the main page. However, if you click on any of the thumbnails the second controller will try to open but since your not logged in the login page will show and when you click the back button to send you to the main controller, it won't go to main instead it will reload the same page and you kind of stock in a loop you have to login or exit the app. any ideas why?
The First ViewController Identifier is set to "mainImages".
- (IBAction)backToMain:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * MI = [storyboard instantiateViewControllerWithIdentifier:#"mainImages"];
UINavigationController *navController = self.navigationController;
if (navController) {
[navController pushViewController:MI animated:NO];
}
}
I also tried this but no luck:
[self dismissViewControllerAnimated:NO completion:nil];
UIViewController * MI = [self.storyboard instantiateViewControllerWithIdentifier:#"mainImages"];
[self presentViewController:MI animated:NO completion:nil];
Try this if you are presenting as modal:
- (IBAction)backToMain:(id)sender {
[self dismissViewControllerAnimated:NO completion:^{
UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
UINavigationController *nav = (UINavigationController *)window.rootViewController;
[nav popToRootViewControllerAnimated:YES];
}];
}
If you need to "go back" from the login view to any other view (in this case 2 of them), you should try using a Modal Segue, a modal segue lose the navigation controller view stack but allow you to dismiss the login view and go back to you last view.
On the first (or thumbnail images view controller) show your new modal view as this:
UIViewController *modalViewController = [[UIViewController alloc] init];
[self presentViewController:modalViewController animated:YES completion:nil];
And then, to dismiss (on login view controller):
- (IBAction)backToMain:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

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

Call another class in IOS

I'm new on IOS platform and after study a little about how does it works, i had one doubt about how to call a new class/view and overlay the current view when a button is pressed. In android i do:
Intent intent = new Intent(a.class, b.class);
startActivity(intent);
Searching on internet, i noticed that i have to use navigation bars to do it. I start an app with tab bar controller and putted a navigation controller. I used the code below:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:#"Dicas"];
[self.navigationController pushViewController: myController animated:YES];
and the return:
There is a way to overlay the current view? I always have to use navigation bars to call another class (use bottom e upper controllers will make my app ugly)?
To hide the navigation bar:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
To hide tab bar:
yourViewController.hidesBottomBarWhenPushed = YES;
You can also call another viewController by using presentViewController function of a viewController class.
[self presentViewController: myController animated:YES completion:nil];
As you said u want to overlay your current view so not sure but You can show your view controller using model view like this
yourViewController *secView = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[secView setModalPresentationStyle:UIModalPresentationPageSheet];//u can use different UIModalPresentationStyle
[secView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.navigationController presentViewController:secView animated:YES completion:nil];
Above one will show your view controller over existing view controller, once presented you need to make provision to dismiss this modal view.
Simple way to present new class or ViewController like this..
ViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self presentViewController:view animated:YES completion:nil];

Attempt to present a viewcontroller whose view is not in the window hierarchy

I have a tab bar application. My requirement is I select the 'scan' tab to scan the qr code and navigate/jump immediatley to another 'list' tab. Both 'scan' and 'list' tab are there in the viewControllers array in didFinishLaunchingWithOptions After referring this link, i don't think i need to set the delegate as both the tabs are already present in the hierarchy.
I get this warning in the following line
if(x)
{
listViewCntrl = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
listViewCntrl.getFlag = YES;
[self presentViewController:listViewCntrl animated:YES completion:Nil]; // I get the warning here
}
If I comment out the above code and add
[self.tabBarController setSelectedIndex:1];
then I would not be able to get the subView of the listViewController (set flag to show the subview) which i need to display inside the list tab after scanning.
App crashes if I add
[self.tabBarController setSelectedViewController:listViewCntrl];
So how do I display the listView's subview after scanning?
You can try this if you use storyboard:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController * destViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:destViewController animated:YES];
You have to set an identifier for your controller in your storyboard.
ListViewController *listController = (ListViewController*)[self.tabController viewControllers][1];
listController.getFlag = 1;
[self.tabBarController setSelectedIndex:1];
The problem is that you're creating an entirely new ListViewController. You say you already have one in the tab controller - you don't need a new one.
You can't use your 3rd option because, again, the two ListViewController's are different objects (they may be of the same class, but they point to a different address).

Resources