go from one UIViewController to another without using navigation (segue) iOS 6 - ios

In my program I have UIViewController which has two buttons, one is "signIN" and other is "register". When "Sign IN" button is pressed, it should go to next viewController without using segue and when "register" button is pressed, it should move to next UIViewController by using segue.

Depending on the way you wish to present your view controller you should choose between:
Pushing the new view controller onto the current Navigation controller
[self.navigationController pushViewController:someOtherViewController animated:YES];
Present the new view controller modally
[self presentModalViewController:someOtherViewController animated:YES completion:nil];
Manually adding the new view controller onto the current view controller:
[self addChildViewController:childController];
childController.view.frame = view.bounds;
[view addSubview:childController.view];
[childController didMoveToParentViewController:self];
// To remove:
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

Use pushViewController:
- (void)didPressButton:(UIButton *)sender
{
[self.navigationController pushViewController:nextViewController animated:YES];
}

[[self navigationController] pushViewController:yourPushedViewController animated:YES];

Related

how to hide navigation controller number 1 instead navigation controller number 2 inside navigation controller number 1

2 Controllers
PageviewController
1)
- (void)applicationDidFinishLaunching:(UIApplication *)application
[self.window setRootViewController:navigationController];
On bottom toolbar button click in view pushing second UINavigationController:
[self.navigationController pushViewController:cnController animated:YES];
Loading of UIPageViewController in UINNavigationController:
#interface SwipeBetweenViewControllers : UINavigationController <UIPageViewControllerDelegate,UIPageViewControllerDataSource,UIScrollViewDelegate>
On bottom toolbar button click in view:
SampleViewController *viewController = [[SampleViewController alloc] initWithStyle:UITableViewStyleGrouped];
viewController.model = settingsModel;
viewController.navigationItem.title = #"Settings";
[viewController willMoveToParentViewController:self];
// [[self navigationController] setNavigationBarHidden:YES animated:YES];
[self.view addSubview:viewController.view];
[self addChildViewController: viewController];
[viewController didMoveToParentViewController:self];
[self.navigationController pushViewController:viewController animated:YES];
It shows 2 navigation controllers. I need to hide the top one when I issue following:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
It hides UIPageviewcontroller navigation and go back to original view. I also tried:
NSMutableArray *allControllers = [self.navigationController.viewControllers mutableCopy];
[allControllers removeObjectAtIndex:allControllers.count -1];
and also
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[SampleViewController class]])
{
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
}
How to hide specific original navigation controller?
Thanks for all your help.
I was able to solve this.
Actually, I was pushing viewController to top one #1 navigationcontroller, whereas it was to pushed into bottom one #2 navigationcontroller.
And wrapping #2 navigationcontroller in a containerUIviewcontroller set right navigation.
Thanks

push view controller after dismissing presented view controller

I have this navigation stack
RootVC ---> VC1 --> (presenting)-> ModalVC
and I have VC2 (not in navigation stack).
When presenting ModalVC, I want to click on button in my ModalVC to dismiss ModalVC, then push VC2 into the navigation stack after VC1 at one click. It should look like this:
RootVC ---> VC1 ---> VC2
I tried a lot of methods to make it, but pushing event fire only, when I return to my RootVC.
I tried to make it with delegates:
In ModalVC on click:
[self dismissViewControllerAnimated:YES completion:^{
if ([self.delegate respondsToSelector:#selector(dismissAndPush:)]) {
[self.delegate performSelector:#selector(dismissAndPush:) withObject:VC2];
}
}];
In VC1:
- (void)dismissAndPush:(UIViewController *)vc {
[self.navigationController pushViewController:vc animated:NO];
}
Please help to understand this behavior. Where is my mistake?
From Apple Documentation:
The presenting view controller is responsible for dismissing the view
controller it presented.
So, VC1 should be dismissing the ModalVC, try to do this
ModalVC on click:
if ([self.delegate respondsToSelector:#selector(dismissAndPush:)]) {
[self.delegate performSelector:#selector(dismissAndPush:) withObject:VC2];
}
In VC1:
- (void)dismissAndPush:(UIViewController *)vc {
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController pushViewController:vc animated:NO];
}];
}
Error has been in other. If Im right understand: some animations before dismissing presented view controller are blocking animations in navigation stack. I solved this problem with 2 ways:
1) deleteting or set right animations before dismissing
2) use setViewControllers in navigation controller (I select it)
- (void)dismissAndPush:(UIViewController *)vc {
[self dismissViewControllerAnimated:NO completion:^{
NSMutableArray *mutableControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
NSArray *controllers = [mutableControllers arrayByAddingObject:vc];
[self.navigationController setViewControllers:controllers animated:NO];
}];
}

Pop view controller after dismiss

I have a navigation controller A on which i push the view controller B. From B i present modally the view controller C. I need to dismiss C and pop B at the same time. I would like to that in sequence, keeping the dismiss animation first and then the pop animation from B to A. I tried without success this code:
[self dismissViewControllerAnimated:YES completion:^{
[self.presentingViewController.navigationController popViewControllerAnimated:YES];
}];
Any suggestion on how can i achieve this?
If you are writing in C viewcontoller then :
UIViewController *pvc = self.presentingViewController;
UINavigationController *navController = [pvc isKindOfClass:[UINavigationController class]] ? (UINavigationController *)pvc : pvc.navigationController;
[self dismissViewControllerAnimated:YES completion:^{
[navController popViewControllerAnimated:YES];
}];
or if in B view controller
[self.presentedViewController dismissViewControllerAnimated:YES completion:^{
[self.navigationController popViewControllerAnimated:YES];
}];
I had tried popping two times in succession before but not dismissing one and popping another one. You can try what I did and see if it works for you.
In Subview B:
- (void)subViewCController:(SubViewCController *)controller didSelectID:(NSNumber *)theID
{
// do something with theID...
// for my case, I popped
// [self.navigationController popViewControllerAnimated:YES];
// for your case
[self dismissViewControllerAnimated:YES completion:nil];
// Somehow, adding a small delay works for me and the animation is just nice
[self performSelector:#selector(backToSubViewA) withObject:nil afterDelay:0.6];
}
- (void)backToSubViewA
{
[self.navigationController popViewControllerAnimated:YES];
}
Are you using storyboards and segues? If so, you can use unwind segue:
In your first view controller (the one you want to jump back to, not the one you're returning from), create an unwind segue action:
- (IBAction)gotoMainMenu:(UIStoryboardSegue *)segue
{
// if you need to do anything when you return to the main menu, do it here
}
Then in storyboard, you can create a segue from the "dismiss" button to the exit outlet icon () in the bar above the scene, and you'll see main menu listed there.

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.

presentModalViewController doesn't work

-(void)backAction:(id)sender
{
SecondViewController *viewCtrl = [[SecondViewController alloc] init];
[self.presentingViewController dismissModalViewControllerAnimated:NO];
[self.presentingViewController presentModalViewController:viewCtrl animated:YES];
}
I want to dismissModalViewControllerAnimated the current controller,then present a new controller. But it only dismissModalViewControllerAnimated:NO.
why?
self.presentingViewController will be nil after you dismiss yourself in the first line. So all you have to do is get a direct reference to whatever that controller is before you do the dismissal. In the following example, the presenting view controller is of the ViewController class. You will have to use what ever class is your actual presenting view controller.
-(void)backAction:(id)sender {
ViewController *presenter = (ViewController *)self.presentingViewController;
SecondViewController *viewCtrl = [[SecondViewController alloc] init];
[presenter dismissModalViewControllerAnimated:NO];
[presenter presentModalViewController:viewCtrl animated:YES];
}
In this line, you are already dismissing the view controller
[self.presentingViewController dismissModalViewControllerAnimated:NO];
Then in the next line you are trying to preset a modal view using the view controller that was already dismissed.
[self.presentingViewController presentModalViewController:viewCtrl animated:YES];
Instead try presenting the modal view using a the parent view controller (if you have any) of both these views. You can also fire a notification to your parent view controller after dismissing the first view so that you can launch the second modal view from the parent view controller.
Maybe you can try this:
[self.presentingViewController dismissViewControllerAnimated:NO completion:^{
[self.presentingViewController presentModalViewController:viewCtrl animated:YES];
}]

Resources