How to open new view controller after click button

2020-07-22 17:41发布

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 }

4条回答
相关推荐>>
2楼-- · 2020-07-22 18:20

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.

查看更多
太酷不给撩
3楼-- · 2020-07-22 18:25

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.

查看更多
Juvenile、少年°
4楼-- · 2020-07-22 18:27
- (IBAction)login:(id)sender {
 ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
 [self presentViewController:vc animated:YES completion:nil]; }
查看更多
Explosion°爆炸
5楼-- · 2020-07-22 18:34

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];
查看更多
登录 后发表回答