<i id='J1SkG'><tr id='J1SkG'><dt id='J1SkG'><q id='J1SkG'><span id='J1SkG'><b id='J1SkG'><form id='J1SkG'><ins id='J1SkG'></ins><ul id='J1SkG'></ul><sub id='J1SkG'></sub></form><legend id='J1SkG'></legend><bdo id='J1SkG'><pre id='J1SkG'><center id='J1SkG'></center></pre></bdo></b><th id='J1SkG'></th></span></q></dt></tr></i><div id='J1SkG'><tfoot id='J1SkG'></tfoot><dl id='J1SkG'><fieldset id='J1SkG'></fieldset></dl></div>
      <tfoot id='J1SkG'></tfoot>
      • <bdo id='J1SkG'></bdo><ul id='J1SkG'></ul>

        <legend id='J1SkG'><style id='J1SkG'><dir id='J1SkG'><q id='J1SkG'></q></dir></style></legend>

        <small id='J1SkG'></small><noframes id='J1SkG'>

      1. 两个没有 segue 的 VC 之间的 Swift 委托

        Swift delegate beetween two VC without segue(两个没有 segue 的 VC 之间的 Swift 委托)

          1. <i id='etDnb'><tr id='etDnb'><dt id='etDnb'><q id='etDnb'><span id='etDnb'><b id='etDnb'><form id='etDnb'><ins id='etDnb'></ins><ul id='etDnb'></ul><sub id='etDnb'></sub></form><legend id='etDnb'></legend><bdo id='etDnb'><pre id='etDnb'><center id='etDnb'></center></pre></bdo></b><th id='etDnb'></th></span></q></dt></tr></i><div id='etDnb'><tfoot id='etDnb'></tfoot><dl id='etDnb'><fieldset id='etDnb'></fieldset></dl></div>
              <tbody id='etDnb'></tbody>
            <legend id='etDnb'><style id='etDnb'><dir id='etDnb'><q id='etDnb'></q></dir></style></legend>
            • <bdo id='etDnb'></bdo><ul id='etDnb'></ul>

                <tfoot id='etDnb'></tfoot>

                  <small id='etDnb'></small><noframes id='etDnb'>

                  本文介绍了两个没有 segue 的 VC 之间的 Swift 委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 3 个课程:

                  • 聊天记录控制器
                  • GetImageFromLibraty(NSObject 类)
                  • ImagePreviewViewController

                  我想从第一个 VC 中按下一个剪辑,然后打开媒体库来选择一个图像.然后将选中的图像作为 previewController 传递给第三个 VC.然后,如果我选择完成",我想将其传递给第一个 VC.

                  I want to press a clip from the first VC, then open the media library to pick an image. Then the selected image is passed to the third VC as a previewController. Then if I select 'done' I want to pass it to the first VC.

                  第一个 VC

                  class ChatLogControoller: UICollectionViewController, UICollectionViewDelegateFlowLayout, NSFetchedResultsControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, DataSentDelegate {
                  
                  func recievePhoto(data: UIImage) {
                      imageFromView = data
                      print("-------(imageFromView = data)")
                  }
                  
                  override func viewDidLoad() {
                      super.viewDidLoad()
                  
                      let vc = ImagePreviewController()
                      self.vc.delegate = self
                  }
                  

                  第二类它只是图像的选择器,所以我将图像传递给第三个 VC,这个图像成功出现在第三个 VC 的 imageView 上!

                  2nd class its just picker of image, so i pass image to 3rd VC and this image appears on imageView of 3rd VC successfully!

                  我的第三个 VC

                  protocol DataSentDelegate {
                      func recievePhoto(data: UIImage)
                  }
                  class PreviewController: UIViewController, UIScrollViewDelegate {
                  
                  var delegate : DataSentDelegate? = nil
                  
                  var aImageView: UIImageView!
                  var aImage: UIImage!
                  
                  override func viewDidLoad() {
                      super.viewDidLoad()
                  
                      navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(actionSend))
                      navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(actionBack))
                  
                  }
                  
                  @objc func actionBack() {
                  
                      dismiss(animated: false, completion: nil)
                  }
                  
                  @objc func actionSend() {
                  
                      let data = aImageView.image
                      delegate?.recievePhoto(data: data!)
                      dismiss(animated: true, completion: nil)
                  }
                  

                  推荐答案

                  您需要在 SecondViewController 中再创建一个协议,以将该委托从 ThirdViewController 传递给 FirstViewController.

                  You need to create one more protocol in your SecondViewController to Pass that delegate from ThirdViewController to FirstViewController.

                  FirstViewController:

                  import UIKit
                  
                  class ViewController: UIViewController, DataSentDelegate, dataSentDelegate  {
                  
                      @IBOutlet weak var imagefromThirdVC: UIImageView!
                  
                      var thirdVCImage: UIImage!
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                      }
                  
                      @IBAction func buttonTapped(_ sender: Any) { 
                          let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
                          vc.delegate = self
                          self.navigationController?.pushViewController(vc, animated: true)
                      }
                  
                      func goToThirdVC() {
                          let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
                          vc.delegate = self
                          self.navigationController?.pushViewController(vc, animated: true)
                      }
                  
                      func recievePhoto(data: UIImage) {
                          thirdVCImage = data
                          imagefromThirdVC.image = thirdVCImage
                      }
                  }
                  

                  SecondViewController:

                  import UIKit
                  
                  protocol dataSentDelegate {
                      func goToThirdVC()
                  }
                  
                  class ViewController2: UIViewController {
                  
                      @IBOutlet weak var passingImage: UIImageView!
                  
                      var delegate: dataSentDelegate? = nil
                  
                      var images: UIImage!
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          images = UIImage(named: "screen")
                      }
                  
                      @IBAction func actionButton(_ sender: Any) {
                          self.delegate?.goToThirdVC()
                      }
                  
                  }
                  

                  ThirdViewController:

                  import UIKit
                  
                  protocol DataSentDelegate {
                      func recievePhoto(data: UIImage)
                  }
                  
                  class ViewController3: UIViewController {
                  
                      var delegate: DataSentDelegate? = nil
                  
                      @IBOutlet weak var passedImageView: UIImageView!
                  
                      var passedImage: UIImage!
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          passedImage = UIImage(named: "screen")
                          passedImageView.image = passedImage
                      }
                  
                      @IBAction func action(_ sender: Any) {
                  
                          let data = passedImageView.image
                          delegate?.recievePhoto(data: data!)
                          //   delegate?.goToFirstVC()
                  
                          guard let viewControllers = self.navigationController?.viewControllers else {
                              return
                          }
                  
                          for firstViewController in viewControllers {
                              if firstViewController is ViewController {
                                  self.navigationController?.popToViewController(firstViewController, animated: true)
                                  break
                              }
                          }
                  
                      }
                  
                  }
                  

                  这篇关于两个没有 segue 的 VC 之间的 Swift 委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  How to format localised strings in Swift?(如何在 Swift 中格式化本地化字符串?)
                  Why do we need to set delegate to self? Why isn#39;t it defaulted by the compiler?(为什么我们需要将委托设置为自我?为什么编译器不默认它?)
                  set delegate and datasource of uitableview programmatically in Swift(在 Swift 中以编程方式设置 uitableview 的委托和数据源)
                  How can I wait for a NSURLConnection delegate to finish before executing the next statement?(在执行下一条语句之前,如何等待 NSURLConnection 委托完成?)
                  Swift can#39;t call protocol method via delegate(Swift 不能通过委托调用协议方法)

                    <tbody id='wzpR9'></tbody>
                  <legend id='wzpR9'><style id='wzpR9'><dir id='wzpR9'><q id='wzpR9'></q></dir></style></legend>

                  1. <tfoot id='wzpR9'></tfoot>

                            <bdo id='wzpR9'></bdo><ul id='wzpR9'></ul>
                          • <i id='wzpR9'><tr id='wzpR9'><dt id='wzpR9'><q id='wzpR9'><span id='wzpR9'><b id='wzpR9'><form id='wzpR9'><ins id='wzpR9'></ins><ul id='wzpR9'></ul><sub id='wzpR9'></sub></form><legend id='wzpR9'></legend><bdo id='wzpR9'><pre id='wzpR9'><center id='wzpR9'></center></pre></bdo></b><th id='wzpR9'></th></span></q></dt></tr></i><div id='wzpR9'><tfoot id='wzpR9'></tfoot><dl id='wzpR9'><fieldset id='wzpR9'></fieldset></dl></div>

                          • <small id='wzpR9'></small><noframes id='wzpR9'>