gpt4 book ai didi

ios - 重用导航栏按钮

转载 作者:行者123 更新时间:2023-11-28 12:04:35 26 4
gpt4 key购买 nike

在我的项目中,我有一个带有三个嵌入式 UIViewControllerUINavigationController。在第一个上,我将 balanceLabelrefreshButton 添加到导航栏。当点击按钮时,首先 View Controller 发送 url 请求并在标签上显示返回值。

@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),我如何才能在其他 ViewController 上获得相同的行为?

最佳答案

您可以通过扩展、使用继承来归档它,

使用所有你想要的通用功能创建你想要的 View Controller ,然后不是直接从 UIViewController 继承,而是从那个基础 viewController 继承

你的基础 Controller 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
}

关于ios - 重用导航栏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49647522/

26 4 0