Sigabrt在尝试显示另一个视图控制器时

时间:2016-03-24 05:35:28

标签: ios swift button viewcontroller

我想在按下按钮时转到另一个视图控制器我的btnSignUp,如果我写这样的代码我有错误" sigabrt"。我该怎么做?

import UIKit
import SnapKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)

        createTop()

    }

    override func didReceiveMemoryWarning() {

 super.didReceiveMemoryWarning()

}

    func createTop() {

        let topView = UIView()

        self.view.addSubview(topView)

        topView.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)

        topView.snp_makeConstraints { (make) -> Void in

            make.width.equalTo((self.view.frame.width/10)*8)
make.height.equalTo(self.view.frame.height/5)

            make.centerX.equalTo(self.view)

            make.top.equalTo(self.view).offset(self.view.frame.height/15)
        }



        let btnSignUp = UIButton()

        self.view.addSubview(btnSignUp)

        btnSignUp.backgroundColor =  UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)

        btnSignUp.layer.borderColor = UIColor.whiteColor().CGColor

        btnSignUp.layer.borderWidth = 1

        btnSignUp.layer.cornerRadius = 6

        btnSignUp.setTitle("Sign Up", forState: UIControlState.Normal)

        btnSignUp.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 17)

        btnSignUp.snp_makeConstraints { (make) -> Void in

 make.width.equalTo(((self.view.frame.width/10)*7)+40)

            make.height.equalTo(self.view.frame.height/13)

            make.top.equalTo(ORView.snp_bottom).offset(20)

            make.centerX.equalTo(self.view)
        }

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

  func buttonAction(sender:UIButton!)
        {

            let signUpVC = SignUpViewController()

            self.presentViewController(signUpVC, animated: true, completion: nil)

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您的操作选择器buttonAction与您的函数声明不匹配 - 它需要一个参数,因此选择器将为buttonAction:

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

此外,请检查您的括号。您的buttonAction函数似乎在 func createTop()

中声明为
func createTop() {

    let topView = UIView()

    self.view.addSubview(topView)

    topView.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)

    topView.snp_makeConstraints { (make) -> Void in

        make.width.equalTo((self.view.frame.width/10)*8)
make.height.equalTo(self.view.frame.height/5)

        make.centerX.equalTo(self.view)

        make.top.equalTo(self.view).offset(self.view.frame.height/15)
    }

    let btnSignUp = UIButton()

    self.view.addSubview(btnSignUp)

    btnSignUp.backgroundColor =  UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)

    btnSignUp.layer.borderColor = UIColor.whiteColor().CGColor

    btnSignUp.layer.borderWidth = 1

    btnSignUp.layer.cornerRadius = 6

    btnSignUp.setTitle("Sign Up", forState: UIControlState.Normal)

    btnSignUp.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 17)

    btnSignUp.snp_makeConstraints { (make) -> Void in

         make.width.equalTo(((self.view.frame.width/10)*7)+40)
         make.height.equalTo(self.view.frame.height/13)
         make.top.equalTo(ORView.snp_bottom).offset(20)
         make.centerX.equalTo(self.view)
    }

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

func buttonAction(sender:UIButton!) {

    let signUpVC = SignUpViewController()
    self.presentViewController(signUpVC, animated: true, completion: nil)
}

最后,我怀疑您希望SignUpViewController()获取新的视图控制器 - 如果您使用的是要调用的故事板instantiateViewControllerWithIdentifier