개발 알다가도 모르겠네요

다음 뷰 컨트롤러로 navigating하는 두 가지 방법 With 뉴스앱 본문

모바일/Swift

다음 뷰 컨트롤러로 navigating하는 두 가지 방법 With 뉴스앱

이재빵 2021. 2. 3. 22:07
728x90
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storyboard = UIStoryboard.init(name : "Main", bundle: nil)
        let newsDetail = storyboard.instantiateViewController(identifier: "NewsDetailController") as! NewsDetailController
        if let news = newsData {
            let row = news[indexPath.row]
            if let v = row as? Dictionary<String, Any> {
                if let imageUrl = v["urlToImage"] as? String {
                    newsDetail.imageUrl = imageUrl
                }
                if let dsec = v["description"] as? String {
                    newsDetail.dsec = dsec
                }
            }
    }
        showDetailViewController(newsDetail, sender: nil)
    }

 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if "NewsDetail" == segue.identifier {
            if let controller = segue.destination as? NewsDetailController {
                if let news = newsData {
                   if let indexPath = tableView.indexPathForSelectedRow { //indexpath 가져오기 
                    let row = news[indexPath.row]
                    if let v = row as? Dictionary<String, Any> {
                        if let imageUrl = v["urlToImage"] as? String {
                            controller.imageUrl = imageUrl
                        }
                        if let dsec = v["description"] as? String {
                            controller.dsec = dsec
                        }
                    }
                    }
                    
            }
        }
    }
    }