how can a UILongPressGestureRecognizer change the present view to another View Controller? - ios

How do I programmatically change from my 1st view controller to my 2nd view controller, which in storyboard is linked to the 1st in storyboard?
My iPad storyboard has a Tab Bar Controller as the "initial View Controller",
which links through a "Relationship Seque view controllers"
to a Navigation Controller, which links through a "root view" link, to View Controller A, which has a UIButton, which links through a 'Manual Seque push' to View Controller B.
Tab Bar Cntrl => Navigation Cntrlr ==(root view)==> View Cntrl A, UIButton ==(push)==> View Cntrl B
So, when running, with View Controller A shown, pressing its UIButton causes the view to change to View Controller B.
There's another UIButton on a UITableView cell on View Controller A. When I press and hold this button, the delegate for UILongPressGestureRecognizer fires ok, and I need View Controller B to now be the view.
How then do I change from View Controller A to View Controller B?
And with VC B open, how do I change back to VC A ?
THE DELEGATE THAT RESPONDS TO PRESS-AND-HOLD............
- (void)schedule_long_press_delegate:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateEnded) {
printf("Long press Ended ................. \n");
// Get storyboard:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad"
bundle: nil];
printf("mainStoryboard = %x \n", (int)mainStoryboard ); // !!! ax
// Get nav controller for MANAGE view:
UINavigationController *MANAGE_UINavigationController = (UINavigationController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"MANAGE_Storyboard_ID"];
printf("MANAGE_UINavigationController = %x \n", (int)MANAGE_UINavigationController ); // !!! ax
// Get MANAGE view's view controller:
SCHEDULE_UIViewController *schedule_UIViewController = [self.storyboard instantiateViewControllerWithIdentifier:
NSStringFromClass([SCHEDULE_UIViewController class])];
printf("schedule_UIViewController = %x \n", (int)schedule_UIViewController ); // !!! ax
// Change screen to MANAGE view:
[MANAGE_UINavigationController pushViewController: schedule_UIViewController animated:YES];
}
else {
printf("Long press detected ..................... \n");
}
}
STORYBOARD SETTINGS:
UITabBarController ... Storyboard ID is blank.
..links to:
UINavigationController ... Class=UINavigationController StoryboardID = "MANAGE_Storyboard_ID"
..links to:
UIViewController ... Class=acc StoryboardID is blank (INITIAL VIEW)
..links to:
UIViewController ... Class & StoryboardID = "SCHEDULE_UIViewController" (TARGET VIEW)
OUTPUT:..........
mainStoryboard = 1f8b4900
MANAGE_UINavigationController = 1e5d45f0
schedule_UIViewController 2 = 1e5d4b20

In the delegate method for the long press gesture recognizer you can push view controller B from code like this:
ViewControllerB *viewControllerB = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([ViewControllerB class])];
[self.navigationController pushViewController:viewControllerB animated:YES];
You can use a different identifier if you want; using the class name is just my preference.
Changing back to view controller A will be handled for you by the navigation controller. The user will be presented with a back button in the upper left corner.

Related

ViewControllers overlapping each other in Main.Storyboard

I've two ViewControllers in Main.Storyboard, entry point ViewController have green background, and another is red background. At the button click of first ViewController it should show second ViewController, but the first and second ViewControllers are overlapping each other and both ViewControllers Buttons are clickable.
Ist view controller (green), bingo is the UIButton
2nd view controller (red), hello is the UIButton
On the click event of the button in first view controller is :
MViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"mvc"];
[self presentViewController:mvc animated:YES completion:nil];
Try this if its a push segue :
MViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:#"mvc"];
[self.navigationController pushViewController:mvc animated:YES];
And you should take one navigation controller and assign it as initial view and make green view the root view. Delete the default root view controller comes with it.
You can write this code in the action block of bingo button
{
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let redVC = storyBoard.instantiateViewControllerWithIdentifier("**RedViewControllerSID**") as RedViewController
self.presentViewController(redVC, animated:true, completion:nil)
}
go storyboard and create Storyboard ID as RedViewControllerSID

SWRevealViewController - push next view controller storyboard

I have a sidebar menu, made with SWRevealViewController.
I want to switch from the view controller and the view with the button to the next view controller. How I do that?
I try to used the ACTION SEGUE: "push", "modal", "revealview controller" and "revealview controller push controller".
But it doesn't works.
And I try it programmatically switch to the next viewcontroller.
MY CODE:
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationcontroller pushViewController:vc2 animated:YES]
This change the ViewController, but without the view (blackscreen).
It seems ViewController2 is a ViewController in storyboard. Try the below code:
ViewController2 * vc2 = [[self storyboard] instantiateViewControllerWithIdentifier:#"ViewControllerId"];
You should give Storyboard ID for ViewController2 as ViewControllerId in your storyboard.
Please note that your current ViewController should be in a Navigationcontroller stack to call self.navigationcontroller.

UITabBar disappears after pushed to new view controller

I have an UITabBarController that has 3 buttons. The second button points to ViewController1 which is connected to another view called ViewController2. After I tap a button in ViewController2 I programmatically present ViewController1 again, that works perfect except one thing. After I "arrived" to ViewController1 the tab bar disappears.
I'm using this method to navigate back to ViewController1. (exactly I navigate to its navigation controller, but already tried with the view)
- (void)presentViewControllerAnimated:(BOOL)animated {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard" bundle:nil];
UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"destination"];
[self presentViewController:firstViewNavigationController animated:animated completion:nil];
}
I call here the first method
- (void)didTapButton:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
[self presentViewControllerAnimated:YES];
}
This method hides the tab bar in the ViewController2, I already tried without it, therefore there is no problem with it.
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}
I can't figure out why this thing happens, I think it's a fair solution, that worked well for a several times when I needed to present views. I've read it can happen with segues, but I'm doing it with code without segues.
Actually your code works right. There should not be tab bar when you present FirstViewController from SecondViewController. Because when you call instantiateViewControllerWithIdentifier its basically creates a new instance of that view controller, and of course, there is no tab bar.
The right way to go back to your first view controller is to pop SecondViewController (or dismiss it, if it presented modally). So your final code should be like this
- (void)didTapButton:(id)sender {
// If this view controller (i.e. SecondViewController) was pushed, like in your case, then
[self.navigationController popViewControllerAnimated:YES];
// If this view controller was presented modally, then
// [self dismissViewControllerAnimated:YES completion:nil];
}
And of course, your view controller hierarchy in storyboard must be like this:
-- UINavigationController -> FirstViewController -> SecondViewController
|
->UITabBarController____|
-...
-...
I've tried the same and got the same result.
My solution was simple, on the push do this :
UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"destination"];
firstViewNavigationController.hidesBottomBarWhenPushed = true; // Insert this and set it to what you want to do
[self presentViewController:firstViewNavigationController animated:animated completion:nil];
and then remove your
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}

UINavigationController pushViewController does not work

I've reviewed all the other similar problems and can't find solution.
My iPad storyboard has a Tab Bar Controller as the "initial View Controller", which links through a "Relationship Seque view controllers" to a Navigation Controller, which links through a "root view" link, to View Controller A, which has a UIButton, which links through a 'Manual Seque push' to View Controller B.
Tab Bar Cntrl => Navigation Cntrlr ==(root view)==> View Cntrl A, UIButton ==(push)==> View Cntrl B
So, when running, with View Controller A shown, pressing its UIButton needs to change to View Controller B. But nothing happens................
THE DELEGATE THAT RESPONDS TO UIBUTTON (PRESS-AND-HOLD gesture)............
- (void)schedule_long_press_delegate:(UILongPressGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateEnded) { printf("Long press Ended ................. \n");
// Get storyboard:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad"
bundle: nil];
printf("mainStoryboard = %x \n", (int)mainStoryboard ); // !!! ax
// Get nav controller for MANAGE view:
UINavigationController *MANAGE_UINavigationController = (UINavigationController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"MANAGE_Storyboard_ID"];
printf("MANAGE_UINavigationController = %x \n", (int)MANAGE_UINavigationController ); // !!! ax
// Get MANAGE view's view controller:
SCHEDULE_UIViewController *schedule_UIViewController = [self.storyboard instantiateViewControllerWithIdentifier:
NSStringFromClass([SCHEDULE_UIViewController class])];
printf("schedule_UIViewController = %x \n", (int)schedule_UIViewController ); // !!! ax
// Change screen to MANAGE view:
[MANAGE_UINavigationController pushViewController: schedule_UIViewController animated:YES];
}
else {
printf("Long press detected ..................... \n");
}
}
STORYBOARD SETTINGS:
UITabBarController ... Storyboard ID is blank. ..links to: UINavigationController ... Class=UINavigationController StoryboardID = "MANAGE_Storyboard_ID" ..links to: UIViewController ... Class=acc StoryboardID is blank (INITIAL VIEW) ..links to: UIViewController ... Class & StoryboardID = "SCHEDULE_UIViewController" (TARGET VIEW)
OUTPUT:..........
When the delegate fires, it displays the pointers, which are all set:
mainStoryboard = 1f8b4900
MANAGE_UINavigationController = 1e5d45f0
schedule_UIViewController 2 = 1e5d4b20
Is there some initialization that I'm missing?
The word "instantiate" tells you that you are creating a new instance. Therefore, when you call:
UINavigationController *MANAGE_UINavigationController = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"MANAGE_Storyboard_ID"];
You are creating a new navigation controller which is not the one that's currently part of the visible hierarchy.
Although your objects and calls may be fine, you're not interacting with the controllers that are managing the screen.
(Note: If you used code style that matched Apple standards, if would be a lot easier for other people to analyze what you've written.)

Navigating to a view controller modally and then navigation using push segue

I have 2 storyboards. I have a situation where I have to navigate from a view controller1 in storyboard 1 to a view controller2 in storyboard 2. The view controller 1 is added as a subview to a parent view controller. When I click on a row in a table view in view controller 1, I have to navigate to storyboard 2.
Initially it was not detecting a touch in the table row. I managed handling it by using a single tap gesture. Now when I click on a row, I am able to navigate to storyboard2 viewcontroller 2 modally. But after navigating ,I am not able to move anywhere on a button click in view controller 2 because it uses all push segues there.
Here is the sample code which I used to navigate.
*NSString * storyboardName = #"OppotunityStoryboard";
NSString * viewControllerID = #"AgreementMainViewController";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
AgreementMainViewController *controller = (AgreementMainViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
controller.productTransactionentity = productTransactionEntity;
[self presentViewController:controller animated:YES completion:nil];*
You have to embed ViewController 2 in a navigation controller in order for it to be able to use push segues when presented modally.

Resources