ios 导航控制器推送视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20742745/
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
Navigation Controller Push View Controller
提问by Ahmed Elashker
Question
题
How to navigate from one view controller to another simply using a button's touch up inside event?
如何仅使用按钮的触摸事件从一个视图控制器导航到另一个视图控制器?
More Info
更多信息
What I tried in a sample project, in steps, was:
我在示例项目中逐步尝试的是:
Create the sample single view application.
Add a new file -> Objective-C Class with XIB for user interface (ViewController2).
Add a button into ViewController.xib and control click the button to ViewController.h to create the touch up inside event.
Go to the newly made IBAction in ViewController.m and change it to this...
- (IBAction)GoToNext:(id)sender { ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; [[self navigationController] pushViewController:vc2 animated:YES]; }
创建示例单视图应用程序。
为用户界面 (ViewController2) 添加一个新文件 -> 带有 XIB 的 Objective-C 类。
在 ViewController.xib 中添加一个按钮并控制单击按钮到 ViewController.h 以创建内部事件。
转到 ViewController.m 中新建的 IBAction 并将其更改为...
- (IBAction)GoToNext:(id)sender { ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; [[self navigationController] pushViewController:vc2 animated:YES]; }
The code runs without errors and I tested the button's functionality with NSLog. However it still doesn't navigate me to the second view controller. Any help would be appreciated.
代码运行没有错误,我用 NSLog 测试了按钮的功能。但是它仍然没有将我导航到第二个视图控制器。任何帮助,将不胜感激。
回答by Anbu.Karthik
Swift3
斯威夫特3
**Push**
do like
喜欢
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController
vc.newsObj = newsObj
navigationController?.pushViewController(vc,
animated: true)
or safer
或更安全
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController {
viewController.newsObj = newsObj
if let navigator = navigationController {
navigator.pushViewController(viewController, animated: true)
}
}
present
展示
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController
vc.newsObj = newsObj
present(vc!, animated: true, completion: nil)
or safer
或更安全
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
{
vc.newsObj = newsObj
present(vc, animated: true, completion: nil)
}
//Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"
bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
//ViewController.m
- (IBAction)GoToNext:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
}
swift
迅速
//Appdelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let navigat = UINavigationController()
let vcw = ViewController(nibName: "ViewController", bundle: nil)
// Push the vcw to the navigat
navigat.pushViewController(vcw, animated: false)
// Set the window's root view controller
self.window!.rootViewController = navigat
// Present the window
self.window!.makeKeyAndVisible()
return true
}
//ViewController.swift
@IBAction func GoToNext(sender : AnyObject)
{
let ViewController2 = ViewController2(nibName: "ViewController2", bundle: nil)
self.navigationController.pushViewController(ViewController2, animated: true)
}
回答by Sandy Rawat
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"];
[self.navigationController pushViewController:viewControllerName animated:YES];
Swift 4:
斯威夫特 4:
let storyBoard = UIStoryboard(name: "storyBoardName", bundle:nil)
let memberDetailsViewController = storyBoard.instantiateViewController(withIdentifier: "viewControllerIdentiferInStoryBoard") as! MemberDetailsViewController
self.navigationController?.pushViewController(memberDetailsViewController, animated:true)
回答by Preetha
Using this code for Navigating next viewcontroller,if you are using storyboard means follow this below code,
使用此代码导航下一个视图控制器,如果您使用故事板意味着请遵循以下代码,
UIStoryboard *board;
if (!self.storyboard)
{
board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
}
else
{
board = self.storyboard;
}
ViewController *View = [board instantiateViewControllerWithIdentifier:@"yourstoryboardname"];
[self.navigationController pushViewController:View animated:YES];
回答by Harshit Goel
Use this code in your button action (Swift 3.0.1):
在您的按钮操作中使用此代码(Swift 3.0.1):
let vc = self.storyboard?.instantiateViewController(
withIdentifier: "YourSecondVCIdentifier") as! SecondVC
navigationController?.pushViewController(vc, animated: true)
回答by PREMKUMAR
For Swiftuse the below code:
对于Swift,请使用以下代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
// Create a nav/vc pair using the custom ViewController class
let nav = UINavigationController()
let vc = NextViewController(nibName: "NextViewController", bundle: nil)
// Push the vc onto the nav
nav.pushViewController(vc, animated: false)
// Set the window's root view controller
self.window!.rootViewController = nav
// Present the window
self.window!.makeKeyAndVisible()
return true
}
ViewController:
视图控制器:
@IBAction func Next(sender : AnyObject)
{
let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
self.navigationController.pushViewController(nextViewController, animated: true)
}
回答by swiftBoy
Let's Say If you want to go from ViewController A --> Bthen
让我们说如果你想从 ViewController A --> B然后
Make sure your ViewControllerA is embedded in Navigation Controller
In ViewControllerA's Button click you should have code like this.
确保您的 ViewControllerA嵌入在导航控制器中
在 ViewControllerA 的 Button 单击中,您应该有这样的代码。
@IBAction func goToViewController(_ sender: Any) {
@IBAction func goToViewController(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
- Please double check your storyboard name, and ViewControllerB's Identifier mentioned in storyboard for ViewControllerB's View
- 请仔细检查您的故事板名称,以及 ViewControllerB 视图的故事板中提到的 ViewControllerB 标识符
Look at StoryboardID = ViewControllerB
看StoryboardID = ViewControllerB
回答by Maulik Salvi
UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"];
[self.navigationController pushViewController:vc animated:YES];
回答by Barath
Swift 4 & 5
斯威夫特 4 & 5
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "yourController") as! AlgoYoViewController
navigationController?.pushViewController(vc,
animated: true)
回答by Victor Rius
This is working perfect:
这是完美的工作:
PD: Remember to import the destination VC:
PD:记得导入目标VC:
#import "DestinationVCName.h"
- (IBAction)NameOfTheAction:(id)sender
{
DestinationVCName *destinationvcname = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCName"];
[self presentViewController:destinationvcname animated:YES completion:nil];
}
回答by sellmaurer
- (void) loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
error:(NSError *)error{
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"nav"];
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"LoggedInVC"];
[nav pushViewController:vc animated:YES];
[self presentViewController:nav animated:YES completion:nil];
}
"nav" is the Storyboard ID for my navigation controller "vc" is the Storyboard ID for my first view controller connected to my navigation controller
“nav”是我的导航控制器的故事板 ID “vc”是我连接到导航控制器的第一个视图控制器的故事板 ID
-hope this helps
-希望这可以帮助