Method being called, but not executing properly

2019-08-23 10:52发布

问题:

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

回答1:

Did you try like below

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!) 
}`


回答2:

  @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