Swift3 Xcode中可重用的*按钮*
我已经创建了一个包含3个选项的操作表,可以推送到其他视图控制器并拨打电话。此选项位于我的应用程序每个屏幕上导航栏上的按钮上。我试图找到一种方法来重用代码,而不必在每个屏幕上重新键入代码。我知道有一种方法可以通过对Swift3 Xcode中可重用的*按钮*,swift3,xcode8,Swift3,Xcode8,我已经创建了一个包含3个选项的操作表,可以推送到其他视图控制器并拨打电话。此选项位于我的应用程序每个屏幕上导航栏上的按钮上。我试图找到一种方法来重用代码,而不必在每个屏幕上重新键入代码。我知道有一种方法可以通过对UIButton进行子类化来实现这一点,但我已经搜索了好几天,没有找到关于如何对子类进行实际编码的明确答案。我现在的代码如下: class moreButton: UIButton { @IBAction func displayActionSheet(_ sender: Any) {
UIButton
进行子类化来实现这一点,但我已经搜索了好几天,没有找到关于如何对子类进行实际编码的明确答案。我现在的代码如下:
class moreButton: UIButton {
@IBAction func displayActionSheet(_ sender: Any) {
let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "moveSegue", sender: self)
})
let shopAction = UIAlertAction(title: "Visit Shop", style: .default, handler: {(action: UIAlertAction!)->Void in self.performSegue(withIdentifier: "shopSegue", sender: self)
})
let callAction = UIAlertAction(title: "Call Us Now", style: .default) { _ in
let url:URL = URL(string: "tel://number")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(followAction)
alertController.addAction(shopAction)
alertController.addAction(callAction)
alertController.addAction(cancel)
self.present(alertController, animated: true, completion: nil)
}
}
我得到了一个错误:
“moreButton”类型的值没有成员“performsgue”
在followAction和shopAction行中,以及最后一行的“类型为'moreButton'的值没有成员'present'”。当我直接从按钮创建一个
iAction
时,此代码正在工作,但现在它似乎有错误,我不知道如何更正。您收到这些错误,因为performsgue
和present
都是UIViewController方法
我不会子类化UIButton,而是创建一个新的helper类,使用class方法为您构建UIAlertController。该方法将接受UIViewController作为其参数,以便能够正确构建UIAlertActions
您可以从displayActionSheet
调用此新方法,如下所示:
let alertController = ActionSheetBuilder.build(viewController: self)
present(alert, animated: true, completion: nil)
将您的创建代码调整到新类中会出现如下情况:
final class ActionSheetBuilder {
class func build(viewController: UIViewController) -> UIAlertController {
let alertController = UIAlertController(title: nil, message: "Get Your Roast On", preferredStyle: .actionSheet)
let followAction = UIAlertAction(title: "Follow Us", style: .default, handler: {(action: UIAlertAction!)->Void in viewController.performSegue(withIdentifier: "moveSegue", sender: viewController) })
let shopAction = UIAlertAction(title: "Visit Shop", style: .default, handler: {(action: UIAlertAction!)->Void in viewController.performSegue(withIdentifier: "shopSegue", sender: viewController) })
let callAction = UIAlertAction(title: "Call Us Now", style: .default) { _ in
let url:URL = URL(string: "tel://2845471035")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(followAction)
alertController.addAction(shopAction)
alertController.addAction(callAction)
alertController.addAction(cancel)
return alertController
}
}
您正试图使按钮正常工作,这正是视图控制器的工作。视图控制器可以执行分段并显示其他控制器,但按钮不能,因此
self
不知道您要的是什么。考虑把这个代码放在一个子类中:<代码> UIViewController <代码>,然后让所有其他的“屏幕”从VC中继承。这真的很有帮助。我只是假设,因为它与按钮的iAction一起工作,所以如果我将按钮子类化,它就会工作。我现在在下面的代码中遇到了一些问题,但我会看看它是否有效。我的想法是,iAction是按钮代表它引起的事情,而不是按钮为自己做的事情。这部分起作用。我最初将alertController放在viewDidLoad中,因此现在将其移动,但我收到了错误:“无法将类型为”(NSObject)->()->“displayActionSheet”的值转换为预期的参数类型“UIViewController”。我还收到了当前(警报…)行上的“预期声明”