私の状況は、行が選択されたときに新しいVCを開き、いくつかの変数を送信するUITableで構成されます。DidSelectAtRow関数の前に実行されるSegue
私の問題は、関数DidSelectAtRowが実行される前にセグが実行されているということです。
上記のprint
コマンドが実行されていないため、私はこれを知っています。その後、次の画面が表示されたときにアプリがクラッシュします。なぜなら、そこにあると予想される変数は、(cellId)
です。
ストーリーボードのセグを削除して実行すると、その機能のすべてのデバッグ出力が正しく実行されます。セグをもう一度作成すると、アプリケーションは新しいスクリーンに切り替わり、上記のコードが実行される前にクラッシュします。私のセグエを作成するには
私は:
1)右)ストーリーボード内VC1に私のUITableViewでセルをクリックすると、私のVC2に
2をドラッグ)show
3通りのタイプを選択しますVC1のprepare for segue
機能からセグ識別子の名前をコピーし、それを新しいSegue用にStoryboardのidentifier
属性に貼り付けます。
アイデア?
以下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を作成する必要があります – Vinodh
ViewControllerからViewControllerへsegueを作成 – Rajat
segueをプログラムで使用する方法を知るには、 //stackoverflow.com/a/21205550/1142743 – Vinodh