- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在我的应用程序中创建一个可重复使用的按钮,并计划使用它自己的 .xib
文件来设计它。问题是我无法将 IBAction
连接到使用它的 Controller 中的自定义按钮。
我创建了一个名为 SampleButton.xib
的新 .xib
文件并添加了一个按钮。这是层次结构和 View 的样子:
然后我创建了一个名为 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/
代码应按如下方式工作:在一个 View 上,我单击标签按钮,并且 tagButtonPressed IBAction 应将 tag 变量设置为 sender.tag 的标签按钮。效果很好。然后用户应该
我有一个暂停 IBAction 和一个恢复 IBAction,两者都用于按钮,但我有在游戏过程中收集的游戏点数。我如何在暂停 IBAction 中存储不同的游戏点整数值并将它们传递给恢复 IBActi
我想知道如何从另一个 IBAction 调用 IBAction。我想要做的是,当我触摸 previousPage 上的按钮时,nextPage 按钮的标题会发生变化。换句话说,我有这些IBAction
我得到了这个随机洗牌的 IBAction @IBAction func playRoundTapped(sender: UIButton) { cardNamesArray = GKRando
根据我的标题,我正在构建一个包含一些 UIButtons 的 View 。 我正在考虑将 2 个 Action 连接到一个 UIButton 事件,因为该按钮需要做两件事,但我需要按特定顺序执行它们。
我刚刚注意到在其 IBAction 中设置 UISwitch 的 isOn 会导致再次调用 IBAction。所以下面的代码: class ViewController: UIViewControll
我在一个应用程序上工作,我想使用另一个 UIControllerView 调用一个 UIControllerView 的 IBAction,请帮助我如何实现它。 假设,我有两个名为 FirstView
当我将按钮链接到源代码(在本例中为 .h 文件)并选择 NSButton 作为类型时,Xcode 将写入: - (IBAction)pushClear:(id)sender; 据我了解应该是 - (I
我如何能够根据另一个事件运行 IBAction?例如,我可以使用一段代码来运行 IBAction。 我想做的是让两个不同的按钮运行一个或多个 IBActions。 最佳答案 您可以将多个按钮连接到同一
好吧,我正在 Xcode(版本 3)中为 Mac OS X 编写一个应用程序,但我的 IBactions 遇到了问题。我编写它们,并将它们连接到我的按钮,但由于某种原因,当我单击按钮时它们永远不会被调
+(UIBarButtonItem*) settingsButtonForController:(UIViewController*) viewController{ UIBarButtonI
我对 Xcode 和编码都很陌生,所以请耐心等待,哈哈。我正在为一个科学博览会项目开发我的第一个应用程序,该应用程序基于选项卡,有两个主页。一页包含多个开关;无论哪个开关打开,都决定其他页面上的大按钮
我正在尝试将多个按钮链接到相同的IBAction,以运行类似但不同的代码。代码是将在另一个 View Controller 上单击的图像设置到按钮下的 UIImageView 中。所有按钮都链接到相同
我需要控制用户何时在搜索栏中键入内容。使用文本字段很容易,但我找不到使用搜索栏执行此操作的方法,因为我无法使用 IBActions。 最佳答案 UISearchBar 有属性 id delegate
我正在尝试为 iPhone 创建一个应用程序,我有一个主视图 Controller 和许多其他 View Controller 。这是因为它是一个多 View 应用程序。 在 ViewControll
我有一个 IBAction: - (IBAction)stop:(id)sender { NSLog(@"Called"); if (timer) { [timer i
嗨,我正在尝试通过单击按钮打开图像,在我的 .h 我有 @interface FirstDetailViewController : UIViewController { IBOutlet U
我正在创建一个新类 - 继承自 UITextView 的 UIMyTextView。 我希望此类发送新类型的事件,然后我将使用 IBuilder 将其连接到适当的 IBAction。 怎么做? 最佳答
我正在学习 CS193p Stanford iOS 类(class),并且即将完成作业 3 - 在该作业中,我还将图形计算器转换为适用于 iPad 的通用应用程序。当在计算器中输入程序(例如“x +
我有一个 IBAction,由 UIButton 触发。 在此 IBAction 中,我将应用程序连接到服务器,返回一个 Plist。 在此期间(在我的应用程序连接到服务器和服务器返回该 Plist
我是一名优秀的程序员,十分优秀!