2016-11-23 70 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