【问题标题】:Programmatically going from one ViewController to another以编程方式从一个 ViewController 转到另一个
【发布时间】:2015-09-01 06:27:54
【问题描述】:

我已经以编程方式设置了这个按钮,我一直试图弄清楚当它被按下以转到另一个 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.
}


}

这就是我设置它的方式,请有人指导我使用我需要使用的正确类型的代码或给我一个示例。谢谢

【问题讨论】:

  • 您需要如何从 1 个 VC 跳转到另一个 VC 的代码示例?

标签: ios swift xcode6


【解决方案1】:

首先以这种方式向您的按钮添加操作:

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 示例了解更多信息。

【讨论】:

  • 您好,感谢您的这项工作,但我遇到了 self.navigationController 的问题?因为我设置按钮的方式是它覆盖在选项卡视图上。
  • 试试这个:self.presentViewController(vc, animated: true, completion: nil)
【解决方案2】:

只有一行

self.navigationController!.pushViewController(self.storyboard?.instantiateViewC‌​ontrollerWithIdentifier("view2") as! UIViewController, animated: true)

【讨论】:

    【解决方案3】:

    创建从 ViewController 到 TargetViewController 的手动转场。

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

    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    

    在行动中这样做。

    func buttonAction(sender:AnyObject)
    {
        self.performSegueWithIdentifier("manualSegue", sender: nil)
    }
    

    希望对你有所帮助。

    【讨论】:

      【解决方案4】:

      首先为这样的按钮创建一个动作..

      - (IBAction)btnInvite:(id)sender {
      
      }
      

      然后将视图控制器导入您想要继续前进的位置...

      #import "YourViewController.h"
      

      最后将下面的代码放入我们在上面创建的按钮操作方法中

      - (IBAction)btnInvite:(id)sender {
      YourViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"YourViewController Identifier Name"];
      [self.navigationController pushViewController:viewController animated:YES];}
      

      希望对你有用

      【讨论】:

        【解决方案5】:
        let VC = self.storyboard?.instantiateViewController(withIdentifier:"yourViewController") as! yourViewController
         self.navigationController?.pushViewController(VC, animated:true)
        

        仅使用您的 Viewcontroller 名称更改 yourViewController

        【讨论】:

        • 请在您的答案中添加一些解释,以便将来的用户更好地理解您的代码的作用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-16
        • 1970-01-01
        • 1970-01-01
        • 2013-09-07
        • 1970-01-01
        • 2018-03-11
        相关资源
        最近更新 更多