我想允许用户选择一行,并且给定该朋友的关系,在行单击时会发生不同的操作。例如,如果是朋友,则聊天打开,如果不是朋友,则可以允许发送朋友请求,等等。
目前,我已经实现了以下内容,但是在每一行点击,无论朋友的状态如何,聊天都会打开并抛出错误,即friendChat
对象生成IndividualChatController
}:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// Ensure controller knows which dataset to pull from,
// so detail view is correct
let friendChat: Friend
if searchController.active && searchController.searchBar.text != "" {
friendChat = filterMappedFriends[indexPath.row]
} else {
friendChat = mappedFriends[indexPath.row]
}
// Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them:
if(friendChat.statusSort == 2) {
performSegueWithIdentifier("showIndividualChat", sender: self)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showIndividualChat" {
let controller = segue.destinationViewController as! IndividualChatController
controller.friendChat = friendChat
controller.senderId = FeastGlobal.sharedInstance.userID
controller.senderDisplayName = FeastGlobal.sharedInstance.userName
}
}
} else if (friendChat.statusSort == 1) {
print("Can invite to be friend")
} else if (friendChat.statusSort == 0) {
print("Invite to Feast")
}
}
主:
关闭segue名称:
答案 0 :(得分:0)
您确定它不适用于其他statusSort案例吗?其他的看起来很好,但statusSort == 2不应该工作,因为你将prepareForSegue定义为一个新的嵌套函数。我想知道您是否刚刚看到statusSort == 2的问题并将其误认为其他人?
statusSort == 2赢得工作的原因是因为你在条件内重新定义了prepareForSegue作为嵌套函数,它需要是视图控制器的一个函数作为覆盖。将它放在与覆盖func tableView相同的级别上作为另一个覆盖。
现在它不会被调用,所以你在不设置目标视图控制器的部分的情况下为statusSort == 2执行segue。这就是为什么它抱怨朋友的话,它没有设定。
像这样:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// You're already doing this part.
}
// Move your prepareForSegue out here like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Do your stuff here with the destination view controller. You have multiple options here to handle the upcoming segue properly.
// Option 1: Use the segue identifier from your existing code so you can distinguish between multiple segues here.
// <YOUR CODE HERE>
// Option 2: If you segue to distinct view controllers, you can just check for the view controller type here. Either do this through optional downcast or use the "is" operator and then cast inside the conditional blocks. The former is shown below and is cleaner.
if let someViewController: SomeViewController = segue.destinationViewController as? SomeViewController {
// This is a segue headed to SomeViewController. Now we can set SomeViewController here. I'm just making up a bar here.
let foo: Int = 1
someViewController.bar = foo
} else if let anotherViewController: AnotherViewController = segue.destinationViewController as? AnotherViewController {
// Another segue headed to AnotherViewController.
let foo: Int = 1
anotherViewController.bar = foo
}
}
答案 1 :(得分:0)
你不应该在didSelectRow方法中放置prepareForSegue方法 这样做会导致未调用prepareForSegueMethod。
这是错误的:
class YourClass: UITableViewController {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Don't put in here
func prepareForSegue(sgue: UIStoryboardSegue, sender: AnyObject?) {
...
}
}
}
相反,将它直接放在你的班级中:
class YourClass: UITableViewController {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
...
}
// Put in here
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
...
}
}