我有一个图书馆应用程序,当您点击每个库时,它会提取一个架子列表,一旦点击,就会拉出每个架子上的书籍列表。我在问一个" +"在导航栏中仅显示可以添加书籍的书籍列表的视图。我试过以编程方式和故事板添加它,但是,虽然应用程序构建和运行正常,但按钮从未出现过。任何建议都会很棒!
下面是我的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
}
}
}
以下是我用来从Shelf Table View Controller推送到TableVC的代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let vc = bookTableViewController()
vc.currentShelf = currentLibrary.allShelves[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
由于
答案 0 :(得分:0)
这是因为您需要创建rightBarButtonItem
,而不仅仅是设置标题。试试这个:
var barButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: @"addTapped:")
self.navigationItem.rightBarButtonItem = barButton