繁体   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