我已成功链接来自CollectionViewController
- >的委托方法。 MenuTableViewController
- > ListViewController
。在ListViewController
扩展程序中,我致电changeTitleView()
,但它无效。但是,数据成功传递,因为print(title)
正确打印传递的信息。如果从changeTitle()
ListViewController
会正确执行
class ListViewController {
var navigationTitle: String?
@objc func changeTitle(title: String) {
let titleLabel = UILabel()
let attributes: NSDictionary = [
NSAttributedStringKey.font:UIFont(name: "HelveticaNeue", size: 20)!,
NSAttributedStringKey.foregroundColor: UIColor(red: 0.1137, green: 0.1137, blue: 0.149, alpha: 0.8) /* #1d1d26 */,
NSAttributedStringKey.kern:CGFloat(2.0)
]
let attributedTitle = NSAttributedString(string:
title.uppercased(), attributes: attributes as? [NSAttributedStringKey : AnyObject])
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}
}
extension ListViewController: HandleTitleView {
@objc func changeTitleView(title: String) {
print(title)
self.navigationTitle = title
changeTitle(title: navigationTitle!)
}
其他信息:
1)我通过SWRevealController传递信息,因此并非所有数据都在同一个导航堆栈中。 ListViewController
位于MenuTableViewController
之上,CollectionViewController
已从MenuTableViewController
实例化,然后被解除
2)我在ListViewController
创建了一个调用changeTitle()
的按钮,它成功更改了navigationItem.titleView
,因此我知道该方法有效。
提前致谢
答案 0 :(得分:0)
你有没有试过以下
class ListViewController {
var navigationTitle: String? {
didSet{
if(navigationTitle != nil) {
changeTitle(title: navigationTitle!)
}
}
}
@objc func changeTitle(title: String) {
let titleLabel = UILabel()
let attributes: NSDictionary = [ NSAttributedStringKey.font:UIFont(name: "HelveticaNeue", size: 20)!, NSAttributedStringKey.foregroundColor: UIColor(red: 0.1137, green: 0.1137, blue: 0.149, alpha: 0.8) /* #1d1d26 */, NSAttributedStringKey.kern:CGFloat(2.0) ]
let attributedTitle = NSAttributedString(string: title.uppercased(), attributes: attributes as? [NSAttributedStringKey : AnyObject])
titleLabel.attributedText = attributedTitle titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}
}
extension ListViewController: HandleTitleView {
@objc func changeTitleView(title: String) {
print(title)
self.navigationTitle = title
// changeTitle(title: navigationTitle!)
}`
答案 1 :(得分:0)
@objc func changeTitle(title: String) {
let titleLabel = UILabel()
let attributeFontSaySomething : [String : Any] = [NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 20) ?? UIFont.systemFont(ofSize: 20) ,NSForegroundColorAttributeName :UIColor(red: 0.1137, green: 0.1137, blue: 0.149, alpha: 0.8),NSKernAttributeName : CGFloat(2.0) ]
var attributes = attributeFontSaySomething
let attStringSaySomething = NSAttributedString(string: title.uppercased(), attributes: attributes)
titleLabel.attributedText = attStringSaySomething
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}
您可以从此链接下载代码