swift5.1 Xcode11.3.1
もし分かる方がいらっしゃたらぜひ教えていただきたいです。
SecondViewController(UIViewController)上のTableViewのTablewViewCellに配置したUIButtonを押したときに、NextViewController(UIViewController)に移動させるのですが、そのときにNextViewControllerで制御しているUILabelのtextに、遷移元のCellに配置してあるUILabelのtextを一度、「var argString = ""」でに入れて、NextViewControllerのUILabelのtextを初期化したいのですがうまくいきません。
Swift
1class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 2 3 4 @IBOutlet weak var tableView: UITableView! 5 6 var misoSoup:[String]! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 misoSoup = ["しじみの味噌汁", "なめこの味噌汁", "トマトの味噌汁"] 11 } 12 13 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 14 return misoSoup.count 15 16 } 17 18 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 19 let cell = tableView.dequeueReusableCell(withIdentifier: "CELL", for: indexPath) as! TableViewCell 20 cell.setData(buttonText: "作る", labelText: misoSoup[indexPath.row]) 21 cell.NexrButton.tag = indexPath.row 22 cell.NexrButton.addTarget(self, action: #selector(tapCellButton(_:)), for: .touchUpInside) 23 24 return cell 25 } 26 27 @objc func tapCellButton(_ sender: UIButton) { 28 let storyboard = UIStoryboard(name: "Main", bundle: nil) 29 let view = storyboard.instantiateViewController(withIdentifier: "Next") as! NextViewController 30 view.argString = misoSoup[sender.tag] 31 32 performSegue(withIdentifier: "Next", sender: Any?.self) 33 34 } 35 36省略
Swift
1class NextViewController: UIViewController { 2 3 4 @IBOutlet weak var UserName: UILabel! 5 var argString = "" 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 self.UserName.text = argString 11 print(self.UserName.text) 12 13 } 14} 15
http://harumi.sakura.ne.jp/wordpress/2019/01/09/determine-which-cell-belongs-to-customcell-button/
UITableViewCell上のUIButtonのタップイベントについては、こちらの記事を参考にさせていただきました。
ありがとうございます。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/19 10:16