2017-09-28 3 views
0

ナビゲーションタイトルに問題があり、コードで設定しています。ここでUINavigationBarTitleItemを設定できません。どうすれば設定できますか?

は、私のコードの構造である:ここで

Navigation Controller -> TableView -> UIScreen where i want to set the title. 

は私のコードです:

TableViewController:私のタイトルビューコントローラを設定

 import UIKit 

    class ProductsTableViewController: UITableViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.register(UserCell.self, forCellReuseIdentifier: cellId) 
} 

let cellId = "cellId" 
let products = [ 

    "Happy Face", 
    "Sad Face", 
    "High Five", 
    "Angry Face", 
    "The Earth" 
] 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return products.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell 

    let product = products[indexPath.row] 
    cell.textLabel?.text = product 

    return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let buyProductViewController = BuyProductViewController() 
    let product = products[indexPath.row] 
    buyProductViewController.nameOfItem = product 

    buyProductViewController.navigationItem.title = product 

    performSegue(withIdentifier: "segue", sender: self) 
} 
} 

    class UserCell: UITableViewCell { 

} 

import UIKit 

クラスBuyProductViewController:のUIViewController {

var nameOfItem: String? { 

    didSet { 

     print(nameOfItem) 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

}

、私は非常に動揺していますし、一週間かけてこれに取り組んとばかりスタックオーバーフローに出くわしてきたので、any1は私を助けることができれば、私は非常に感謝します。

ありがとうございました。

+0

試し 'self.title = "タイトル名を"' 'viewDidLoad' –

答えて

0

これは次の場合に役に立ちます。 1. ProductsTableViewControllerがUINavigationControllerに組み込まれています。 2.識別子「segue」を持つSegueがプッシュされます。違いは、プッシュされたコントローラのタイトルを直接設定することです。 NavigationBarは表示されたVCのタイトルをナビゲーションバーのタイトルとして自動的に使用します。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let buyProductViewController = BuyProductViewController() 
    let product = products[indexPath.row] 
    buyProductViewController.nameOfItem = product 

    buyProductViewController.title = product 

    performSegue(withIdentifier: "segue", sender: self) 
} 
+0

ので修正は何ですか? – OkiRules

+0

ViewControllerとiOSの 'title'プロパティを設定すると、残りの部分が処理されます。 'buyProductViewController.title = product'で、' .navigationItem'プロパティでは動作しません。これが機能していない場合は、上記のポイント1または2のいずれかが前提条件ではありません。 –

0

あなたはBuyProductViewControllerにタイトルを設定する必要があります確認してください:に設定

import UIKit 
class BuyProductViewController: UIViewController { 
    var nameOfItem: String? { 
     didSet { 
      print(nameOfItem) 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.title = nameOfItem 
    } 
} 
関連する問題