![](/img/trans.png)
[英]Pass Data (Label) from TableViewCell to another ViewController
[英]Pass value to another viewcontroller according to tableviewcell
我在每個tableViewCell上添加了UIButton,以便在點擊它時,它可以根據tableViewCell上的文本將用戶發送到另一個viewController。 我已經使用了singleTon和sharedManager在viewControllers之間傳遞值,並且在每個tableViewcell的函數“ setSelected”中設置了標題。
override func setSelected(selected: Bool, animated: Bool) {
SharingManager.sharedInstance.selectedCollege = self.CollegeLabel.text!
}
然后,我獲取“ selectedCollege”的值,並將標題設置在另一個UIviewcontroller上。
override func viewDidAppear(animated: Bool) {
self.title = SharingManager.sharedInstance.selectedCollege
}
但是,它不能正確設置標題,因為標題並不總是與tableViewCell上的文本匹配。
為什么要使用SharingManager? 如果要在ViewController之間傳遞數據,可以使用prepareForSegue方法。
使用prepareForSegue
方法。 1)在情節提要中設置segue 2)進行如下操作:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let cntrl = segue.destinationViewController as? SecondViewController {
cntrl.publicVar = 'your value'
}
}
你也可以得到indexOf選定的tableView行。 例:
let selectedIndex : NSIndexPath = self.tableView.indexPathForSelectedRow
另一種方式-初始化帶有故事板ID和調用pressent函數的viewController
您可以執行以下操作來滿足您的需求
在您要設置標題的視圖控制器中創建一個變量
class SecondVC: UIViewController {
//MARK: - Variables
var title : String = ""
.......
現在點擊按鈕
override func setSelected(selected: Bool, animated: Bool) {
let title = self.CollegeLabel.text!
let secondVC = self.storyboard?.instantiateViewControllerWithIdentifier("SecondVC") as! GSMenuCategoryPriceVC
secondVC.title = title
self.navigationController?.pushViewController(secondVC, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.