gpt4 book ai didi

ios - 带有自己的 .xib 文件的 UIButton 的 IBAction

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:26 32 4
gpt4 key购买 nike

我想在我的应用程序中创建一个可重复使用的按钮,并计划使用它自己的 .xib 文件来设计它。问题是我无法将 IBAction 连接到使用它的 Controller 中的自定义按钮。

我创建了一个名为 SampleButton.xib 的新 .xib 文件并添加了一个按钮。这是层次结构和 View 的样子:

enter image description here

然后我创建了一个名为 SampleButton.swift 的新 swift 文件,其中包含一个名为 SampleButton 的类,该类是 UIButton 的子类,并将其指定为我的 SampleButton.xib 文件中的文件所有者。

SampleButton.swift内容如下:

import Foundation
import UIKit

@IBDesignable
class SampleButton: UIButton {

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

func setup() {
guard let view = loadViewFromNib() as? UIButton else {
return
}
view.frame = bounds
view.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,
UIView.AutoresizingMask.flexibleHeight]
addSubview(view)

view.layer.borderWidth = 2
view.layer.borderColor = UIColor.white.cgColor
}

func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIButton
}

@IBAction func pressed(_ sender: Any) {
print("Called in here")
}
}

然后我可以在 Storyboard中创建一个新按钮并将其设置为自定义并将类设置为 SampleButton。但是现在,如果我 ctrl + 从我的按钮拖动到我相应的 View Controller 来为按钮创建一个 IBAction,它不会被调用。 SampleButton.swift 文件中的那个是。即使我删除了 SampleButton 文件中的 IBAction,它仍然不会被调用。

这里有什么帮助吗?我希望能够单独设计按钮,然后在使用它们的 Controller 中为它们设置 IBactions

最佳答案

我在我的一些自定义 xib View 中遇到了同样的问题,我最初的想法是我可以将我的 xib 设置为 IBDesignable,然后从 View Controller 中我的按钮的 Storyboard渲染连接 socket 。

那没用。

因此,我使用自定义 View 中的委托(delegate)回调设置了一些变通方法。我使用它们为 View Controller 的 View 创建了 IBOutlets,然后在 viewDidLoad 中我将设置委托(delegate)并处理 View Controller 中的按钮点击

import UIKit

// defines a callback protocol for the SampleButtonView
protocol SampleButtonViewDelegate: class {
func sampleButtonTapped(_ button: SampleButton)
}

@IBDesignable
class SampleButton: UIView, NibLoadable {

// create IBOutlet to button if you want to register a target/action directly
@IBOutlet var button: UIButton!

// set delegate if you want to handle button taps via delegate
weak var delegate: SampleButtonViewDelegate?

// initializers to make it so this class renders in view controllers
// when using IBDesignable
convenience init() {
self.init(frame: .zero)
}

override init(frame: CGRect) {
super.init(frame: frame)
loadFromNib(owner: self)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadFromNib(owner: self)
}

@IBAction func buttonTapped(_ sender: Any) {
delegate?.sampleButtonTapped(_ button: self)
}
}

// here's a sample ViewController using this view and the delegate callback method
class ViewController: UIViewController {

@IBOutlet var sampleButtonView: SampleButton!

override func viewDidLoad() {
super.viewDidLoad()

sampleButtonView.delegate = self
}
}

extension ViewController: SampleButtonViewDelegate {
func sampleButtonTapped(_ button: SampleButton) {
// TODO: run logic for button tap here
}
}

为了完整起见,我还将在此处添加此 NibLoadable 协议(protocol)定义。

// I used this for the @IBDesignable functionality to work and actually render 
// my xib layouts in the storyboard view controller layouts using this class
import UIKit

/// Defines an interface for UIViews defined in .xib files.
public protocol NibLoadable {

// the name of the associated nib file
static var nibName: String { get }

// loads the view from the nib
func loadFromNib(owner: Any?)
}

public extension NibLoadable where Self: UIView {

/// Specifies the name of the associated .xib file.
/// Defaults to the name of the class implementing this protocol.
/// Provide an override in your custom class if your .xib file has a different name than it's associated class.
static var nibName: String {
return String(describing: Self.self)
}

/// Provides an instance of the UINib for the conforming class.
/// Uses the bundle for the conforming class and generates the UINib using the name of the .xib file specified in the nibName property.
static var nib: UINib {
let bundle = Bundle(for: Self.self)
return UINib(nibName: Self.nibName, bundle: bundle)
}

/// Tries to instantiate the UIView class from the .xib file associated with the UIView subclass conforming to this protocol using the owner specified in the function call.
/// The xib views frame is set to the size of the parent classes view and constraints are set to make the xib view the same size as the parent view. The loaded xib view is then added as a subview.
/// This should be called from the UIView's initializers "init(frame: CGRect)" for instantiation in code, and "init?(coder aDecoder: NSCoder)" for use in storyboards.
///
/// - Parameter owner: The file owner. Is usually an instance of the class associated with the .xib.
func loadFromNib(owner: Any? = nil) {
guard let view = Self.nib.instantiate(withOwner: owner, options: nil).first as? UIView else {
fatalError("Error loading \(Self.nibName) from nib")
}
view.frame = self.bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
}
}

您也可以简单地将您在 View Controller 中定义的函数注册为自定义 View 中按钮的目标/操作函数。

override func viewDidLoad() {
super.viewDidLoad()

mySampleButtonView.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}

@objc func buttonTapped(_ sender: UIButton) {
// handle button tap action in view controller here...
}

关于ios - 带有自己的 .xib 文件的 UIButton 的 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52208392/

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