Swift:在自定义视图中未调用 UIButton 触摸事件

Posted

技术标签:

【中文标题】Swift:在自定义视图中未调用 UIButton 触摸事件【英文标题】:Swift: UIButton touch event not getting called inside the custom view 【发布时间】:2016-09-16 12:23:58 【问题描述】:

我从 xib(freeform) 创建了一个自定义视图,其中有两个按钮(登录和取消),并且我根据某些条件将其呈现在视图中。自定义视图很好地出现在另一个视图上,但按钮(登录和取消)没有收到任何触摸事件。

自定义类和init方法的代码:

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) 
  

这是我将自定义视图添加为子视图的代码块。

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

        
    

【问题讨论】:

您是否尝试在 loginButtonAction 中添加打印语句? 我在 loginButtonAction 处添加了断点 为什么不尝试在有 tableview 的类中制作委托 customAlertView 和使用方法。 请检查我的答案。如有必要,我将上传代码.. Ankit,我建议你用这两个类做一个小项目,把它放在谷歌驱动器上并在这里分享 URL,这样任何人都可以查看它并为你提供最佳解决方案。 【参考方案1】:

你也可以这样做。

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) 


  


【讨论】:

感谢@Hasya 的建议,但不幸的是它不适用于我的情况【参考方案2】:

检查一下,它的工作原理:

ViewController.swift:

import UIKit

class ViewController: UIViewController 

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

    


    @IBAction func displayAlertBtnTapped(sender: AnyObject) 
        let screen = UIScreen.mainScreen().bounds
        let customView = CustomAlertView.init(frame: CGRect(origin: CGPoint(x: 0,y: 80), size: CGSize(width: screen.width, height: screen.height/3)))
        self.view .addSubview(customView)
    

CustomAlertView.swift:

import UIKit

class CustomAlertView: UIView 

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

        print("Login button clicked");
    

    @IBAction func cancelButtonAction(sender: AnyObject) 
        print("Cancel button clicked");
    


如需测试,请使用以下 GitHub 链接:

https://github.com/k-sathireddy/AlertViewSample

【讨论】:

谢谢 Sathi Reddy ...您的示例代码运行良好。但是在我的情况下,当用户从表格视图中选择任何选项时,我们必须为用户显示自定义警报(如果用户选择了高级类别,则会出现自定义视图) 嗨,请检查您项目中的 IBOutlets 是否有 customAlertView...如果不工作..我将更改我的示例项目以使用 TableView..请检查并让我知道结果...

以上是关于Swift:在自定义视图中未调用 UIButton 触摸事件的主要内容,如果未能解决你的问题,请参考以下文章

在自定义表格视图单元格中设置 UIButton 的高度

在自定义视图中连接到 UIButton 的 AddTarget 不起作用。 (斯威夫特 5,iOS)

Swift 3 - 自定义 UIButton 半径删除约束?

在自定义单元格 Swift 中保存 uibutton 状态

在自定义 UIView 上为 UIButton 设置动画

Swift 中未调用 UICollectionView DidDeselectItemAtIndexPath 方法