开发者

Questions about iPhone app structure - Core Data, Views, Modal Views etc

About a month ago my wife and I came up with a good idea for an iPhone app, so I set about thinking of how the app would work from a users perspective and about a week ago I opened up XCode for the first time and began to develo开发者_运维知识库p.

Before I get too stuck into the writing of the app, I want to make sure I have some key concepts clear in my mind, specifically related to architecture.

FYI, as a principle I would like to try to create as much of my app programmatically (specifically the UI) so that I have a thorough understanding of what's going on. Later on I might use IB as a tool to speed up UI development (applies at the moment because I'm using TableViews as opposed to static views).

View Controllers

  • So UINavigationControllers are generally not sub-classed and are created as a property in the AppDelegate and are the main 'sub-view' of the MainWindow.xib.

  • The UINavigationController controls a stack of views, and is usually initiated with a root view controller.

  • The navigation controller is generally referenced and messaged from within the current/top view controller as [self navigationcontroller] which is a read-only property of the UIViewController parent class.

Core Data

  • The Managed object context is the main control point for accessing data in the model but it is generally not used directly and instead fetch requests are used to return arrays, sets or dictionaries of data objects which are then used by the view controllers to present and manipulate data.

  • the context is created as a property in the app delegate and is then passed to the root view controller upon start up which itself passes it to subsequent views in the stack before telling the navigation controller to change the view (for example when a cell is tapped).

So it's taken me a week to get to here, I can create my model, fill it with data and display it on a table view but I have not been able to find much on Modal Views when you want to edit data, specifically how to design modal views (if there's any specific difference to normal views)?

Also, I'm not entirely sure of the whole 'binding' of data to view objects what it does and how to do it programmatically?

Many thanks for your comments, suggestions in advance.


A view becomes modal when it is displayed using presentModalViewController:animated. You can pass along the model to be edited before presenting the modal view.

DetailViewController dvc = ...;
dvc.model = model;
dvc.delegate = self; // if you want to use a delegate pattern.
[self presentModalViewController:dvc animated:YES];

Since the model you want to edit is available in the modal view controller you can set the values in viewDidLoad. There is no databinding available on the iOS platform which means you have to updated the view and your model manually.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.someTextField.text = self.model.someText;
}

What you need to consider when working with a modal view is that you must provide a way to dismiss it. E.g. by having a navigation bar on top with Cancel and Save buttons. Dismissing a modal view controller is done using:

[self dismissModalViewControllerAnimated:YES];

It is possible to dismiss the modal view from within the modal view controller but that also means that such a modal controller must be able to save the model in question. I would personally use a callback to the presenting controller letting it know wether to save or cancel as well as handling the dismissing of the modal view. This can be done using a delegate pattern. So when the user taps Save you can update the model and then notify the delegate.

- (IBAction)save:(id)sender {
    self.model.someText = self.someTextField.text;
    [self.delegate detailViewControllerDidSave:self];
}

And in the presenter have a method like:

-(void)detailViewControllerDidSave:(DetailViewController *)controller {
  // save the model
  [self dismissModalViewControllerAnimated:YES];
}


Some answers, from my knowledge of views/data. I've no experience yet with CoreData so won't answer that part.

  • A modal view is just a regular view. But its size can be a little different, since it covers some UI elements that wouldn't be covered by a view pushed in a navigation controller.

  • For the data 'binding' to views, you have already done it if you fill a table view. There is a series of 'data source' methods to implement in your Controller where you provide your data from your Model to your View (MVC pattern).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜