简体   繁体   中英

Swift: UIButton touch event not getting called inside the custom view

I have created a custom view from xib(freeform) in which there are two button (Login and cancel) and i have present it at a view according to some condition. Custom view get present on another view nicely but the button(Login an cancel) not getting any touch event.

Code of custom class and init method:

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

This is the block of code from where i have add the custom view as a 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() 

        }
    }

You can also do like this way.

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


  }

}

Check it, its working:

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

}

在此输入图像描述

For testing, use the following GitHub link:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM