3

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

        }
    }
Hasya
  • 9,792
  • 4
  • 31
  • 46

2 Answers2

1

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


  }

}
Hasya
  • 9,792
  • 4
  • 31
  • 46
1

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

enter image description here

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

}

enter image description here

For testing, use the following GitHub link:

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

KSR
  • 1,699
  • 15
  • 22
  • Thank you Sathi Reddy ...Your sample code is running fine. But in my case when user will chose any option from table view, accordingly we have to show the custom alert for user ( if user have choses the premium category then custom view will appear) – Ankit Kr.Vishwakarma Sep 16 '16 at 13:37
  • Hi, please check IBOutlets in your project for customAlertView... If not working..i will change my sample project to work with TableView..Please check and let me know the result... – KSR Sep 16 '16 at 18:19