2017-10-09 106 views
0

我已成功鏈接鏈接代理方法,從CollectionViewControllerMenuTableViewControllerListViewController。在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是對MenuTableViewControllerCollectionViewController頂部從MenuTableViewController實例化,然後解僱

2)我在ListViewController調用changeTitle()創建了一個按鈕,它成功地改變了navigationItem.titleView,所以我知道的方法工作。

在此先感謝

+0

你試過路過標題代替navigationTitle! ? – arvidurs

+0

是的,不幸的是沒有工作,所以我試圖將它傳遞給一個局部變量(navigationTitle) – zachenn

+1

這裏至少有2個語法錯誤,其餘的程序缺席。你確定問題在這裏嗎? – Ssswift

回答

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

剛剛嘗試過。同樣,該方法被調用,但titleView不會改變。我認爲這與事實有關,它在SWRevealController – zachenn

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 
} 

,你可以從這個鏈接下載代碼

Link

+0

它沒有工作:(我想我只是要擺脫swrevealcontroller。它沒有更新,因爲ios6反正 – zachenn

+0

你檢查我的碼 –