将导航栏项目按钮设置为新标题

Set navbar item button to new title

美好的一天。我遇到了一个奇怪的问题,我想在我选择一行后将正确的导航项设置为 Done in my next。我试过了,它成功了。但它正在中断,因为实现 doneEditing body 的函数仅在下一个视图控制器中,任何帮助将不胜感激。这是我的代码:

 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
        }
    }

我的下一个视图控制器是:

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。

使用这个:

var rightAddBarButtonItem:UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: destinationVC, action: "doneEditing:")

self 应该在选择器在进行调用的同一个 class 中定义时使用。在这种情况下,选择器位于单独的 class.

建议你在NewCategoryViewController的viewDidLoad方法中添加右栏按钮。在这种情况下,代码将是:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneEditing:")

doneEditing: 方法实现为

func doneEditing(sender: UIBarButtonItem) {

}