在我的项目中,UINavigationController
有三个嵌入式UIViewController
。在第一个我将balanceLabel
和refreshButton
添加到导航栏。当点击按钮时,第一个视图控制器发送网址请求并在标签上显示返回值。
@IBAction func refreshButtonAction(_ sender: UIBarButtonItem) {
let operation = GetInfoOperation(...)
operation.completionBlock = { [weak self] in
DispatchQueue.main.async {
guard let balance = operation.output?.value?.balance else { return }
self?.balanceLabel.text = balance
let significantDigits = Int(Double(balance.toInt64!) * pow(10, -10))
}
}
queue.addOperation(operation)
}
如何在每个ViewController中没有重复@IBAction func refreshButtonAction(_ sender: UIBarButtonItem)
的其他ViewControllers上获得相同的行为?
答案 0 :(得分:1)
您可以使用继承
按扩展名对其进行归档使用您想要的所有常用功能创建所需的视图控制器,然后直接从UIViewController继承继承该基本viewController
您的基本控制器BaseViewController
class BaseViewController: UIViewController {
//all comman functionallity
//add your button here
}
class ChildOneViewController: BaseViewController {
//this class will get all the functionality from BaseViewController
//you can access the BaseViewController button here
//add function for ChildOneViewController
}
class ChildtwoViewController: BaseViewController {
//this class will get all the functionality from BaseViewController
//you can access the BaseViewController button here
//add function for ChildtwoViewController
}