Passing data from one view controller to another

前端 未结 3 385
忘掉有多难
忘掉有多难 2021-01-28 06:37

I\'m parsing data with json, and i want to pass model_id to ActiveSubViewController / id_model. What am i doing wrong?

//  ViewController.swift
//
//  Copyright          


        
相关标签:
3条回答
  • 2021-01-28 07:14

    Do make a separate property var model_id : String = "" in the starting of your class and then use the prepare function outside your LoginBtnTapped function :

    Here it is :

          //  ViewController.swift
        //
        //  Copyright © 2017 smokzz. All rights reserved.
        //
    
        import UIKit
        import SwiftMessages
        import Alamofire
    
    
    
        class ViewController: UIViewController,UIAlertViewDelegate {
    
            @IBOutlet var myTableView: UIView!
            @IBOutlet weak var EmailField: UITextField!
            @IBOutlet weak var PasswordField: UITextField!
    
             var model_id : String = ""
    
            @IBAction func LoginBtnTapped(_ sender: Any) {
    
                let userEmail = EmailField.text!
                let userPassword = PasswordField.text!
    
    
    
                if((userEmail.isEmpty) || (userPassword.isEmpty)){
    
        //            createAlert(title: "Oops! ", message: "Email or Password field is empty")
    
                    let view = MessageView.viewFromNib(layout: .CardView)
                    view.configureTheme(.error)
                    view.configureDropShadow()
                    let iconText = ["                                                                    
    0 讨论(0)
  • 2021-01-28 07:16

    Make string variable in your AppDelegate Like

    var Model_id = String()
    

    and where you are getting value of username and userid sends its value to AppDelegate. make AppDelgate object like below and same in destinationViewController

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    

    to pass value to app delegate variable

    self.appDelegate. Model_id = "your Value"
    

    and get your values wherever you want to fetch like below

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let  string = appDelegate. Model_id as! String
    

    0 讨论(0)
  • 2021-01-28 07:22

    Please Use This Updated Code.

        import UIKit
        import SwiftMessages
        import Alamofire
    
    
    
        class ViewController: UIViewController,UIAlertViewDelegate {
    
            @IBOutlet var myTableView: UIView!
            @IBOutlet weak var EmailField: UITextField!
            @IBOutlet weak var PasswordField: UITextField!
    
            var model_id = ""
    
            @IBAction func LoginBtnTapped(_ sender: Any) {
    
                let userEmail = EmailField.text!
                let userPassword = PasswordField.text!
    
    
    
                if((userEmail.isEmpty) || (userPassword.isEmpty)){
    
                    //            createAlert(title: "Oops! ", message: "Email or Password field is empty")
    
                    let view = MessageView.viewFromNib(layout: .CardView)
                    view.configureTheme(.error)
                    view.configureDropShadow()
                    let iconText = ["                                                                    
    0 讨论(0)
提交回复
热议问题