gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:58:17 25 4
gpt4 key购买 nike

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

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

我的下一个 View Controller 是:

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 应在选择器定义在进行调用的同一类中时使用。在这种情况下,选择器在一个单独的类中。

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

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

doneEditing: 方法实现为

func doneEditing(sender: UIBarButtonItem) {

}

关于ios - 将导航栏项目按钮设置为新标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740801/

25 4 0