ios 应用程序中所有视图控制器的相同按钮 [英] Same button across all view controllers in ios app
问题描述
我正在开发一个带有 10 个视图控制器的应用程序,其中包括 2 个 webview 控制器.每个视图控制器在右上角都包含一个发送反馈"按钮,具有完全相同的样式和功能.现在,我在每个视图控制器中为按钮编写完全相同的代码.我想知道是否有一种方法可以只在一个地方编写方法并将其用于所有按钮,因为功能是相同的.我正在使用 swift 3.
I am developing an app with 10 view controllers, including 2 webview controllers. Every view controller contains a 'send feedback' button on top right with exactly same style and functionality. Right now, I am writing exact same code in each view controller for the buttons. I was wondering if there is a way to write the method only at one place and use it for all the buttons, since the function is same. I am using swift 3.
推荐答案
创建视图控制器的新子类:
Create new subclass of view controller:
class SendFeedbackViewController: UIViewController {
@IBOutlet weak var sendFeedbackButton: UIButton!
@IBAction func sendFeedback() {
/* do whatever you need */
}
}
然后从这个新的视图控制器继承所有的视图控制器:
Then subclass all your view controllers from this new view controller:
class YourViewController: SendFeedbackViewController {
/* your entire logic */
}
在情节提要中设置类类型:
In storyboard set class type:
现在您可以连接您的发送反馈按钮出口和操作:
Now you can connect your send feedback button outlet and action:
这篇关于ios 应用程序中所有视图控制器的相同按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!