将导航栏项目按钮设置为新标题 [英] Set navbar item button to new title
问题描述
美好的一天.我面临一个奇怪的问题,当我选择一行时,我想在下一个中将正确的导航项设置为完成".我试过了,它奏效了.但它打破了,因为实现 doneEditing 主体的函数只在下一个视图控制器中,任何帮助将不胜感激.这是我的代码:
Good day. I'm facing a weird issue, I'd like to set the right navigation item to Done in my next when I've selected a row. I tried it, and it's worked. But it's breaking however, because the function which implements the doneEditing body, is only in the next view controller, any help will be really appreciated. This is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "editContact" {
let indexPath = tableView.indexPathForSelectedRow()
let destinationVC: NewCategoryViewController = segue.destinationViewController as! NewCategoryViewController
let contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as! Contact
destinationVC.contact = contact
var rightAddBarButtonItem:UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneEditing:")
destinationVC.navigationItem.rightBarButtonItem = rightAddBarButtonItem
}
}
我的下一个视图控制器是:
and my next view controller is :
import UIKit
import CoreData
class NewCategoryViewController: UIViewController {
// MARK: - Properties
var contact: Contact? = nil
// initialize the core data context:
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// MARK: - Outlets
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phoneField: UITextField!
@IBOutlet weak var categoryField: UITextField!
// MARK: - Actions
@IBAction func savebtn(sender: AnyObject) {
let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context!)
let newContact = Contact(entity: entity!, insertIntoManagedObjectContext: context)
newContact.name = nameField.text
newContact.email = emailField.text
newContact.phone = phoneField.text
//newContact.photo = UIImageJPEGRepresentation(imageHolder.image, 1)
var error: NSError?
context?.save(&error)
if let errorSaving = error {
var alert = UIAlertController(title: "Alert", message: "Couldn't save contact !!!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
nameField.text = ""
emailField.text = ""
phoneField.text = ""
var alert = UIAlertController(title: "Notification", message: "Contact added", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = contact?.name
if contact != nil {
nameField.text = contact?.name
emailField.text = contact?.email
phoneField.text = contact?.phone
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func doneEditing() {
}
}
推荐答案
将目标从 self 更改为 destinationVC.
change target from self to destinationVC.
使用这个:
var rightAddBarButtonItem:UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: destinationVC, action: "doneEditing:")
self
应该在选择器定义在进行调用的同一个类中时使用.在这种情况下,选择器位于一个单独的类中.
self
should be used when the selector is defined in the same class which makes the call. In this case the selector is in a separate class.
或
我建议您在 NewCategoryViewController 的 viewDidLoad 方法中添加右侧栏按钮.在这种情况下,代码将是:
I would suggest you to add the right bar button in the viewDidLoad method of NewCategoryViewController. In which case the code will be:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneEditing:")
AND将 doneEditing:
方法实现为
func doneEditing(sender: UIBarButtonItem) {
}
这篇关于将导航栏项目按钮设置为新标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!