简体   繁体   English

在导航到ViewController之前为它加载数据的最佳方法是什么?

[英]What's the best way to load data for a ViewController before navigating to it?

In my application there are two view controllers that navigate to a DetailsViewController. 在我的应用程序中,有两个视图控制器可导航到DetailsViewController。

Right now, when the DetailsViewController appears, I fetch data from the server and display it on the UI. 现在,当DetailsViewController出现时,我从服务器获取数据并将其显示在UI上。 I dislike this because the UI is blank while the network request is going on. 我不喜欢这样做,因为在进行网络请求时用户界面为空白。 What I want is that the data be loaded in the previous view controllers and then passed to DetailsViewController. 我想要的是将数据加载到以前的视图控制器中,然后传递给DetailsViewController。

Now the problem is that I have the exact same "load-data-and-then-push" code in two view controllers and I'm not sure what the most sensible way is to remove the repetition. 现在的问题是,我在两个视图控制器中具有完全相同的“加载数据然后推送”代码,而且我不确定删除重复的最明智的方法是什么。

One idea is to have the two view controllers inherit from a common superclass which contains the loading/pushing method. 一种想法是让两个视图控制器从包含加载/推动方法的公共超类继承。 I don't like this strategy because, supposing I have more ViewControllers like DetailsViewController down the line, I wouldn't like to write a loading superclass for each one. 我不喜欢这种策略,因为假设我有更多像DetailsViewController这样的ViewController,我不想为每个类编写一个加载超类。

Another idea would be to define a static method in the DetailsViewController which the two view controllers can invoke but this method contains UI related code (specifically, code to show an HUD Progressbar and a UIAlertView in case network fetch fails) which makes me uncomfortable. 另一个想法是在DetailsViewController中定义一个静态方法,两个视图控制器可以调用该方法,但是此方法包含与UI相关的代码(特别是在网络获取失败的情况下显示HUD Progressbar和UIAlertView的代码),这让我感到不舒服。

I am very new to iOS and Objective-C so I might be missing something simple and obvious. 我是iOS和Objective-C的新手,所以我可能缺少一些简单明显的东西。

My favorite would be in this case to create a new class which handles the loading of the data (like http-request, etc.) and to create a delegate protocol for this class. 在这种情况下,我最喜欢的是创建一个新类来处理数据的加载(例如http-request等),并为此类创建一个委托协议。 This delegate callback might then be implemented in your two viewControllers which would then perform the push segue to your DetailsViewController when called. 然后,可以在您的两个viewControllers中实现此委托回调,然后在调用它们时对您的DetailsViewController进行推送选择。 Delegation is a very nice and powerful feature, check out the documentation here: Delegation 委托是一个非常好的功能,请在此处查看文档: 委托

Well, I'd better write it in the comments, but I have no reputation for that. 好吧,我最好在评论中写出来,但是我对此没有声誉。

Imagine you are reading a json with several students information (name, year, etc.) you can create a student object with the property that will be read and an object that will have a method that will run in the background that will be responsible for accessing your WS (JSON or whatever it is) and record this information to the student object. 假设您正在读取带有多个学生信息(名称,年份等)的json,则可以创建一个具有将被读取的属性的学生对象,以及一个对象,该对象的方法将在后台运行,从而负责访问您的WS(JSON或其他形式)并将此信息记录到Student对象。 So if you have 10 students you will have an NSArray containing 10 students. 因此,如果您有10个学生,则将有一个包含10个学生的NSArray。 This array is what you will for your next viewcontroller. 该数组是您将要用于下一个ViewController的数组。

It is a lot of code, but you think examples easily. 这是很多代码,但是您容易想到示例。

If you use Storyboards you can use prepareForSegue: sender: to pass your data/model class to your DetailsViewController. 如果使用情节提要,则可以使用prepareForSegue: sender:将数据/模型类传递给DetailsViewController。 If you use xib's you can do the same after instantiating the DetailsViewController and before pushing it. 如果使用xib,则可以在实例化DetailsViewController之后并在推送之前执行相同的操作。

If you need to load subsequent data from your server you should write a class that does this network stuff for you. 如果需要从服务器加载后续数据,则应编写一个为您完成此网络工作的类。

If DetailsViewController needs to load some additional data, you can use something like a loading view like Andy suggested. 如果DetailsViewController需要加载一些其他数据,则可以使用类似Andy建议的加载视图之类的内容。 This is a widely used method. 这是一种广泛使用的方法。

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

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