Push UIViewController to UINavigationController from a popover - ios

I have a problem when trying to push a new view controller onto an existing navigation controller.
The thing I'm trying is to make a UIPopoverController appear when pushing a navigation UIBarButtonItem, and from that "dropdown" select a menu point which will push the associated view controller onto the "main" navigation controller.
I've tried the following, which gives a modal. But I want the view pushed.
If selecting push instead of modal the result is as following.
I've also tried making a custom UITableViewController (on the popover) from which I've tried the following code:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:#"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:#"students"];
if (indexPath.row == 0) {
[dash pushViewController:students animated:YES];
// [[dash navigationController] presentViewController:students animated:YES completion:nil];
}
NSLog(#"%#", [dash title]);
NSLog(#"index = %i", indexPath.row);
}
Is there a way to do what I am trying to accomplish?

This code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:#"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:#"students"];
is creating too many new instances. You should be using the existing storyboard (self. storyboard) and the existing navigation controller. The navigation controller needs to be passed to the table view controller (which you should use because the storyboard doesn't have the required information). We'll call this originatingNavigationController, a new #property on the table view controller.
When the segue triggers to show the popover, set the navigation controller reference into the destination view controller (the table view).
Then, in the didSelectRowAtIndexPath: method you just instantiate the students VC and push it:
UIViewController *students = [self.storyboard instantiateViewControllerWithIdentifier:#"students"];
[self.originatingNavigationController pushViewController:students animated:YES];
and then the table view controller should dismiss itself (its popover).

Related

Open Storyboard on top of previous view

I have created storyboard which I want to open on top of another view or in childView so that when I close or destroy this view of storyboard the earlier view on which the storyboard is opened remains the same.
When I run the following code:-
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[[UIApplication sharedApplication].keyWindow setRootViewController:detailViewController];
First, the storyboard opens but I don't know whether it opens on top of previous view or it destroys the previous view & then open.
Second, the functions needed on that storyboard runs automatically which is as I want but how these things are working.
If anyone can help me understand the above code and its working.
NOTE: I cannot call the earlier view again in same state because of some reason.
Thanks in Advance!!
Here you are setting root view controller
it will not keep your back screen as it is what you want
If you want to keep current screen and show other screen on that
you have two approaches
1) Present ViewController
2) Push View Controller
1) Present ViewController
for this you can present your screen on top of other screen which is visible
for example
- (IBAction)btnNextTapped:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[self presentViewController:detailViewController animated:true completion:nil]
}
2) Push View Controller
For that you need NavigationController and need to push your ViewController from current visible screen
i.e
[self.navigationController pushViewController:vc animated:true];
EDIT
as per discussion you need to find current top view controller then you should present it
Add this method below your method
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
And Replace this method with code
- (void)goToNewPage:(CDVInvokedUrlCommand*)command
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[[self topMostController] presentViewController:detailViewController animated:true completion:nil];
}

How do I segue from a NIB subview to a viewcontroller in the storyboard

I have a combination of a storyboard and NIB viewcontrollers how can I segue from one to the other
This is the Scenario:
UITabBarController(storyboard)|- UINavigationController (subclassed in storyboard) -> [UIViewController (NIB)] -> UIViewController (in Storyboard)
You'll have to give the UIViewController in the Storyboard a 'Storyboard ID' (in the Identity inspector tab). Then instantiate that ViewController in your NIB ViewController's code like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"YourViewControllerId"];
[self.navigationController pushViewController:vc animated:YES];
In your UINavigationController subclass you need to instantiate your view controller with its view in a NIB(let's call it NIBViewController) and set that view controller as the rootViewController with the method setViewControllers:animated:. Do it in awakeFromNib like this:
- (void)awakeFromNib
{
[super awakeFromNib];
NIBViewController *viewController = [[NIBViewController] init];// It will look for the appropriate XIB as long as you did not change its name and it's in the main bundle, otherwise use initWithNibName:bundle:
[self setViewControllers:#[viewController] animated:NO];
}
Then, to go from the NIBViewController to other view controller contained in the storyboard just set an identifier in the storyboard for that view controller, instantiate it and push it or present it, as you want.
It is important to mark that in your NIBViewController instance self.storyboard will be nil because that view controller is not contained in a storyboard, so you need to instantiate a new storyboard(solution A**) or get the storyboard from the navigation controller(solution B):
// Solution A
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// Solution B
UIStoryboard *storyboard = [self.navigationController storyboard];
UIViewController *storyboardViewController = [storyboard instantiateViewControllerWithIdentifier:#"viewControllerId"];
[self.navigationController pushViewController:storyboardViewController animated:YES];
** Please note that this is creating a new instance of the storyboard, so better not to try tricky things and avoid segues to previous view controllers contained in the storyboard or you will find that those view controllers are not the same ones... It's the first solution that came to my mind, but definitely you should go for the solution B.

iOS - navigation controller from storyboard

I have embedded my view controller in a navigation controller on my storyboard, then added a table view which I configured from code. I'm trying to make it, when I click a row in the table, it should change the view and putting the subview into the stack, however I can't access the navigation controller.
[self.navigationController pushViewController:disclosureView animated:YES];
When this code launches, it gives the error: " NavController[990:c07] Application tried to push a nil view controller on target . "
I hope that you didn't get call the ViewController properly :
if(indexPath.row == 0)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *disclosureView = [storyboard instantiateViewControllerWithIdentifier:#"disclosureView" ];
[self.navigationController pushViewController:disclosureView animated:YES];
Here is the screenShot for getting the UIViewController identifier:

segue on destination view controller not working after programmatically going to destination view controller

segues not working after going to destination view controller (which is in storyboard )programmatically from a button click of view controller (which is not in storyboard)
- (IBAction)OK:(id)sender {
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
mainViewController *controller = (mainViewController *)[myStoryboard instantiateViewControllerWithIdentifier:#"mainViewController"];
[controller setModalPresentationStyle: UIModalPresentationFormSheet];
[self presentModalViewController:controller animated:YES];
}
Okey, if i am not wrong, i think , the button click segue is using navigationcontroller , if so , watch your custom segue ,
[self presentModalViewController:controller animated:YES];
you are making an model segue, so your navigation controller will be unknown after the segue is done.

Linking a new viewcontroller to Storyboard?

There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

Resources