gpt4 book ai didi

ios - 如何将数据从 subview Controller 传递到父 View Controller ? swift

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:40 25 4
gpt4 key购买 nike

我创建了一个事件,当单击其中一个文本字段时,它会弹出一个带有产品列表的子项(警告对话框),但是当我单击列表中的一个项目时,我无法在提交的文本中显示它警报已解除。

这是父 View

import Foundation
import UIKit

class ViewAward: UIViewController{

@IBOutlet var tfMCN: UITextField!
@IBOutlet var tfAmount: UITextField!
@IBOutlet var tfProduct: UITextField!
@IBOutlet var tfTotal: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

let rightAddBarButtonItem:UIBarButtonItem = UIBarButtonItem(title: "Send", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewAward.searchTapped))

self.navigationItem.setRightBarButtonItems([rightAddBarButtonItem], animated: true)

let state = String(ViewPopUpProduct.Product.ProductDescription)
print("My view state:"+state)

self.tfProduct.text = state
tfProduct.addTarget(self, action: #selector(ViewAward.productTapped), forControlEvents: UIControlEvents.TouchDown)

}

func searchTapped(sender:UIButton) {

let alertController = UIAlertController(
title: "Award",
message:"Award successfully posted!",
preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))

self.presentViewController(alertController, animated: true, completion: nil)
}

func productTapped(textfield: UITextField){

//tfProduct.endEditing(true)
tfProduct.resignFirstResponder()

let popOverVC = UIStoryboard(name:"Main",bundle:nil).instantiateViewControllerWithIdentifier("sbPopUpID") as! ViewPopUpProduct

self.addChildViewController(popOverVC)

popOverVC.view.frame = self.view.frame

self.view.addSubview(popOverVC.view)

popOverVC.didMoveToParentViewController(self)

}
}

当用户点击这些项目时

import UIKit

class ViewPopUpProduct: UIViewController {

@IBOutlet var tableView: UITableView!

var productDescription = ["Product 1","Product 2","Product 3"]
var productID = ["prdct1","prdct2","prdct3"]


// Global Variables
struct Product {
static var ProductID = String()
static var ProductDescription = String()
}

override func viewDidLoad() {
super.viewDidLoad()
self.showAnimate()
self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)

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

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

@IBAction func cancelPopUp(sender: AnyObject) {
self.removeAnimate()
}


func showAnimate()
{
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
self.view.alpha = 0.0;
UIView.animateWithDuration(0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
});
}

func removeAnimate()
{
UIView.animateWithDuration(0.25, animations: {
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
}
});
}

//Mark - Table View

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.productID.count
}

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

cell.productLabel.text = productDescription[indexPath.row]

return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

tableView.deselectRowAtIndexPath(indexPath, animated: true)

Product.ProductID = String(productID[indexPath.row])
Product.ProductDescription = String(productDescription[indexPath.row])

self.removeAnimate()

}

}

最佳答案

我通常为此目的使用闭包。比委托(delegate)更简单、更简洁:

class MainViewController: UIViewController {

func showChildViewController() {
guard let vc = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as? ChildViewController else {
return
}
vc.didSelectItem = { [weak self](item) in
if let vc = self {
// Do something with the item.
}
}
presentViewController(vc, animated: true, completion: nil)
}

}

class ChildViewController: UIViewController {

var didSelectItem: ((item: Item) -> Void)?

@IBAction func buttonPressed() {
didSelectItem?(item: <#your item goes here#>)
}

}

关于ios - 如何将数据从 subview Controller 传递到父 View Controller ? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285588/

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