2014-11-14 70 views
0

我有一個圖書館應用程序,當你點擊每個圖書館時,它會打開一個書架列表,一旦點擊,然後拉出書架列表。我遇到了問題,只在顯示書籍列表的視圖上的導航欄中添加「+」以添加圖書。我曾嘗試以編程方式將其添加到故事板中,但儘管應用程序構建並正常運行,但該按鈕從未出現。任何意見,這將是真棒!Swift - 嵌套導航控制器缺少「編輯」按鈕

下面

是我bookTableViewController代碼:

import UIKit 

class bookTableViewController: UITableViewController { 

@IBOutlet var addBookButton: UIBarButtonItem! 

var currentShelf = Shelf() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.navigationItem.title = "Shelf" 

    navigationItem.rightBarButtonItem?.title = "Add" 

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return currentShelf.allBooksOnShelf.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

    cell.textLabel.text = currentShelf.allBooksOnShelf[indexPath.row].bookName 

    return cell 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    if currentShelf.allBooksOnShelf[indexPath.row].hasBeenRead == false{ 
     currentShelf.allBooksOnShelf[indexPath.row].hasBeenRead = true 
    var alert = UIAlertController(title: "Read Book", message: "Thanks For Reading!", preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 

    } 
    else{ 
     currentShelf.allBooksOnShelf[indexPath.row].hasBeenRead = false 
     var alert = UIAlertController(title: "Read Book", message: "You Just UnRead A Book...", preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
     self.presentViewController(alert, animated: true, completion: nil) 

    } 

} 

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

    if editingStyle == .Delete { 

     // Delete the row from the data source 
     currentShelf.allBooksOnShelf.removeAtIndex(indexPath.row) 
     self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

    } else if editingStyle == .Insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a  new row to the table view 
    } 
    } 


} 

這裏是我使用從貨架上表視圖控制器推到這個TableVC代碼:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    let vc = bookTableViewController() 

    vc.currentShelf = currentLibrary.allShelves[indexPath.row] 

    navigationController?.pushViewController(vc, animated: true) 


} 

感謝

回答

0

這是因爲你需要創建rightBarButtonItem,而不僅僅是設置標題。試試這個:

var barButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: @"addTapped:") 
self.navigationItem.rightBarButtonItem = barButton 
+0

完美!這非常感謝。 – awallraff 2014-11-14 18:34:50

相關問題