Ios 如何将文本字段数据从第三个viewcontroller传递到第一个viewcontroller
我想将Ios 如何将文本字段数据从第三个viewcontroller传递到第一个viewcontroller,ios,iphone,uiviewcontroller,delegates,Ios,Iphone,Uiviewcontroller,Delegates,我想将ViewControllerC中textfield中的文本发送到ViewControllerA 通过使用代理,我试图将文本从ViewControllerC传递到ViewControllerA 我无法在ViewControllerC 有人能帮我吗 ViewControllerC protocol DataEnteredInDestinationDelegate: class { func userDidEnterInformation(info: String) } class D
ViewControllerC
中textfield中的文本发送到ViewControllerA
通过使用代理,我试图将文本从ViewControllerC
传递到ViewControllerA
我无法在ViewControllerC
有人能帮我吗
ViewControllerC
protocol DataEnteredInDestinationDelegate: class {
func userDidEnterInformation(info: String)
}
class DestinationSearchViewController: MirroringViewController {
var delegate: DataEnteredInDestinationDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell: UITableViewCell? = tableView.cellForRow(at: indexPath)
componetsTextField.text = cell?.textLabel?.text
delegate?.userDidEnterInformation()
self.navigationController?.popToRootViewController(animated: true)
}
}
ViewControllerA
class HomeViewController: MirroringViewController, DataEnteredInDestinationDelegate
{
func userDidEnterInformation(info: String){
locationView.destination.text = info
}
}
此外,还应设置ViewControllerC
的委托
viewControllerC.delegate = viewControllerA
首先,您必须始终将代表标记为弱,例如:
weak var delegate: DataEnteredInDestinationDelegate?
然后,您需要像这样连接代理:
let vcA = ViewControllerA()
let vcC = ViewControllerC()
vcC.delegate = vcA // Connect delegate
调用以下代码后,ViewControllerC中的委托方法将起作用:
delegate?.userDidEnterInformation(textString)
在这里,通知中心可以代替代表成为一个很好的方法。使Viewcontroller成为观察者以接收文本信息,如下所示 在viewDidLoad()中编写此代码 并在类Viewcontroller A中的任意位置编写此代码
func userDidEnterInformation(notification: Notification) {
if let textInfo = notification.userInfo?["textInfo"] {
textField.text = textInfo
}
}
在Viewcontroller C中,通过编写以下代码,使用textInfo发布通知
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "UserDidEnterInformation"), object: nil, userInfo: ["textInfo": textField.text])
考虑以下示例:-
let aVCobjA = UIViewController()
let aVCobjB = UIViewController()
let aVCobjC = UIViewController()
var aNavigation = UINavigationController()
func pushVC() {
aNavigation.pushViewController(aVCobjA, animated: true)
aNavigation.pushViewController(aVCobjB, animated: true)
aNavigation.pushViewController(aVCobjC, animated: true)
//Here you will get array of ViewControllers in stack of Navigationcontroller
print(aNavigation.viewControllers)
//To pass data from Viewcontroller C to ViewController A
self.passData()
}
// To pass data access stack of Navigation Controller as navigation controller provides a property viewControllers which gives you access of all view controllers that are pushed.
func passData() {
let aVCObj3 = aNavigation.viewControllers.last
let aVCObj1 = aNavigation.viewControllers[0]
//Now you have access to both view controller pass whatever data you want to pass
}
@amma teja希望使用委托模式传递数据根据我的要求,我编写了如下代码func userDidEnterInformation(notification:notification){if let textInfo=notification.userInfo?[“textInfo”]{locationView.destination.text=textInfo}我在locationView.destination.text=textInfo中遇到错误,显示“无法将任何类型的值分配给类型字符串”@ammateja convert textInfo to string。文本信息作为?string此代码不起作用。如果我在ViewControllerC中写这个,它根本不属于ViewControllerC您是否正在使用视图控制器A和视图控制器C的导航控制器?@Leena am现在正在使用从viewController A到viewController B的navigationController和从viewController B到viewController C的navigationController,同时从viewController C到viewController A,我正在使用PoptorootViewController当您可以从navigationcontrolleri的堆栈访问视图控制器A时,我在viewcontrollerC中编写了此代码我需要如何调用viewcontrollerA?
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "UserDidEnterInformation"), object: nil, userInfo: ["textInfo": textField.text])
let aVCobjA = UIViewController()
let aVCobjB = UIViewController()
let aVCobjC = UIViewController()
var aNavigation = UINavigationController()
func pushVC() {
aNavigation.pushViewController(aVCobjA, animated: true)
aNavigation.pushViewController(aVCobjB, animated: true)
aNavigation.pushViewController(aVCobjC, animated: true)
//Here you will get array of ViewControllers in stack of Navigationcontroller
print(aNavigation.viewControllers)
//To pass data from Viewcontroller C to ViewController A
self.passData()
}
// To pass data access stack of Navigation Controller as navigation controller provides a property viewControllers which gives you access of all view controllers that are pushed.
func passData() {
let aVCObj3 = aNavigation.viewControllers.last
let aVCObj1 = aNavigation.viewControllers[0]
//Now you have access to both view controller pass whatever data you want to pass
}