Back button close modalview - ios

I'm working on an iPad application, with 3 UIViewController's:
The first one contains a UITableView. On "didSelectRowAtIndexPath" it opens a modal with the second ViewController, like that :
NavRapportViewController *nav=[[NavRapportViewController alloc]initWithRootViewController:secondViewController];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[XAppDelegate.window.rootViewController presentViewController:nav animated:YES completion:nil];
In this second view there is a button to open the third view :
GalleryLayout *layout = [[GalleryLayout alloc] init];
GalleryViewController *gallery = [[GalleryViewController alloc] initWithCollectionViewLayout:layout];
[gallery setLegID:monLeg.legId];
gallery.delegate = self;
[self.navigationController pushViewController:gallery animated:YES];
Last, in the third view there is a back button. When I press the back button I supposed to return on the second view. But in practice, the second view appear during less than one second and the modal dismiss itself.
I've try to override the backButton action like that :
-(void)backPressed:(id)sender {
[self.navigationController popToViewController:self.delegate animated:YES];
}
But the result is the same.
Do you know why the modal close itself, and how should I do to keep it open ?

The second view controller is the root view controller of navigation controller. you can just pop to root view controller.
-(void)backPressed:(id)sender {
//[self.navigationController popToRootViewControllerAnimated:YES];
//OR
[self.navigationController popViewControllerAnimated:YES];
}

Related

Navigation Controller presented modally is empty 2nd time

I am trying to present a UITableViewController modally on one of my view controllers which is further embedded in a UITabBarController. The code I am using is -
ContactTableViewController *contactsVC=[self.storyboard instantiateViewControllerWithIdentifier:#"contact"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactsVC];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
contactsVC.myDelegate = self;
[self.navigationController presentViewController:navController animated:YES completion:nil];
The view is presented just fine the first time I do it but when I navigate to this view again in my app, the view being presented is completely blank.. No navigation bar buttons, no title, nothing..
Can't figure out a reason... Any help is very much appreciated!
UPDATE:
HomeViewController *vc = [[HomeViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
This what I do to navigate to that view again while unhiding the tab bar. Home View is the view that presents the Contacts Table View modally.

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

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

Dismiss a nib an pop to a view controller in a tab bar controller

My app it's embedded in a tab bar controller, but I'm using a nib in which I'm doing some operation.
At the end of this operation I've to pop to a view controller in my tab bar controller. I tried to use this code (suggested by an another user on here):
[self.tabBarController setSelectedIndex:<#(NSUInteger)#>];
[self.navigationController popToRootViewControllerAnimated:NO];
but it doesn't do anything. I guessed i should use this instructions:
[self dismissViewControllerAnimated:NO completion:nil];
but I've to put something in the completion, I don't know how to do that, can you help me?
Update:
I will show you my storyboard so you can understand what I'm trying to explain:
also in "Product View Controller", when I press on a button (I design the button by code) it shows me this file XIB:
In this view there's a button (usually it's hidden), when I press on this button it will add the product I'm looking to a remote cart. After the adding this product to the remote cart, I will pop to the "Carriage View Controller". When I pop to the "Carriage View Controller" I should pass them some data of my cart.
So my app has this tab view:
Home View Controller: the main view controller, loaded when the app is started
Category View Controller: it's a table view controller in which I'm displaying the category of products in my store
Carriage View Controller: a controller in which I will display the data of my cart
Info View Controller: a controller in which I display information about the app
From the "Category View Controller" with a segue I pop a View Controller in which I display by a custom view the products. When I press on one of this products it calls a the xib file I posted before file in which there are the details of the product selected before.
I hope you understand better what I'm trying to do.
CODE:
Here's the code to display the xib I posted:
- (void)collectionView:(PSCollectionView *)collectionView didSelectCell:(PSCollectionViewCell *)cell atIndex:(NSInteger)index {
int productID = (int)index;
productID = productID + 1;
if (IS_IPHONE_5) {
ProductScannedViewController *productScannedVC = [[ProductScannedViewController alloc]initWithNibName:#"ProductScannedViewControllerRetina4" bundle:nil];
productScannedVC.idProductScanned = [NSString stringWithFormat:#"%d", productID];
[productScannedVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:productScannedVC animated:YES completion:nil];
} else {
ProductScannedViewController *productScannedVC = [[ProductScannedViewController alloc]initWithNibName:#"ProductScannedViewController" bundle:nil];
productScannedVC.idProductScanned = [NSString stringWithFormat:#"%d", productID];
[productScannedVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:productScannedVC animated:YES completion:nil];
}
}
[self dismissViewControllerAnimated:YES completion:^{
// completion code here
}];
You can only use the popToRootViewControllerAnimated method if the view controller is part of that navigation controller's navigation stack.
If you are presenting the view controller like this
TestViewController * vc = [[TestViewController alloc] init];
[self presentViewController:vc animated:YES completion:^{
}];
And want to do the tab navigation inside I suggest injecting the tab bar as a weak property
TestViewController * vc = [[TestViewController alloc] init];
[vc setTabController:self.tabBarController];
[self presentViewController:vc animated:YES completion:^{
}];
Inside the TestViewController where you want to do the navigation:
[self dismissViewControllerAnimated:YES completion:^{
[tabController setSelectedIndex:0];
}];
I tested this code.

Go to specific tab bar views

I have 6 view controllers when my app starts. It's like an image gallery. When a user pushes for example the button on the third view, he/she should gets to the third view in the tab bar.
I use this code to launch the view controllers on the top of the tab bar controller:
- (void)viewDidAppear:(BOOL)animated {
static BOOL first = YES;
if (first) {
UIViewController *popup = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[self presentViewController:popup animated:NO completion:nil];
first = NO;
}
}
By using this code to dismiss this new view, I'm just coming to the specific view, but not my tab-bar page...
-(IBAction)dismissView {
TabBarPage3 *screen = [[ TabBarPage3 alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
Please help me with this!
Thanks
This is the code for change view on TabBar
[((UITabBarController *)(self.parentViewController))setSelectedIndex:index];
Sample Project of UITabbar

Resources