gpt4 book ai didi

ios - 调整 UINavigationController 的协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 13:48:10 25 4
gpt4 key购买 nike

在我的每一个 View Controller 中,我都调用一个函数来编辑导航 Controller 的外观和左/右按钮的文本。我不是在每个 View Controller 中都包含此代码,而是尝试实现每个 View Controller 都遵循的协议(protocol)。

每个 View Controller 中的当前代码:

func setupNavReg(backButtonText: String, nextButtonText: String)
{
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)

let leftNavButton = UIBarButtonItem(title: backButtonText, style: .Plain, target: self, action: Selector("backButtonPressed"))
let customFont = UIFont(name: "Gill Sans", size: 14.0)

leftNavButton.setTitleTextAttributes([NSFontAttributeName:customFont!], forState: UIControlState.Normal)
self.navigationItem.leftBarButtonItem = leftNavButton

//nextButton left out for
}

协议(protocol)设计

protocol navBarInterface {
var navigationController: UINavigationController? { get }
func configureWithBackButton(backButtonText: String, nextButtonText: String)
}

extension navBarInterface {
func configureWithBackButton(backButtonText: String, nextButtonText: String) {

if let navigationController = navigationController {
navigationController.navigationBar.barTintColor = UIColor.whiteColor()
navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController.navigationBar.shadowImage = UIImage()
let customFont = UIFont(name: "Gill Sans", size: 14.0)

let leftNavButton = UIBarButtonItem(title: backButtonText, style: .Plain, target: nil, action: Selector("backButtonPressed"))
leftNavButton.tintColor = UIColor.whiteColor()
leftNavButton.setTitleTextAttributes([NSFontAttributeName:customFont!], forState: UIControlState.Normal)
navigationController.navigationItem.leftBarButtonItem = leftNavButton

//nextButton left out
}
}
}

到目前为止,我还无法让它发挥作用。导航 Controller 的外观没有改变,但我发现当 View Controller 出现时,我设置的协议(protocol)断点被命中。我还担心按下导航按钮时我将无法点击选择器功能。

最佳答案

如果此协议(protocol)仅适用于 UINavigationController,那么我会忘记该协议(protocol),而只需在 UINavigationController 上定义一个扩展:

extension UINavigationController {

func configureWithBackButton(backButtonText: String, nextButtonText: String) {
self.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
let customFont = UIFont(name: "Gill Sans", size: 14.0)

let leftNavButton = UIBarButtonItem(title: "Back", style: .Plain, target: nil, action: Selector("backButtonPressed"))
leftNavButton.tintColor = UIColor.whiteColor()
leftNavButton.setTitleTextAttributes([NSFontAttributeName:customFont!], forState: UIControlState.Normal)
self.navigationItem.leftBarButtonItem = leftNavButton

//nextButton left out
}
}

我只是这么说,因为协议(protocol)有时会以奇怪的方式与类和子类交互 - 即,如果您的类定义了该方法,但协议(protocol)也定义了它,会发生什么,等等。所以尝试扩展该类,看看是否可以解决问题所在。

我还注意到您的代码有所不同 - 在原始方法 setupNavReg 中,您将后退按钮的文本设置为 backButtonText,但在您的扩展中设置到“返回”。这是故意的吗?这可能就是为什么文本没有更改为您传递给方法的任何字符串的原因......

关于ios - 调整 UINavigationController 的协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34661848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com