我正在使用准备segue在视图控制器之间发送数据,当标识符是“ShowPost”时,第一个if条件工作并将url发送到视图控制器。但是当标识符是“profileInfo”时,segue不发送数据。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowPost" {
if let indexPath = tableView.indexPathForSelectedRow {
let _url = searchResults[indexPath.row]["link"].stringValue
let destinationController = segue.destinationViewController as! ShowPostViewController
destinationController.url = _url
}
else if segue.identifier == "ProfileInfo" {
if let indexPath = tableView.indexPathForSelectedRow {
let _userName = searchResults[indexPath.row]["owner"]["display_name"].stringValue
// print("name dc: \(destinationController.userName)")
let _userId = searchResults[indexPath.row]["owner"]["user_id"].stringValue
let _userReputation = searchResults[indexPath.row]["owner"]["reputation"].stringValue
let _ppImageString = searchResults[indexPath.row]["owner"]["profile_image"].stringValue
let destinationController = segue.destinationViewController as! ProfileInfoViewController
destinationController.userName = _userName
destinationController.userId = _userId
destinationController.userReputation = _userReputation
destinationController.ppImageString = _ppImageString
}
}
}
}
答案 0 :(得分:3)
else语句被添加到第一个if语句中,并且无法运行。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowPost" {
if let indexPath = tableView.indexPathForSelectedRow {
let _url = searchResults[indexPath.row]["link"].stringValue
let destinationController = segue.destinationViewController as! ShowPostViewController
destinationController.url = _url
}
}
else if segue.identifier == "ProfileInfo" {
if let indexPath = tableView.indexPathForSelectedRow {
let _userName = searchResults[indexPath.row]["owner"]["display_name"].stringValue
// print("name dc: \(destinationController.userName)")
let _userId = searchResults[indexPath.row]["owner"]["user_id"].stringValue
let _userReputation = searchResults[indexPath.row]["owner"]["reputation"].stringValue
let _ppImageString = searchResults[indexPath.row]["owner"]["profile_image"].stringValue
let destinationController = segue.destinationViewController as! ProfileInfoViewController
destinationController.userName = _userName
destinationController.userId = _userId
destinationController.userReputation = _userReputation
destinationController.ppImageString = _ppImageString
}
}
}
答案 1 :(得分:1)
你的else语句在if语句中,所以永远不会被执行。
所以,将另一个if语句分开。
确保您已从界面构建器中设置了正确的标识符和类。
答案 2 :(得分:1)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowPost" {
if let indexPath = tableView.indexPathForSelectedRow {
let _url = searchResults[indexPath.row]["link"].stringValue
let destinationController = segue.destinationViewController as! ShowPostViewController
destinationController.url = _url
}
}
else if segue.identifier == "ProfileInfo" {
if let indexPath = tableView.indexPathForSelectedRow {
let _userName = searchResults[indexPath.row]["owner"]["display_name"].stringValue
// print("name dc: \(destinationController.userName)")
let _userId = searchResults[indexPath.row]["owner"]["user_id"].stringValue
let _userReputation = searchResults[indexPath.row]["owner"]["reputation"].stringValue
let _ppImageString = searchResults[indexPath.row]["owner"]["profile_image"].stringValue
let destinationController = segue.destinationViewController as! ProfileInfoViewController
destinationController.userName = _userName
destinationController.userId = _userId
destinationController.userReputation = _userReputation
destinationController.ppImageString = _ppImageString
}
}
}
答案 3 :(得分:0)
使用Switch可能会更清楚一点:
switch segue.identifier ?? "" {
case "ShowPost":
if let indexPath = tableView.indexPathForSelectedRow {
let _url = searchResults[indexPath.row]["link"].stringValue
let destinationController = segue.destinationViewController as! ShowPostViewController
destinationController.url = _url
}
case "ProfileInfo":
if let indexPath = tableView.indexPathForSelectedRow {
let _userName = searchResults[indexPath.row]["owner"]["display_name"].stringValue
// print("name dc: \(destinationController.userName)")
let _userId = searchResults[indexPath.row]["owner"]["user_id"].stringValue
let _userReputation = searchResults[indexPath.row]["owner"]["reputation"].stringValue
let _ppImageString = searchResults[indexPath.row]["owner"]["profile_image"].stringValue
let destinationController = segue.destinationViewController as! ProfileInfoViewController
destinationController.userName = _userName
destinationController.userId = _userId
destinationController.userReputation = _userReputation
destinationController.ppImageString = _ppImageString
}
default: break
}