【问题标题】:Reuse navigation bar button重用导航栏按钮
【发布时间】:2018-09-13 19:26:10
【问题描述】:

在我的项目中,我有 UINavigationController 和三个嵌入的 UIViewControllers。在第一个上,我将balanceLabelrefreshButton 添加到导航栏。单击按钮时,首先查看控制器发送 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)

}

我怎样才能在其他 ViewControllers 上获得相同的行为而不在每个 ViewController 中重复 @IBAction func refreshButtonAction(_ sender: UIBarButtonItem)

【问题讨论】:

标签: ios swift uinavigationcontroller uinavigationitem


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多