Balabama
Balabama

Reputation: 65

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 }

Upvotes: 4

Views: 18381

Answers (4)

pandu
pandu

Reputation: 47

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.

Upvotes: 1

Akash Raghani
Akash Raghani

Reputation: 557

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.

Upvotes: 1

Sanket_B
Sanket_B

Reputation: 745

- (IBAction)login:(id)sender {
 ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
 [self presentViewController:vc animated:YES completion:nil]; }

Upvotes: 1

Premal Khetani
Premal Khetani

Reputation: 3195

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

Upvotes: 11

Related Questions