【问题标题】:addTarget not working for UIButton created in SwiftaddTarget 不适用于在 Swift 中创建的 UIButton
【发布时间】:2015-01-28 08:21:26
【问题描述】:

所以,我使用 Swift 创建了一个 UIButton,并使用以下代码对其进行了动画处理,它是超级视图:

    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)
    }

这很好。然后我创建了一个函数,应该在上面创建的按钮的 touchUpInside 上调用它:

    @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

【问题讨论】:

  • 您是否为您的introImage 设置了userInteractionEnabled = true
  • 不,我最初没有。我已经尝试过你的建议,它似乎奏效了,@Swipesight。感谢您的帮助!
  • 将 userInteractionEnabled 设置为 true 对我有用!
  • 请在此处查看我的答案stackoverflow.com/a/37706494/1416824

标签: ios iphone swift uibutton addtarget


【解决方案1】:

要给按钮添加动作,需要使用下面的代码

loginButton.addTarget(self,action:#selector(loginPressed),
forControlEvents:.TouchUpInside)

这段代码对我来说很好用。 有关更多详细信息,您可以关注此 [Attach parameter to button.addTarget action in Swift

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多