[英]How can I pass data without using segues from programmatically created UIPopoverController to ViewController using delegates and protocols?
I have an issue that I couldn't manage how to do. 我有一个无法解决的问题。 The issue is that I have programmatically created UIPopoverController calling from FirstViewController shown below.
问题是我已经以编程方式创建了从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)
}
And passCustomer delegate method defination in FirstViewController ; 并且在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
}
And AddCustomerPopoverVC shown below ; 并且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.
}
}
When I tapped to my save button it does nothing. 当我点击保存按钮时,它什么也没做。 I mean it does sth.
我的意思是…… but it does nothing about the passing value process.
但是它对传递价值过程没有任何作用。 What should I do for the complete this process.
我应该怎么做才能完成此过程。 What am I doing wrong or missed ?
我在做什么错还是错过了?
I've already check the link below but I can't say that I've got it clearly.. Trying to understand protocol/delegates in Swift 我已经检查了下面的链接,但我不能说清楚了。. 试图了解Swift中的协议/代理
I'm pretty sure you need to assign delegate
of popView
and not popController
: 我很确定您需要分配
popView
delegate
而不是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)
}
Also it's a good practice to call optional delegates methods like this (notice question mark instead of exclamation): 最好调用这样的可选委托方法(注意问号而不是感叹号):
delegate?.passCustomer(customer)
So if delegate is nil app won't crash. 因此,如果委托为零,应用程序将不会崩溃。 If you are completely sure that delegate will never be nil then you better declare it as
var delegate : PassValues!
如果您完全确定委托永远不会为零,那么最好将其声明为
var delegate : PassValues!
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.