【发布时间】:2016-11-23 10:08:59
【问题描述】:
My situation consists of a UITable which when a row is selected it opens up a new VC and sends across a couple of variables.
我的问题是 segue 在函数 DidSelectAtRow 运行之前被执行。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print ("hello")
self.selectedCellTitle = self.communities[indexPath.row]
self.cellId = indexPath.row
print ("now here: ", communityIds[cellId!])
self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
}
我知道这是因为上面的print 命令没有被执行。然后应用程序在下一个屏幕时崩溃,因为它预期的变量(cellId) 为空。
如果我删除情节提要中的 segue 并运行,那么该函数中的所有调试输出都会正确运行。一旦我再次创建 segue,应用程序就会切换到新屏幕并在运行上述任何代码之前崩溃。
为了创造我的转场,我是:
1) 右键单击 Storyboard 中 VC1 上我的 UITableView 中的单元格并拖动到我的 VC2
2) 选择类型为show
3) 从 VC 1 中的 prepare for segue 函数中复制 segue 标识符名称,并将其粘贴到 Storyboard 中的 identifier 属性中以获取新的 segue。
有什么想法吗?
下面是VC1的完整代码:
import UIKit
class CommunitiesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var selectedCellTitle: String?
var cellId: Int?
var communities = [String]() //{ didSet { communitiesTableView.reloadData()
// }
// }
var communityIds = [String]()
var flag = true
var userEmailText: String?
var tempComId: Int?
@IBOutlet weak var joinCommunityButton: UIButton!
@IBOutlet weak var createCommunityButton: UIButton!
@IBOutlet weak var communitiesTableView: UITableView!
override func viewDidLoad() {
self.communitiesTableView.delegate = self
self.communitiesTableView.dataSource = self
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.communities.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
cell.textLabel?.text = self.communities[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print ("hello")
self.selectedCellTitle = self.communities[indexPath.row]
self.cellId = indexPath.row
print ("now here: ", communityIds[cellId!])
self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if flag == true
{
self.communitiesTableView.reloadData()
let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/getDetails.php?");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
let postString = "email=\(self.userEmailText!)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
if error != nil {
print("error=\(error)")
return
}
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
if let arr = json?["communities"] as? [[String:String]] {
self.communities = arr.flatMap { $0["name"]!}
self.communitiesTableView.reloadData()
}
} catch{
print(error)
}
}
}
task.resume()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "createCommunitySegue" {
let createCommunityController: CreateNewCommunity = segue.destination as! CreateNewCommunity
createCommunityController.myEmail = self.userEmailText
}
if segue.identifier == "joinCommunitySegue" {
let joinCommunityController: JoinCommunity = segue.destination as! JoinCommunity
joinCommunityController.myEmail = self.userEmailText
}
if segue.identifier == "showCommunitySegue" {
let showCommunityController: ShowCommunityViewController = segue.destination as!ShowCommunityViewController
print("yes here: ", self.cellId!)
showCommunityController.communityIsCalled = self.selectedCellTitle
showCommunityController.comIds = self.communityIds
showCommunityController.communityId = self.cellId
}
}
}
【问题讨论】:
-
你应该从 viewcontroller 顶部而不是单元格创建 segue
-
创建从 ViewController 到 ViewController 的 segue
-
请使用以下帖子了解如何以编程方式使用它stackoverflow.com/a/21205550/1142743
-
太棒了!!就这样搞定了!感谢您的链接也@Vinodh
-
发布一个答案,您如何获得对他人有帮助的解决方案
标签: ios swift uitableview uiviewcontroller