基本上,我的应用程序中有两个ViewController。 FirstViewController包含一个UITextField,UILabel和一个UITextView。当用户通过UITextView执行LongPressGesture时,弹出的SecondViewController将显示一个表,其中包含用户选择其中一个选项的选项。当用户触摸屏幕上的任何地方,而不是在管道连接的SecondViewController中显示的表格时,SecondViewController会消失。
我想要实现的是,只要SecondViewController消失,我想要一个变量从SecondViewController传递到FirstViewController,特别是传递给UITextField。
我尝试做这项工作的方式如下:
(1)我在SecondViewController中定义了一个Protocol,如下所示:
import UIKit
protocol MyProtocol {
func setResultOfBusinessLogic(valueSent: String) // valueSent represents the Variable that we will send from the SecondViewController to the FirstViewController.
}
(2)我在SecondViewController中定义了一个变量,它将保存传递给FirstViewController的值,当SecondViewController的弹出窗口消失时:
class IndicativeDesignWorkingLifeVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
var delegate:MyProtocol? // We will also create a variable of type MyProtocol which will be accessed from FirstViewController.
}
(3)接下来,在ViewWillDisappear函数下面的SecondViewController中,我执行了以下逻辑,需要执行该逻辑,其结果将作为参数发送到bullet-point(1)中定义的setResultOfBusinessLogic函数:
override func viewWillDisappear(_ animated: Bool) {
// Next, we will add some business logic that needs to be performed inside of viewWillAppear function and the result of it will be sent as an argument to setResultOfBusinessLogic function.
let firstName = "Sergey"
let lastName = "Kargopolov"
let fullName = firstName + " " + lastName
delegate?.setResultOfBusinessLogic(valueSent: fullName)
}
(4)我使FirstViewController符合我之前在SecondViewController中创建的协议,如下所示:
class ViewController: UIViewController, UITextViewDelegate, UIPopoverPresentationControllerDelegate, MyProtocol {
}
(5)我在FirstViewController中创建了一个Variable,它将保存从SecondViewController传递的值,如下所示:
class ViewController: UIViewController, UITextViewDelegate, UIPopoverPresentationControllerDelegate, MyProtocol {
var valueSentFromSecondViewController:String?
}
(6)我在FirstViewController的末尾添加了以下函数,以确保FirstViewController符合我之前在SecondViewController内创建的协议:
func setResultOfBusinessLogic(valueSent: String) {
if valueSentFromSecondViewController == valueSent {
testTextField.text = valueSentFromSecondViewController
}
}
(7)最后,我在FirstViewController内部函数下面添加了以下代码,当在UITextView中检测到LongPressGesture时,它将SecondViewController实例化为弹出窗口:
func longpressGestureTriggeredForIndicativeDesignWorkingLifeTextView (gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.began {
self.view.endEditing(true)
}
let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IndicativeDesignWorkingLifeVC")
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "IndicativeDesignWorkingLifeVC") as! IndicativeDesignWorkingLifeVC
secondViewController.delegate = self
popController.modalPresentationStyle = UIModalPresentationStyle.popover
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = (indicativeDesignWorkingLifeTextView!)
popController.popoverPresentationController?.sourceRect = CGRect(x: (indicativeDesignWorkingLifeTextView.contentSize.width)/2 , y: (indicativeDesignWorkingLifeTextView.contentSize.height), width: 0, height: 0) // this line of code controls the location of the popover arrow head. In terms of its horizontal as well as vertical position from where it is going to popup, in this case from the UITextView object.
self.present(popController, animated: true, completion: nil)
}
但是,有了上述所有内容,当弹出的SecondViewController我没有看到SecondViewController中的参数(即fullName)显示在FirstViewController的UITextField中时。
有谁能指导我做错了什么?为什么它不起作用?我在这里错过了什么吗?
谢谢, 沙迪。