【问题标题】:Pass Data using a pushViewController?使用 pushViewController 传递数据?
【发布时间】:2017-01-23 10:32:24
【问题描述】:

使用此代码,我可以“继续”到我的视图控制器的同一个实例

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC")
    self.navigationController?.pushViewController(vc, animated: true)


}

但是,我如何传递数据?我只知道如何使用 segue 选项传递数据。当我用这个运行代码时,我得到 nil 错误,因为新的实例化视图控制器无法读取数据。

【问题讨论】:

    标签: ios swift uiviewcontroller segue


    【解决方案1】:

    例如我在这里添加,详细说明可以从here获取教程

    class SecondViewController: UIViewController {
    
    var myStringValue:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    
        // We will simply print out the value here
        print("The value of myStringValue is: \(myStringValue!)")
    }
    

    并将字符串发送为

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! SecondViewController
          vc.myStringValue = "yourvalue"
    
        self.navigationController?.pushViewController(vc, animated: true)
    
    
    }
    

    【讨论】:

    • 非常详细。非常感谢。
    【解决方案2】:

    首先。这不是segue。这只是将另一个视图推送到堆栈。而且(就像 Ashley Mills 所说)这与您当前所在的实例不同。这是视图控制器的新实例。

    但您需要做的就是填充数据。你已经有控制器了……

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        // you need to cast this next line to the type of VC.
        let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC // or whatever it is
        // vc is the controller. Just put the properties in it.
        vc.thePropertyYouWantToSet = theValue
    
        self.navigationController?.pushViewController(vc, animated: true)
    }
    

    然后在你的第二个视图控制器中捕获这样的值

    class DetailVC: UIViewController {
        var thePropertyYouWantToSet = String()
    
        override func viewDidLoad() {
            print(thePropertyYouWantToSet)
        }
    }
    

    【讨论】:

      【解决方案3】:

      您使用的不是segue。这只是将视图控制器的一个新实例(不是同一个)推送到导航堆栈上。

      要继续,在故事板中,您只需将链接从集合视图单元拖动到视图控制器,然后在 prepareForSegue 方法中分配数据...

      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          if let viewController = segue.destinationViewController as? DetailVC {
              viewController.someProperty = self.someProperty
          }
      }
      

      【讨论】:

      • 选择这个答案是因为,虽然所有答案在技术上都是正确的,但这是唯一一个表明我必须使用 self. 才能传递相同的值,这是真正的问题
      【解决方案4】:

      DetailVC 中,在创建object 的同时创建一个变量并赋值。检查以下示例:

      class DetailVC {
       var dataString: String?
      }
      

      如下传递数据:

      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      
          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let vc = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
          vc.dataString = "PASS THE DATA LIKE THIS"
          self.navigationController?.pushViewController(vc, animated: true)
      
      
      }
      

      【讨论】:

        【解决方案5】:

        如果您遵循无故事板模式,您可以这样做

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let viewController = NextViewController()
            viewController.dataInsideNextViewController = "Data to be passed"
            navigationController?.pushViewController(viewController, animated: true)
        }
        

        【讨论】:

          【解决方案6】:

          在您的 ViewController1 中使用这些代码启动您的 ViewController2

          Class ViewController1: UIViewController {
          
              var dataFromVC2 = ""
          
              func loadVC2() {
                  let vc2 = ViewController2()
                  vc2.dataFromVC1 = "Hi VC2"
                  vc2delegate = self
                  navigationController?.pushViewController(vc2, animated: true)
              }        
          
          }
          

          在您的 ViewController2 中,添加此代码。您可以使用委托属性从 ViewContoller2 传回数据。

          Class ViewController2: UIViewController {
          
              var dataFromVC1: String = ""
              weak var delegate: ViewController1!
          
              func passData_toVC1() {
                  delegate.dataFromVC2 = "How's it going VC1?"
              }        
          
          }
          

          【讨论】:

            猜你喜欢
            • 2014-07-01
            • 1970-01-01
            • 2013-06-21
            • 2017-02-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-14
            相关资源
            最近更新 更多