我有一个无法解决的问题。问题是我已经以编程方式创建了从FirstViewController调用的UIPopoverController,如下所示。
@IBAction func addButton(sender: UIButton) {
let popoverRect = CGRectMake(-360.0, 170.0, 515.0, 415.0)
var popView = AddCustomerPopoverVC (nibName :"AddCustomerPopoverVC" , bundle : nil)
var popController = UIPopoverController (contentViewController: popView)
popController.delegate = self
popController.popoverContentSize = CGSize(width: 515.0 , height: 415.0)
popController.presentPopoverFromRect(popoverRect , inView: self.view , permittedArrowDirections: UIPopoverArrowDirection.allZeros, animated: true)
}
并且在FirstViewController中通过passCustomer委托方法定义;
func passCustomer(customer: Customer) {
self.profilePhoto.image = UIImage(named: "circleBlue.png") as UIImage?
self.usernameLabel.text = customer.name
self.eMailLabel.text = customer.eMail
}
并且AddCustomerPopoverVC如下所示;
import UIKit
import Foundation
protocol PassValues {
func passCustomer(customer : Customer )
}
class AddCustomerPopoverVC: UIViewController {
var delegate : PassValues? = nil
@IBOutlet weak var navBar: UINavigationBar!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var surnameTextField: UITextField!
@IBOutlet weak var eMailTextField: UITextField!
@IBOutlet weak var phoneTextField: UITextField!
@IBOutlet weak var advSwitchOutlet: UISwitch!
@IBOutlet weak var taxFreeSwitchOutlet: UISwitch!
@IBAction func saveButton(sender: UIBarButtonItem) {
var cName = "\(self.nameTextField.text) \(self.surnameTextField.text)"
var cEmail = self.eMailTextField.text
var cPhone = self.phoneTextField.text
var cAdDelivery = self.advSwitchOutlet.on ? true : false
var cTaxFree = self.taxFreeSwitchOutlet.on ? true : false
let customer : Customer = Customer(name: cName, eMail: cEmail, phone: cPhone, adDelivery: cAdDelivery, taxFree: cTaxFree)
delegate!.passCustomer(customer)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
当我点击我的保存按钮时,它什么也没做。我的意思是……但是它对传递价值过程没有任何作用。我应该怎么做才能完成此过程。我在做什么错还是错过了?
我已经检查了下面的链接,但我不能说清楚了。
Trying to understand protocol/delegates in Swift
最佳答案
我很确定您需要分配delegate
的popView
而不是popController
:
@IBAction func addButton(sender: UIButton) {
let popoverRect = CGRectMake(-360.0, 170.0, 515.0, 415.0)
var popView = AddCustomerPopoverVC (nibName :"AddCustomerPopoverVC" , bundle : nil)
popView.delegate = self
var popController = UIPopoverController (contentViewController: popView)
popController.delegate = self
popController.popoverContentSize = CGSize(width: 515.0 , height: 415.0)
popController.presentPopoverFromRect(popoverRect , inView: self.view , permittedArrowDirections: UIPopoverArrowDirection.allZeros, animated: true)
}
最好调用这样的可选委托方法(注意问号而不是感叹号):
delegate?.passCustomer(customer)
因此,如果委托为零,应用程序将不会崩溃。如果您完全确定委托永远不会为零,那么最好将其声明为
var delegate : PassValues!
。关于ios - 如何在不使用委托(delegate)和协议(protocol)的情况下将数据从编程创建的UIPopoverController传递给ViewController的情况下传递数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32459108/