Swift:未在自定义视图

时间:2016-09-16 12:23:59

标签: ios swift uibutton

我已经从xib(自由格式)创建了一个自定义视图,其中有两个按钮(登录和取消),我根据某些条件在视图中显示它。自定义视图很好地出现在另一个视图上但是按钮(登录取消)没有得到任何触摸事件。

自定义类和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() 

        }
    }

2 个答案:

答案 0 :(得分: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) {


  }

}

答案 1 :(得分:1)

检查它,它的工作原理:

ViewController.swift:

    AND id NOT IN (array_to_string(excludeArcs,','))

enter image description here

CustomAlertView.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

要进行测试,请使用以下GitHub链接:

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