簡體   English   中英

將數據從父 ViewController 傳遞到沒有故事板的子 VC - Swift 5

[英]Pass data from a Parent ViewController to Child VC without Storyboards - Swift 5

一旦我從 Github API 通過 UITextField 獲取數據,我就找不到將數據傳遞給 Child VC 的方法

protocol GithubManagerDelegate {
    func didUpdateGithub(_ githubManager: GithubManager, github: GithubModel)
    func didFailWithError(error: Error)
}

struct GithubManager {
    let profileUrl = "https://api.github.com/users"
    let clientId = // deleted this
    let secretId = // deleted this

    var delegate: GithubManagerDelegate?

    func fetchProfile(profileName: String) {
        let urlString = "\(profileUrl)/\(profileName)?client_id=\(clientId)&client_secret=\(secretId)"

        performRequest(with: urlString)
    }


    func performRequest(with urlString: String) {
        if let url = URL(string: urlString) {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { (data, response, error) in
                if error != nil {
                    self.delegate?.didFailWithError(error: error!)
                    return
                }

                if let safeData = data {
                    if let github = self.parseJSON(safeData) {
                        self.delegate?.didUpdateGithub(self, github: github)
                        print(github)
                    }
                }
            }
            task.resume()
        }

    }

    func parseJSON(_ githubData: Data) -> GithubModel? {
        let decoder = JSONDecoder()

        do {
            let decodeData = try decoder.decode(GithubData.self, from: githubData)
            let name = decodeData.name ?? "The user does not have a name"
            let login = decodeData.login ?? "No username by this name." // TODO: Change to give a alert
            let avatarUrl = decodeData.avatar_url
            let blog = decodeData.blog ?? "No Blog"
            let bio = decodeData.bio ?? "No Bio"
            let location = decodeData.location ?? "No location"
            let followers = decodeData.followers
            let following = decodeData.following


            let github = GithubModel(githubName: login, githubUser: name, githubAvatar: avatarUrl, githubBio: bio, githubLocation: location, githubBlog: blog, githubFollowers: followers, githubFollowing: following)

            return github

        } catch {
            delegate?.didFailWithError(error: error)
            return nil
        }
    }
}

在 textFieldDidEndEditing 的父 VC 中,我獲取輸入文本並使用它從 GithubAPI 獲取信息

if let username = searchTextField.text {
            DispatchQueue.main.async {
                self.githubManager.fetchProfile(profileName: username)
            }
        }

然后在我的 Child VC 中,我使用 GithubManagerDelegate,在那里我使用 DispatchQueue where,用信息填充標簽。 但是信息是空的,因為一旦收到數據,我就無法將其傳遞給孩子。

func didUpdateGithub(_ githubManager: GithubManager, github: GithubModel) {
        DispatchQueue.main.async {
            self.usernameLabel.text = github.githubName
        }
    }

我從 ParentVC 到 ChildVC 的方式:

導航控制器?.pushViewController(profileVC,動畫:真)

希望我讓自己清楚問題是什么......

當您嘗試在parent ViewController 和child ViewController 之間傳遞數據時,您應該向子視圖添加子視圖而不是導航。

child ViewController 中: var passingName: String?

例如

let child = #YourChildViewController insitiate it.
        self.addChild(child)
        child.passingName = passingName
        self.view.addSubview(child.view)
        child.didMove(toParent: self)

我希望這個解決方案可以幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM