2017-03-16 1 views
2

私は迅速な開発に新しいです。私はコンセントを作成せずにラベルのテキストにアクセスしようとしています。ラベルを付けるようにタグを設定し、テキストを取得しようとしました。swift 3.0でコンセントを作成せずにラベルやテキストフィールドのテキストにアクセスできますか?

私はそれを行うことができない私はそれに成功したが、私はプログラムで次のViewControllerにそのテキストをナビゲートしたいのですが、このリンクに How Can I access the textfields in static TableViewcells without creating referencing outlet?

を言及しました。誰も私のためにそれを助けることができますか?

私はあなたのコードを試してみましたし、それが動作する次のコード

for view: UIView in self.view.subviews { 
     if (view is UILabel) { 
      let stortboard = UIStoryboard.init(name:"Main",bundle:nil) 
      let vc = stortboard.instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController 

      let lbl: UILabel? = (view as? UILabel) 

      vc?.lblData = lbl?.text 
      print(lbl?.text ?? "not found") 
      //self.navigationController?.pushViewController(vc!, animated: true) 
      self.present(vc!, animated: true, completion: nil) 
     } 
} 
+0

あなたのラベルにタグを設定し、viewWithTagを取得してみましょう let foundView = self.view.viewWithTag(yourtag number) UILabel { print(lbl?。text? "not found") } –

+0

'SecondViewController'に変数' lblData'がありますか? –

+0

@ShitalあなたはSecondViewControllerに1つの文字列変数を作成し、currentViewController "vc?.variableName = lbl?.text"に設定してから、それをSecondViewControllerに使用する必要があります。viewControllerを押した後 –

答えて

1

を試してみました。あなたはlblData:String!あなたがタグによって検索したい場合は説明するように、あなたがviewWithTagを使用することができ、この

for view in self.view.subviews { 
     if (view is UILabel) { 
      let stortboard = UIStoryboard.init(name:"Main",bundle:nil) 
      let vc = stortboard.instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController 
      let lbl: UILabel? = (view as? UILabel) 
      vc?.lblData = lbl?.text 
      print(lbl?.text ?? "not found") 
      self.present(vc!, animated: true, completion: nil) 
     } 
    } 
+0

これは私のために働いた。ありがとうございます:) – Shital

2

を試してみてください

SecondViewControllerであることを確認してください。テキストフィールドの

設定タグを次のように

enter image description here

//Get One By One with Tag 
if let txtField1 = self.view.viewWithTag(1) as? UITextField { 
    print(txtField1.text!) 
} 

を、それはあなたを助ける願っています。

関連する問題