Ios 以编程方式从一个ViewController转到另一个ViewController
我以编程的方式设置了这个按钮,我一直在试图弄清楚,当按下它时,如何切换到另一个Ios 以编程方式从一个ViewController转到另一个ViewController,ios,swift,xcode6,Ios,Swift,Xcode6,我以编程的方式设置了这个按钮,我一直在试图弄清楚,当按下它时,如何切换到另一个ViewController。我一直在尝试查找实现这一点所需的代码类型。我好像找不到 import UIKit class ViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() let image = UIImage(named: "Camera.png") as
ViewController
。我一直在尝试查找实现这一点所需的代码类型。我好像找不到
import UIKit
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "Camera.png") as UIImage!
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(self.view.frame.width / 2 - 30, self.view.frame.size.height - 70, 70, 70)
button.setImage(image, forState: .Normal)
self.navigationController?.view.addSubview(button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这就是我如何进行设置的。请有人告诉我需要使用哪种类型的代码,或者给我举个例子。谢谢只要一句话
self.navigationController!.pushViewController(self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! UIViewController, animated: true)
首先,通过以下方式向按钮添加操作:
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
然后添加辅助对象方法:
func buttonTapAction(sender:UIButton!){
//this code will navigate to next view when you press button.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(vc, animated: true)
}
完整代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "s.png") as UIImage!
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(self.view.frame.width / 2 - 30, self.view.frame.size.height - 70, 70, 70)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonTapAction(sender:UIButton!){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}
查看示例了解更多信息。创建从ViewController到TargetViewController的手动序列 现在,向按钮添加一个操作
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
在行动中做到这一点
func buttonAction(sender:AnyObject)
{
self.performSegueWithIdentifier("manualSegue", sender: nil)
}
希望这对您有所帮助。首先为按钮创建一个操作,如下所示
- (IBAction)btnInvite:(id)sender {
}
然后导入要继续的viewcontroller,如
#import "YourViewController.h"
最后,把下面的代码放到按钮操作方法中,不管我们在上面创建了什么
- (IBAction)btnInvite:(id)sender {
YourViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"YourViewController Identifier Name"];
[self.navigationController pushViewController:viewController animated:YES];}
希望对你有用
let VC = self.storyboard?.instantiateViewController(withIdentifier:"yourViewController") as! yourViewController
self.navigationController?.pushViewController(VC, animated:true)
仅使用您的Viewcontroller名称更改您的Viewcontroller您需要如何从一个VC跳转到另一个VC的代码示例吗?您好,谢谢您的帮助,这很有效,但我对self.navigationController有问题吗?因为我设置按钮的方式是将其覆盖在选项卡视图上。请尝试以下操作:
self.presentViewController(vc,动画:true,完成:nil)
请为您的答案添加一些解释,以便将来的用户更好地理解您的代码。