2016-11-23 98 views
0

我的情況由UITable組成,當選中一行時,它將打開一個新的VC併發送一對變量。在DidSelectAtRow函數之前執行的Segue正在執行

我的問題是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)爲空。

如果我刪除storyboard中的segue並運行,那麼該函數中的所有調試輸出都可以正常運行。只要我再次創建segue,應用程序就會切換到新的屏幕並在上述任何代碼運行之前崩潰。

要創建我賽格瑞我:

1)在我的內故事板VC1的UITableView細胞右鍵單擊並拖動到我的VC2

2)選擇類型爲show

3)複製VC 1中的prepare for segue函數的segue標識符名稱,並將其粘貼到Storyboard中屬於新segue的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 


    } 
} 

} 
+0

你應該從viewcontroller頂部創建segue而不是從單元格 – Vinodh

+1

從ViewController創建segue到ViewController – Rajat

+1

請使用以下帖子瞭解如何以編程方式繼續並使用它http: //stackoverflow.com/a/21205550/1142743 – Vinodh

回答

0

您正在創建從小區到的ViewController您SEGUE,你需要創建一個視圖控制器順着接下去的ViewController這樣,

enter image description here