Swift - 嵌套导航控制器缺少“编辑”按钮
Posted
技术标签:
【中文标题】Swift - 嵌套导航控制器缺少“编辑”按钮【英文标题】:Swift - nested navigation controller missing "edit" button 【发布时间】:2014-11-14 17:46:43 【问题描述】:我有一个图书馆应用程序,当您点击每个图书馆时,它会拉出书架列表,点击后会拉出每个书架上的书籍列表。我在导航栏中仅在显示书籍列表的视图上放置“+”时遇到问题,以便能够添加书籍。我已经尝试以编程方式以及使用情节提要添加它,但是,尽管应用程序可以正确构建和运行,但按钮永远不会出现。任何与此相关的建议都会很棒!
下面是我的 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)
谢谢
【问题讨论】:
【参考方案1】:这是因为你需要创建一个rightBarButtonItem
,而不仅仅是设置标题。试试这个:
var barButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: @"addTapped:")
self.navigationItem.rightBarButtonItem = barButton
【讨论】:
以上是关于Swift - 嵌套导航控制器缺少“编辑”按钮的主要内容,如果未能解决你的问题,请参考以下文章
用于在 Swift 中删除 UITableView 中的行的编辑按钮不起作用