ios 如何在单击按钮时从一个视图控制器导航到另一个视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10309238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:03:30  来源:igfitidea点击:

How to navigate from one view controller to another view controller on button click?

iosuiviewcontrollernavigationuibutton

提问by user1355217

I am new to iOS Application development, please help me how can I go from one view controllerto another view controlleron button click?

我是 iOS 应用程序开发的新手,请帮助我如何在单击按钮时从一个view controller转到另一个view controller

回答by sKhan

Follow the below step,let the button selector is

按照以下步骤,让按钮选择器是

[button addTarget:select action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];and implement the selector as

[button addTarget:select action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];并将选择器实现为

-(void)buttonClick{
UIViewController *controler = [[UIViewController alloc] init];
[self.navigationController pushViewController:controler animated:YES];}

and also make sure viewController has NavigationController embedded within it and replace UIViewController with the Controller you wish to push.

并确保 viewController 中嵌入了 NavigationController 并将 UIViewController 替换为您希望推送的控制器。

回答by Mohammad Kamran Usmani

Use this code in your Objective-Cfunction for navigation -

在您的Objective-C功能中使用此代码进行导航 -

DashboardViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DashboardView"];
[dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:dvc animated:YES completion:nil];

回答by Mohammad Kamran Usmani

Try this:

尝试这个:

nextViewController *obj =[[nextViewController alloc]initWithNibName:@"nextViewController" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];

回答by rishi

You can use any of approach -

您可以使用任何方法 -

  1. pushViewController: animated: - To Push the view on navigation stack

  2. presentModalViewController:nc animated: - To present the view modally.

  1. pushViewController:动画: - 在导航堆栈上推送视图

  2. presentModalViewController:nc 动画: - 以模态方式呈现视图。

回答by Nitin

YourSecondViewcontroller *temp = [[YourSecondViewcontroller alloc]initWithNibName:@"YourSecondViewcontroller" bundle:nil];
[self.navigationController pushViewController:temp animated:YES];

//or

//或者

[self presentModalViewController:temp animated:YES];

Visit this reference for tutorial and working demo code

Visit this reference for tutorial and working demo code

Hope, this will help you..enjoy

希望,这会帮助你..享受

回答by k.shree

//SAViewController will be your destiation view

// import SAViewController.h file in your current view

//SAViewController 将是你的目的地视图

// 在当前视图中导入 SAViewController.h 文件

SAViewController *admin = [[SAViewController alloc]initWithNibName:@"SAViewController" bundle:nil];
[self presentModalViewController:admin animated:YES];
[admin release];

回答by Diptee Parmar

Try this code:

试试这个代码:

- (IBAction)btnJoin:(id)sender {

   SecondViewController *ViewController2 = [self.storyboardinstantiateViewControllerWithIdentifier:@"SecondViewController"];
   [self.navigationController pushViewController: ViewController2 animated:YES];

}