繁体   English   中英

从另一个视图调用对象

[英]Call object from another View

我正在开发一个向Web服务发出请求的应用程序,一旦Web服务答案(以JSON格式),我便在另一个视图(repository.swift)中将结果解码

我只需要在其他视图之一(Swype.swift)中使用存储在存储库中的值

这是我的解码JSON的代码:

if let json = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [String:AnyObject] {
                    for (_, value) in json {
                        if let dict = value as? [String:AnyObject] {
                            let repo = Repository(jsonData: dict)
                            repos.append(repo)
                        }
                    }
                }

而我的“仓库”类:

class Repository {

var idobjet: String?
var title: String?
var price: String?
var description: String?
var added: String?
var userid: String?
var user_name: String?
var user_zipCode: String?
var category_id: String?
var category_label: String?
var subcategory_id: String?
var subcategory_label: String?
var picture_url: String?

init(jsonData: [String:AnyObject]) {
    self.idobjet = jsonData["id"] as? String
    self.title = jsonData["title"] as? String
    self.price = jsonData["price"] as? String
    self.description = jsonData["description"] as? String
    self.added = jsonData["addedDate"] as? String
    self.userid = jsonData["userid"] as? String
    self.user_name = jsonData["user_name"] as? String
    self.user_zipCode = jsonData["user_zipCode"] as? String
    self.category_id = jsonData["category_id"] as? String
    self.category_label = jsonData["category_label"] as? String
    self.subcategory_id = jsonData["subcategory_id"] as? String
    self.subcategory_label = jsonData["subcategory_label"] as? String
    self.picture_url = jsonData["picture_url"] as? String

   }
}

var repos = [Repository]()

我需要在其他视图中获取“ picture_url”,“ title”和“ Description”值,以创建Imageview和其他一些字段:

//Construct the imgUrl to get an image URL for the pages
                let urlString: NSString =  "picture_url" Values
                if let url = NSURL(string: urlString as String) {
                    if let data = NSData(contentsOfURL: url) {
                        newCard.image = UIImage(data: data)!

                        newCard.content = "title" value
                        newCard.desc = "price" value
                        self.data.append(newCard)
                        NSLog("fetch new data")

                        }
                    }
                }

根据使用此字段的视图控制器的数量,最简单的解决方案可能是定义一个属性,该属性将您的Repository对象保存在目标视图控制器中,然后在设置之前在父视图控制器中进行设置(使用(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender方法)。

另一方面,如果必须经常访问这些字段,则设置视图控制器可以依赖的数据模型类可能更方便。

好的,因此有多种方法可以解决此问题。

这是我的建议:

将存储库对象初始化为全局变量。

var repo:Repository?

class SomeViewControllerOne: UIViewController { 
    func initializeRepository() {
        repo = Repository(....)
        // continue with initialization
    }
}

class AnotherViewControllerTwo: UIViewController {
    func useRepository() {
        if let repository = repo {
            print(repository.title)
        }
    }
}

对象“ repo”是全局的,可以在整个应用程序中使用。 只是小心,因为对象是可选的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM