方法被调用,但执行不正确 [英] Method being called, but not executing properly

查看:279
本文介绍了方法被调用,但执行不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地从 CollectionViewController -> MenuTableViewController -> ListViewController链接链接的委托方法。在 ListViewController 扩展名中,我调用 changeTitleView(),但是它不起作用。但是,数据已成功传递,因为 print(title)正确打印了传递的信息。如果从 ListViewController

内调用 changeTitle()即可正确执行p $ p> class ListViewController {
var navigationTitle:字符串?

@objc func changeTitle(title:String){
let titleLabel = UILabel()
let属性:NSDictionary = [
NSAttributedStringKey.font:UIFont(name: HelveticaNeue,大小:20)!,
NSAttributedStringKey.foregroundColor:UIColor(红色:0.1137,绿色:0.1137,蓝色:0.149,alpha:0.8)/ *#1d1d26 * /,
NSAttributedStringKey.kern :CGFloat(2.0)
]

let attributedTitle = NSAttributedString(string:
title.uppercased(),attributes:attribute as?[NSAttributedStringKey:AnyObject])
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}
}

扩展ListViewController:HandleTitleView {
@objc func changeTitleView(title:String){
print(title)
self.navigationTitle = title

changeTitle(title:navigationTitle!)
}

Additi全部信息:



1)我正在通过SWRevealController传递信息,因此并非所有数据都在同一导航堆栈中。 ListViewController MenuTableViewController 之上,而 CollectionViewController 实例化自 MenuTableViewController 然后关闭



2)我在 ListViewController 调用 changeTitle()并成功更改了 navigationItem.titleView ,因此我知道该方法有效。 / p>

预先感谢

解决方案

  @objc func changeTitle(title:String){
let titleLabel = UILabel()
let attributeFontSaySomething:[String:Any] = [NSFontAttributeName:UIFont(name: HelveticaNeue,size:20)吗? ? UIFont.systemFont(ofSize:20),NSForegroundColorAttributeName:UIColor(红色:0.1137,绿色:0.1137,蓝色:0.149,alpha:0.8),NSKernAttributeName:CGFloat(2.0)]

var属性= attributeFontSaySomething

let attStringSaySomething = NSAttributedString(字符串:title.uppercased(),属性:属性)

titleLabel.attributedText = attStringSaySomething
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}

,您可以从此链接下载代码



链接


I have successfully chain linked delegate methods from CollectionViewController -> MenuTableViewController -> ListViewController. In ListViewController extension I call changeTitleView() but it is not working. The data, however is successfully passed because print(title) correctly prints passed information. changeTitle() executes properly if called from within 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!)
}

Additional information:

1) I am passing information through SWRevealController so not all data is within the same navigation stack. ListViewController is on top of MenuTableViewController and CollectionViewController is instantiated from MenuTableViewController and then dismissed

2) I created a button in ListViewController that calls changeTitle() and it successfully changes the navigationItem.titleView so I know the method works.

Thanks in advance

解决方案

  @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
}

and you can download the code from this link

Link

这篇关于方法被调用,但执行不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆