将数据从视图控制器传递到具有委托的视图控制器

问题描述:

试图将数据从一个视图控制器(从alamofire请求)发送到导航控制器中的下一个视图控制器。将数据从视图控制器传递到具有委托的视图控制器

我试着与委托,但我不明白它的工作。我已经知道这不是这样,但我需要找到解决方案来实现它。

请参见下面的代码,从发送variabels视图控制器:

protocol SendDataToScanInfo { 
    func sendData (vendorname01 : String, productname01: String, productstatus01: String, productdescription01: String) 
} 

class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate, CLLocationManagerDelegate{ 

var delegate:SendDataToScanInfo? 

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { 

Alamofire.request(URL_SCAN_ID, method: .post, parameters: ScanParameters, encoding: JSONEncoding.default) .responseJSON 
       { 

        response in 

        //printing response 
        print(response.request!) 
        print(response.response!) 
        print(response.data!) 
        print(response.result) 
        print(response.error) 

        //getting the json value from the server 
        let value = response.result.value 
        print(value!) 
        let json = JSON(value!) 

        let productdesc0:JSON = json["productdesc"] 
        let productdescString = productdesc0.string 


        let productname0:JSON = json["productname"] 
        let productnameString = productname0.string 


        let tagstate0:JSON = json["tagstate"] 
        let tagstateString = tagstate0.string 


        let vendorname0:JSON = json["vendorname"] 
        let vendornameString = vendorname0.string 


        //self.performSegue(withIdentifier: "ScanInfo", sender: productdescString) 
        self.delegate?.sendData(vendorname01: vendornameString!, productname01: productnameString!, productstatus01: tagstateString!, productdescription01: productdescString!) 




        print(vendornameString) 
       } 
      if code != nil 
      { 
       let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) 
       let destination = mainStoryboard.instantiateViewController(withIdentifier: "ScanInfo") 
       navigationController?.pushViewController(destination, animated: true) 
      } 

      captureSession.stopRunning(); 
      //self.dismiss(animated: true, completion: nil) 
     } 
    } 
} 

下一个视图控制器应该接受它:

class ScanInfoViewController: UIViewController, SendDataToScanInfo { 



    @IBOutlet weak var Vendor: UILabel! 
    @IBOutlet weak var VendorScan: UILabel! 
    @IBOutlet weak var Product: UILabel! 
    @IBOutlet weak var ProductScan: UILabel! 
    @IBOutlet weak var Status: UILabel! 
    @IBOutlet weak var DescriptionScan: UILabel! 
    @IBOutlet weak var Description: UILabel! 
    @IBOutlet weak var StatusScan: UILabel! 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     DescriptionScan.text = descriptionBLA 
     print("jddjd", descriptionBLA) 
     let URL_SCAN_INFO = "http://makeitrain.get-legit.com:8998/checktag" 

     // Do any additional setup after loading the view. 

    } 


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

    func sendData(vendorname01: String, productname01: String, productstatus01: String, productdescription01: String) { 
     VendorScan.text = vendorname01 
     ProductScan.text = productname01 
     DescriptionScan.text = productdescription01 
     StatusScan.text = productstatus01 
     print("MMMM", StatusScan.text) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "ScanInfo" { 
      let sendingVC: ScanController = segue.destination as! ScanController 
      sendingVC.delegate = self 
     } 
    } 
} 

我希望有人能帮帮我!

+0

您可以注入服务呼叫。信息控制器不是目的地吗?如果是这样,你为什么重写Segue方法呢? –

+0

[视图控制器之间传递数据]的可能的复制(https://*.com/questions/5210535/passing-data-between-view-controllers)来传递视图控制器之间的数据 –

+0

最简单的方法,我发现是与应用程序委托试检查我的这个环节上为你解答https://*.com/questions/44877352/passing-data-from-one-view-controller-to-another/44877517#44877517 –

做到这一点的最佳方法是使用segue。在控制器之间连接一个继电器,并在prepareForSegue中添加一个变量,该变量代表您正在继续控制的控制器:let viewController = segue.destination as! viewController。现在,您可以使用viewController.variable访问并更改viewController中的变量。

+0

耶士谢谢你也许有帮助!解决它与赛格! –

为了传递数据,就像williej926所说,segues是要走的路。要将数据从一个视图控制器传递到另一个视图控制器,需要在这两个视图控制器之间创建一个segue,并在您的项目中存在多个用于传递数据的segue时为segue提供一个标识符,那么这是必须的。在您的第一个视图控制器的类中,您应该使用内置的方法创建一个prepareForSegue方法。在这个prepareForSegue方法中,如果segue的标识符等于您在故事板中设置的标识符,则编写该方法。在这个if语句中,你需要告诉这个viewcontroller你的segue的目标是什么。要这样写let destination = segue.destination as! nextViewControllerClass.要访问变量并在第二个视图控制器中设置它们,请编写destination.variableName = thisVariableName。下面是一个例子,向您展示了这个代码中的纯粹外观。

在第一个视图控制器类

class FirstViewController: UIViewController { 

var thisString: String? 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
      if let identifier = segue.identifier { 
       if(identifier == "secondViewController") { 
        let destination = segue.destination as! SecondViewController//SecondViewController is second view controller's class 
        destination.myString = thisString 
      } 
     } 
    } 
} 

第二个视图控制器类

class SecondViewController: UIViewController { 
    var myString: String?//this will equal to thisString in FirstViewController 
} 
+0

谢谢你!解决它与赛格! –

wrote an answer这个不是很久以前:

其中一个更简单的方法通过 从一个VC到另一个VC的信息要么是通过initiliazer,要么是通过在提交第二个VC之前设置的变量。

该secone方法会让你通过一个委托,主要是当数据返回到最初的VC。无论哪种方式,你需要一个类似的设置:

class LoggedInVCViewController : UIViewController { 

    var info : String? { 
     didSet { 
      if let newInfo = self.info { 
      //do what ever you need to do here 
      } 
     } 
    } 

    override viewDidLoad() { 
     super.viewDidLoad() 

    } 

} 

func presentLoggedInScreen(yourInfo: String) { 
    let stroyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let loggedInVC:LoggedInVCViewController = 
    storyboard.instantiateViewController(withIdentifier: "loggedInVC") as! 
    LoggedInVCViewController 
    loggedInVC.info = yourInfo 
    self.present(loggedInVC, animated: true, completion: nil) 
} 
class LoggedInVCViewController : UIViewController { 

    var info : Any? { 
     get { 
      if let this = self.info { 
       return this 
      } else { 
       return nil 
      } 
     } set { 
      if let new = newValue { 
       // 
      } 
     } 
    } 

    init(info: Any?) { 
     //This first line is key, it also calls viewDidLoad() internally, so don't re-write viewDidLoad() here!! 
     super.init(nibName: nil, bundle: nil) 

     if let newInfo = info { 
      //here we check info for whatever you pass to it 
      self.info = newInfo 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 

然后使用:

func presentLoggedInScreen(yourInfo: String) { 
     let loggedInVC = LoggedInVCViewController(info: yourInfo) 
     self.present(loggedInVC, animated: true, completion: nil) 
} 

或者,如果你使用的变量的方法:

func presentLoggedInScreen(yourInfo: String) { 
     let loggedInVC = LoggedInVCViewController() 
     loggedInVC.info = yourInfo 
     self.present(loggedInVC, animated: true, completion: nil) 
} 

我还要继续,并链接到其他文章,讨论使用Storyboards的注意事项,以及定制初始化工具以传递数据。我也读过他们!