How to call a View Controller from not related View Controller programmatically? - ios

as you see on the image, on the normal case I click the button1 to call the second view and then I click the button2 to call the last view, and in a particular scenario I want to click the button1 to call the last view.
I added this on the methode viewDidLoad:
if(condition)
[self performSegueWithIdentifier:#"thirdview" sender:self];
but it is not working, any help please.
thank you.

You need to add a segue from ViewController1 to Viewcontroller3.
But if you do this with many viewControllers then it becomes messy, I call it the spaguetti segues.
The alternative is to present that viewController modally.
Example:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
ViewController3 *viewController =
[storyboard instantiateViewControllerWithIdentifier:#"viewController"];
[self presentViewController:viewController animated:YES completion:nil];
(make sure you give ViewController3 a storyboardId)

Related

How to open new view controller after click button?

I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.

presentViewController not working as I'd like

I have my presentViewController code set up like this:
ProductViewController *vc = [[ProductViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
It indeed presents the vc controller, but I'm unable to tap anything on it, it's like a frozen screen. Is it because there is still a view on top of it? Or I shouldn't be using alloc] init?
ProductViewController is an existing view controller (it's actually the root view controller), so basically I'd just like the presentViewController to just go back to the root view controller.
Edit:
I have a ShippingViewController where the user enters name/shipping details.
Then they tap on "Checkout" button that presents the PaymentViewController (which has a navigation bar, all other View Controllers don't).
On the PaymentViewController, the user enters their credit card details and tap the "Pay" button.
If the payment is successful, I wish for ProductViewController (the initial/root view controller) to be presented. Currently if I am just calling dismissViewController, it presents ShippingViewController.
This seemed to take care of my problem:
NSString *storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"id"];
[self presentViewController:vc animated:YES completion:nil];

Initiating a segue from uiview or uivewcontroller that is not on storyboard

How can I initiate segue from a custom UIView or custom UIViewController that is not on Storyboard? - they are created programmatically inside a parent UIViewController.
Although the destination UIViewController is on the storyboard.
Then just do it the old fashioned way. Instantiate it and present it modally or just push it if you are in a navigation controller. Hope this helps.
EDIT:
You can talk about segue only if it is in the storyboard. If Your source View Controller is not in it, you just present the next one as I said. You can instantiate your destination view controller from the storyboard:
MyViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self presentViewController:controller animated:YES];
From this point you will be "back in the storyboard" so you can perform segues in the destination View Controller.
First give storyboard id. Select the view controller than write a identifier(Storyboard ID)
Secondly, just open your .m file then write belove codes.
UIStoryboard * st = [UIStoryboard storyboardWithName:#"nameOfStoryboard" bundle:nil];
MyViewController *viewController = [st instantiateViewControllerWithIdentifier:#"YourStoryboardID"];
[self.navigationController pushViewController:viewController animated:YES];
Last line of code. Might also be
[self presentModalViewController:viewController animated:YES];

how to transit to a view controller

I made a view controller A in storyboard and linked with other view controller. Now I have another view controller B and I want to write a method that can transit from B to A. How can I do this?
There is a segue to controller A in storyboard.
And controller B is loaded from another view I created programmatically so I can't set a segue in storyboard.
Since you are using storyboard. This is more accurate, try this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"YOUR VIEW CONTROLLER"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
But be sure to name your view's identifier otherwise it will crash and wont work.
Then when you want to dimiss the view:
[self dismissModalViewControllerAnimated:YES];

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