ios - Swift:UIButton 触摸事件未在自定义 View 中调用

标签 ios swift uibutton

我从 xib(freeform) 创建了一个自定义 View ,其中有两个按钮(登录和取消),我根据某些条件在 View 中显示它。自定义 View 很好地显示在另一个 View 上,但按钮(登录和取消)没有任何触摸事件。

自定义类代码和初始化方法:

import UIKit
class customAlertView: UIView {

  @IBOutlet weak var messageLabel: UILabel!
  @IBOutlet weak var loginButton : UIButton!
  @IBOutlet weak var cancelButton: UIButton!

  var view : UIView!


  override init(frame: CGRect) {
    super.init(frame: frame)

    view  = setUpFromXib()
    view.frame  = frame
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  func setUpFromXib() -> UIView {

    let bundle  = NSBundle(forClass: self.dynamicType)
    let nib     = UINib(nibName: "customAlertView", bundle: bundle)
    let view    = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    addSubview(view)
    translatesAutoresizingMaskIntoConstraints = true
    return view

  }

  @IBAction func loginButtonAction(sender: AnyObject) {
  }

  @IBAction func cancelButtonAction(sender: AnyObject) {
  }
}

这是我将自定义 View 添加为 subview 的代码块。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if Reachability.isConnectedToNetwork() {

            categoryObj = categoryArray .objectAtIndex(indexPath.row) as! TECategoryDetails
            if categoryObj.categoryType == "premium" {

              let screen = UIScreen.mainScreen().bounds

             customView  = customAlertView.init(frame: CGRect(origin: CGPoint(x: 0,y: 80), size: CGSize(width: screen.width, height: screen.height/3)))
              self.view .addSubview(customView)

                }

            else{
              watchAllFlag = false
              self.performSegueWithIdentifier("Episode", sender: self)
            }
        }
        else {
            self.showAlertPopUp() 

        }
    }

最佳答案

你也可以这样做。

import UIKit
class customAlertView: UIView {

  @IBOutlet weak var messageLabel: UILabel!
  @IBOutlet weak var loginButton : UIButton!
  @IBOutlet weak var cancelButton: UIButton!

  var view : UIView!


  override init(frame: CGRect) {
    super.init(frame: frame)

    view  = setUpFromXib()
    view.frame  = frame

    loginButton.addTarget(self, action: Selector(“loginButtonAction:”), forControlEvents: .TouchUpInside)
    cancelButton.addTarget(self, action: Selector(“cancelButtonAction:”), forControlEvents: .TouchUpInside)


  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

  func setUpFromXib() -> UIView {

    let bundle  = NSBundle(forClass: self.dynamicType)
    let nib     = UINib(nibName: "customAlertView", bundle: bundle)
    let view    = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    addSubview(view)
    translatesAutoresizingMaskIntoConstraints = true
    return view

  }

  func loginButtonAction(sender: AnyObject) {


  }

  func cancelButtonAction(sender: AnyObject) {


  }

}

关于ios - Swift:UIButton 触摸事件未在自定义 View 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39531450/

相关文章:

ios - iPad 上的 iOS 10.1.1 应用商店拒绝 iPhone 应用

ios - 禁用/隐藏应用内购买

ios - 无法在 Crashlytics/fabric 中获取崩溃报告。显示错误 : "It looks like we are missing dSYMs to process crashes for the versions below."

ios - 在 Swift 中设置 Coreplot Axis 的 Axis 约束

ios - 平移手势不会将触摸转发到 Mapbox MGLMapView

iphone - 应用程序在按钮的方法调用时崩溃

objective-c - 更改单元格内的 UIButton 图像

ios - Xcode 选项卡式应用程序 - 添加新选项卡 View

ios - 在用户将手指从按钮上移开后,保持显示的发光效果TouchWhen Highlighted

iphone - 如果 iPhone 应用程序中的 UITextField 不为空,则启用 UIButton