Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift如何添加shouldReceiveTouch on开关?_Swift_Ios8 - Fatal编程技术网

Swift如何添加shouldReceiveTouch on开关?

Swift如何添加shouldReceiveTouch on开关?,swift,ios8,Swift,Ios8,我试图在UI开关上添加我的项目shouldReceiveTouch,但我有一个错误 class ViewController: UIViewController,UIGestureRecognizerDelegate { var newSwitch: UISwitch! var deleteButtonIndex:Int! var deleteSwitchIndex:Int! let gesture = UILongPressGestureRecognizer() func CreateSwi

我试图在UI开关上添加我的项目shouldReceiveTouch,但我有一个错误

class ViewController: UIViewController,UIGestureRecognizerDelegate {
var newSwitch: UISwitch!
var deleteButtonIndex:Int!
var deleteSwitchIndex:Int!
let gesture = UILongPressGestureRecognizer()

func CreateSwitchWithIndex(index:Int) {
     let newSwitch = UISwitch()
     newSwitch.tintColor = UIColor.blackColor()
     newSwitch.on = false
     newSwitch.tag = index+1;
     newSwitch.addTarget(self, action: Selector("switchSen:"), forControlEvents: UIControlEvents.ValueChanged)
     self.view.addSubview(newSwitch)
     newSwitch.setOn(false, animated: true)

     let newSwitchConstraintL = NSLayoutConstraint(item: newSwitch, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)

    let newSwitchConstraintH = NSLayoutConstraint(item: newSwitch, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)

    let coordX = 368
    let coordY = 190 

    newSwitchConstraintX = NSLayoutConstraint(item: newSwitch, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 0, constant: CGFloat(coordX))

    newSwitchConstraintY = NSLayoutConstraint(item: newSwitch, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 0, constant: CGFloat(coordY))

    view.addConstraints([newSwitchConstraintX,newSwitchConstraintY,newSwitchConstraintH,newSwitchConstraintL])

    var longPress = UILongPressGestureRecognizer(target: self, action: "menu:")
     longPress.minimumPressDuration = 1
     longPress.delegate = self 
     newSwitch .addGestureRecognizer(longPress)
 }

func switchSen(sender:UISwitch!) {
    if (sender.on == true){
        println("on")
    }
    else{
        println("off")
    }
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

    if(touch.view == newSwitch){
        return false
    }else{
        return true
    }
}

func menu(gesture:UILongPressGestureRecognizer) {

    if(gesture.state == UIGestureRecognizerState.Began){
        self.deleteButtonIndex = gesture.view?.tag
        self.deleteSwichIndex = gesture.view?.tag
        becomeFirstResponder()
        var menu = UIMenuController.sharedMenuController()
        var deleteItem = UIMenuItem(title: "Delete", action: Selector("delete"))
        menu.menuItems = [deleteItem]
        var lastLocation:CGPoint = gesture.locationInView(view)
        menu.setTargetRect(CGRectMake(lastLocation.x, lastLocation.y, 0.0, 0.0), inView: view)
        menu.setMenuVisible(true, animated: true)
    }
}

func delete(){
    println("Delete")
}
}
我刚刚得到以下错误:致命错误:在iftouch.view==newSwitch上展开可选值时意外发现nil


如何添加shouldReceiveTouch on开关?

请尝试使用此线路

let newSwitch: UISwitch!
而不是

var newSwitch: UISwitch!