gpt4 book ai didi

ios - 使用委托(delegate)/协议(protocol)将数据传递给第三方 View Controller

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:50 26 4
gpt4 key购买 nike

我有 3 个 View Controller :http://i58.tinypic.com/2envu2x.png

View Controller 1 是第一个,它继续到 View Controller 2。 View Controller 3 是 View Controller 2 的 subview ,因为 View Controller 2 中有一个容器 View 。我需要将数据从 View Controller 1 传递到 View Controller 3。使用传统的委托(delegate)和协议(protocol),我必须实际转至 View Controller 3 才能将数据传递给它。但是 segue 是从第一个 View Controller 到容器 View (第二个 View Controller ),而不是第三个。我如何修改委托(delegate)/协议(protocol)来实现这一点?

这是我的第一个 View Controller 的代码(它有一个表格 View ,所以我将协议(protocol)放在表格单元格中):

import UIKit

protocol DataEnteredDelegate {
func userDidCHooseClass(classChose: String)
}

class FirstTableViewCell: UITableViewCell {

var delegate:DataEnteredDelegate?
@IBAction func buttonTapped(sender: AnyObject) {

let string = "che107"
self.delegate?.userDidCHooseClass(string)
}




override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

这是 View Controller 3 的代码,我想接收字符串的 View Controller :

  import UIKit

class SecondTBC: UITableViewController, DataEnteredDelegate {

var stringThing = String()

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()




}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 0
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! firstCell



return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue1" {
var otherController = FirstTableViewCell()
otherController.delegate = self
}
}


func userDidCHooseClass(classChose: String) {
stringThing = classChose
}

最佳答案

要在两个未连接的 View Controller 之间传递数据,您需要使用:

presentingViewController!.dismissViewControllerAnimated(true, completion: nil)

并像这样通过 viewWillDisappear 传输您的数据:

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if self.isBeingDismissed() {
self.delegate?.acceptData(textFieldOutlet.text)
}
}

我已经发布了 a tutorial ,其中包括一个您可以下载和检查的工作项目文件。


这是上下文中的模式示例。

View Controller 2:

// place the protocol in the view controller that is being presented
protocol PresentedViewControllerDelegate {
func acceptData(data: AnyObject!)
}

class PresentedViewController: UIViewController {
// create a variable that will recieve / send messages
// between the view controllers.
var delegate : PresentedViewControllerDelegate?
// another data outlet
var data : AnyObject?


@IBOutlet weak var textFieldOutlet: UITextField!
@IBAction func doDismiss(sender: AnyObject) {
if textFieldOutlet.text != "" {
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
}


override func viewDidLoad() {
super.viewDidLoad()
print("\(data!)")

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if self.isBeingDismissed() {
self.delegate?.acceptData(textFieldOutlet.text)
}
}

}

View Controller 1:

class ViewController: UIViewController, PresentedViewControllerDelegate {

@IBOutlet weak var textOutlet: UILabel!
@IBAction func doPresent(sender: AnyObject) {
let pvc = storyboard?.instantiateViewControllerWithIdentifier("PresentedViewController") as! PresentedViewController

pvc.data = "important data sent via delegate!"
pvc.delegate = self
self.presentViewController(pvc, animated: true, completion: nil)
}

override func viewDidLoad() {
super.viewDidLoad()

}

func acceptData(data: AnyObject!) {
self.textOutlet.text = "\(data!)"

}
}

关于ios - 使用委托(delegate)/协议(protocol)将数据传递给第三方 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com