简体   繁体   English

点击按钮后如何打开新的视图控制器?

[英]How to open new view controller after click button?

I added one ViewController to my project and created one class. 我在项目中添加了一个ViewController并创建了一个类。 After I bind this class to my ViewController . 将此类绑定到ViewController

In another controller I have method: 在另一个控制器我有方法:

- (IBAction)login:(id)sender { // How here I can do redirect to controller ViewController } - (IBAction)login:(id)sender { // How here I can do redirect to controller ViewController }

There are two ways to push view controllers in the application. 有两种方法可以在应用程序中推送视图控制器。

1) by Segue 1)Segue

2) By View controller identifier 2)通过View控制器标识符

1) By Segue : 1)通过Segue:

[self performSegueWithIdentifier:"SegueIdentifier" sender:self];

2) By View controller identifier : 2)通过View控制器标识符:

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. 注意:在UIViewController中,请输入要推送到另一个视图的视图控制器名称。

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. 如果你想稍后回到当前的ViewController然后使用Navigation ViewController,或者只使用self的presentViewController(你当前的viewController) - 就像他们之前所说明的那样,不要回头。

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. 对于一个简单的执行块(演示或实践),它们完全相同,但对于项目应用程序来说,做出正确的选择是完全重要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在按钮上单击以打开uitabbaritem上的新视图控制器 - How to open new view controller on uitabbaritem on button click Swift - 单击Button后打开新视图 - Swift - Open new View after click on Button 如何通过菜单按钮打开新的视图控制器? - How to open new view controller by menu button? 是否可以单击 imageView 并打开一个新视图 Controller? - Is is possible to click an imageView and open a new View Controller? 如何从UITableViewRow按钮单击事件打开新视图? - How to open new view from UITableViewRow button click event? 单击显示指示器按钮后如何转到第二个视图控制器 - How to go to second view controller after click on disclosure indicator button 如何将详细信息视图控制器打开到新的视图控制器中? - How can open a detail view controller into a new view controller? 如何在 SwiftUI 中单击按钮打开 web 视图? - How to open web view on button click in SwiftUI? 添加显示按钮以注释并在新的视图控制器中打开信息 - add disclosure button to annotation and open information in new view controller 如何从CustomTableViewCell中的任何动态按钮单击推送新视图控制器? - How to push New View Controller From any dynamic button click which is in CustomTableViewCell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM