Ios addTarget不适用于Swift中创建的UIButton

Ios addTarget不适用于Swift中创建的UIButton,ios,iphone,swift,uibutton,addtarget,Ios,Iphone,Swift,Uibutton,Addtarget,因此,我使用Swift创建了一个UIButton,并使用以下代码为它和它的superview设置动画: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //create a UIView to contain buttons. Set background

因此,我使用Swift创建了一个UIButton,并使用以下代码为它和它的superview设置动画:

    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.

      //create a UIView to contain buttons. Set background color to nil, so it's transparent.
      var controlsView = UIView(frame: CGRect(x:0, y:0, width:400, height:400));
      controlsView.alpha = 1.0
      controlsView.backgroundColor = nil

      //create a button and add some attributes. Self explanatory
      var loginButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
      loginButton.frame = CGRectMake(20.0, 40.0, 200, 60)
      loginButton.alpha = 0.0
      loginButton.setTitle("Login", forState: .Normal)
      loginButton.titleColorForState(.Normal)
      loginButton.layer.cornerRadius = 20;
      loginButton.layer.borderWidth = 1.0
      loginButton.layer.borderColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [0.827, 0.325, 0.0, 1.0])
      loginButton.addTarget(self, action: "loginPressed:", forControlEvents: .TouchUpInside)

      //add the button to the view created to hold it
      controlsView.addSubview(loginButton)

      //now test blurring the background view. Create a UIVisualEffectView to create the blur effect. New in iOS7/8
    /*
      var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
      visualEffectView.frame = self.introImage.bounds
      self.introImage.addSubview(visualEffectView)
      visualEffectView.alpha = 0.0
    */

      //add the controls subview to the image view already here.
      self.introImage.addSubview(controlsView)

      //loginButton.addTarget(self, action: "loginButtonWasPressed:",forControlEvents:.TouchUpInside)


      UIView.animateWithDuration(0.7,
        animations: {
            loginButton.alpha = 1.0
            loginButton.frame = CGRect(x: 20.0, y: 100.0, width: 200, height: 60)
            //visualEffectView.alpha = 0.5
        },
        completion:nil)
    }
那很好。然后,我创建了一个函数,该函数应该在我上面创建的按钮的触碰内部调用:

    @IBAction func loginPressed(sender:UIButton){
      var alertView = UIAlertView();
      alertView.addButtonWithTitle("Ok");
      alertView.title = "title";
      alertView.message = "message";
      alertView.show();
    }

当我按下按钮时,该功能不会启动。有人能帮忙吗?我正在一台实际的设备上测试这一点(iPhone 6 Plus/iOS 8.1.1

要向按钮添加操作,您需要使用以下代码

loginButton.addTarget(self,action:#selector(loginPressed),
forControlEvents:.TouchUpInside)
这个代码对我来说很好用。 如需了解更多详细信息,请遵循以下步骤[


谢谢。

你是否为你的
introImage
设置了
userInteractionEnabled=true
?不,我一开始没有设置。我已经尝试了你的建议,而且似乎奏效了,@swipsight。谢谢你的帮助!将userInteractionEnabled设置为true对我有效!请在这里查看我的答案