UINavigationController pushViewController does not work - ios

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.)

Related

Navigation View Controller from Subview

I have been working on this problem for a while and thought I would ask for some help. I have 3 view-controllers: 1 Navigation Controller, 1 Main controller and 1 detail view controller.
Within the main view controller, I have a series of subviews with buttons. Due to the class structure, however, I am unable to directly call self.storyboard to get the current storyboard object.
I have tried 2 different methods, a variety of ways, and am still unsuccessful. I posted my methods below and described what is and what is not happening in each segment. The overall goal is to present a child view controller (the detail view) by tapping a button in a subview, of which does not have access to the parent storyboard directly.
Method 1
//Instantiate the new view controller
ProfileViewViewController *tempViewToShow = [del.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"profile"];
// Pass data into the new view controller
tempViewToShow.thisUser = self.postUser;
// Output a simple log to ensure both were created
NSLog(#"Temp User Name: %#, Profile Desc: %#", [tempViewToShow.thisUser getFullName], tempViewToShow.description);
// Using the AppDelegate for the RootViewController, present the detail view
[UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:tempViewToShow animated:YES completion:NULL];
Issues
The issue with this series is that the detail view does not carry the navigation controller (since it is not mentioned), however, this way still shows a full View Controller
Method 2
...
// Use the Delegate and the navigation controller to present the new view controller
[UIApplication.sharedApplication.delegate.window.rootViewController.navigationController presentViewController:tempViewToShow animated:YES completion:NULL];
Issues
Does not display anything
Method 3
// Use the recommended 'pushViewController' for the navigation controller to carry over
[UIApplication.sharedApplication.delegate.window.rootViewController.navigationController pushViewController:tempViewToShow animated:NO];
Issues
Does not display anything
En toto, how would I make this work? What lines would I modify and how? Thanks!
You can solve this issue like this:
ProfileViewViewController *tempViewToShow = [del.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"profile"];
UINavigationController *naviController = [[UINavigationController alloc] tempViewToShow];
And then do this :
[UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:naviController animated:YES completion:NULL];
You can create instance of storyboard from storyboard name.once you have correct storyboard instance, get NavigationController from Its identifier, and detailviewController from its identifier. Push detailviewcontroller on Navigationviewcontroller.
get storyboard-- replace name of your storyboard in "MainSToryboard"
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:[NSBundle mainBundle]];
get instance of Navigationcontroller - replace identifier:
UINavigationController *navController =(UINavigationController *)
[storyboard instantiateViewControllerWithIdentifier:#"navcontroller"];
get detailviewconrtoller :
UIViewController *detailvc=
[storyboard instantiateViewControllerWithIdentifier:#"profile"];
Push detail on current navigationcontroller:
[navController pushViewController:detailvc animated:YES];
I found an alternate solution. The cause was because the incorrect view controller was being called by
UIApplication.sharedApplication.delegate.window.rootViewController.*
The workaround is:
In the primary view controller class, I passed the displayed viewcontroller into the delegate class. Then, from the child class I wanted to call, I referenced that view controller, and navigation controller, and it worked just fine. My final code is below:
// Create the detail View Controller
ProfileViewViewController *tempViewToShow = [del.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"profile"];
// Set the user variable in the detail view controller
tempViewToShow.thisUser = self.postUser;
// Push the view controller into the navigation controller
// Note that del.currentNav comes from this code:
/*
* In this class, create the delegate reference
* AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate]
*
* In the Delegate class, get the set the current navigation controller {let currentVC : UIViewController = passedInVC}
* self.currentNav = currentVC.navigationController;
*/
[del.currentNav pushViewController:tempViewToShow animated:YES];

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

Title and nav bar disappearing when launching view controller with code [duplicate]

This question already has an answer here:
presentViewController does not show Tab Bar or Navigation Bar
(1 answer)
Closed 7 years ago.
I am using following code to launch a view controller modally. This view controller is not being created from scratch. Rather, I created and styled it in storyboard, gave it a name, embedded it in a navigation controller so it has a navigation bar and created cancel and done buttons, gave it a title and the code below launches it.
The problem is that while most of the features of the screen show up such as labels and images, the title and navigation bar that you can see in the storyboard disappear. Has anyone come across this issue and have a fix for it?
My code to launch VC created in storyboard.
- (void) editView:(id) sender
{
NSLog(#"launch button pressed");
UIStoryboard *storyBoard = self.storyboard;
IDEditVC *editVC =
[storyBoard instantiateViewControllerWithIdentifier:#"editvc"];
editVC.item = _item;
[self presentModalViewController:editVC animated:YES];
}
I think what's happening is that since you are directly instantiating the view controller by name it's getting just that from the storyboard rather than embedding it in the nav controller.
Try this:
In your storyboard, make the Navigation Controller the initial/root View Controller for the storyboard
Instead of instantiating by name, use UIStoryboard's instantiateInitialViewController method.
EDIT:
Based on your comments below, you might want to try this:
- (void) editView:(id) sender
{
NSLog(#"launch button pressed");
UIStoryboard *storyBoard = self.storyboard;
IDEditVC *editVC =
[storyBoard instantiateViewControllerWithIdentifier:#"editvc"];
editVC.item = _item;
UINavigationController *nav = [UINavigationController alloc] initWithRootViewController: IDEditVC];
// Do whatever setup you want to here for your title bar, etc
[self presentModalViewController:nav animated:YES];
}
Try to use PushViewController instead of instantiate:
How to push viewcontroller ( view controller )?
Good luck

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

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

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.

Resources