以编程方式从一个 ViewController 转到另一个

Programmatically going from one ViewController to another

我已经以编程方式设置了这个按钮,并且我一直在尝试弄清楚如何在按下它时转到另一个 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?.instantiateViewC‌​ontrollerWithIdentifier("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)
    }
}

查看 THIS 样本了解更多信息。

创建从您的 ViewController 到目标 ViewController 的手动转场。

现在,给按钮添加一个动作。

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