gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:44:57 26 4
gpt4 key购买 nike

我从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com