调用addTarget时以未捕获的异常NSException终止

时间:2018-04-01 06:31:58

标签: swift

我创建了一个快速的Playground,我希望每当我按下按钮时都会发生一个动作,但是我一直得到一个运行时异常"以NSException"类型的未捕获异常终止。我将如何解决这个问题(快速)我不确定发生了什么,我是新手。谢谢编辑:我添加了完整的代码

import Foundation
import PlaygroundSupport
import SpriteKit
import GameplayKit
import UIKit

let textFiled = UITextField(frame: CGRect(x: 320, y: 130, width: 100, height: 33))
let view1 = UIView(frame: CGRect(x: 0, y: 0, width: 1200, height: 1200))
var btn = UIButton(frame: CGRect(x: 280, y: 550, width: 200, height: 50))



public class LoadView: UIViewController {

public static func loadView() {


    view1.backgroundColor = UIColor(patternImage: UIImage(named: "background1.png")!)

   //text label
    let label = UILabel()
    label.frame = CGRect(x: 230, y: 100, width: 400, height: 30)
    label.text = "Enter a 3 Digit Key In the range 128-255:"
    label.textColor = .black

    //rectangle
    let rect = UIView(frame: CGRect(x: 30, y: 40, width: 718, height: 1100))
    rect.backgroundColor = .brown
    view1.addSubview(rect)

    //text field
    //let textFiled = UITextField(frame: CGRect(x: 320, y: 130, width: 100, height: 33))
    textFiled.backgroundColor = UIColor.gray
    textFiled.borderStyle = UITextBorderStyle.line
    view1.addSubview(textFiled)        

    //message text
    let msg2 = UILabel()
    msg2.frame = CGRect(x: 300, y: 400, width: 400, height: 30)
    msg2.text = "Encrypted Message:"
    msg2.textColor = .black

    //message text field
    let Encyped_msg = UITextField(frame: CGRect(x: 220, y: 435, width: 300, height: 33))
    Encyped_msg.backgroundColor = UIColor.gray
    Encyped_msg.borderStyle = UITextBorderStyle.line
    view1.addSubview(Encyped_msg)

   //message text
    let msg2 = UILabel()
    msg2.frame = CGRect(x: 300, y: 400, width: 400, height: 30)
    msg2.text = "Encrypted Message:"
    msg2.textColor = .black

    //message text field
    let Encyped_msg = UITextField(frame: CGRect(x: 220, y: 435, width: 300, height: 33))
    Encyped_msg.backgroundColor = UIColor.gray
    Encyped_msg.borderStyle = UITextBorderStyle.line
    view1.addSubview(Encyped_msg)

    view1.addSubview(msg)
    view1.addSubview(msg2)
    view1.addSubview(label)



    PlaygroundPage.current.liveView = view1


}


}


public class bttn: LoadView{
public static func createButton(){
    btn.setTitle("Process", for: .normal)
    btn.setTitleColor(UIColor.black, for: .normal)
    btn.backgroundColor = .green
    print("im here")
    btn.addTarget(self, action: #selector(bttn.encr), for: UIControlEvents.touchUpInside)

    view1.addSubview(btn)
}

@objc func encr(sender: UIButton) {
    print("need to encrypt")
}

}

0 个答案:

没有答案