2016-05-25 65 views
2

我正在使用準備segue在視圖控制器之間發送數據,第一個if條件在標識符爲「ShowPost」時起作用並將url發送給視圖控制器。但是當標識符是「profileInfo」時,segue不會發送數據。準備使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 
     } 
    } 
    } 
} 

回答

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

你else語句是在if語句中,所以它永遠不會被執行。

所以,把另一個if語句分開。

確保您已經從界面構建器設置了正確的標識符和類。

+0

由於它的工作,但現在它是不打裏面的任何破發點:如果讓indexPath = {tableView.indexPathForSelectedRow 讓_userName = searchResults [indexPath.row] [「owner」] [「display_name」]。stringValue –

+0

沒有得到你想說的話。你把哪個斷點打不到? – Lion

+0

從這行代碼let _userName = searchResults [indexPath.row] [「owner」] [「display_name」]。stringValue 到if let的結尾ie ie destinationController.ppImageString = _ppImageString –

0

也許這將是一個更清晰的交換機一點:

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 
} 
+0

當我使用開關的情況下我得到的情況下說的錯誤:字符串類型的表達式模式不能匹配「String?」類型的值 –

+0

我已經更新了,你可以用'?? 「」因爲它肯定是一個字符串,或者強制解包它'segue.identifier!'或者在函數'guard let identifier = segue.identifier else {return}'的開頭設置一個守護進程並使用'identifier'而不是 – Daniel

+0

它固定的錯誤,但它仍然無法正常工作,當我設置ProfileInfo情況下,它沒有擊中中的斷點。 –

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 
       } 
      } 

    }