1 / 28

View Controllers

View Controllers. Content taken from book: “ iPhone SDK Development” by Bill Dudney and Chris Adamson. Important concepts to review.

hubert
Download Presentation

View Controllers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. View Controllers Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson

  2. Important concepts to review • Objective-C instance variables go in the header file, inside the @interface declaration, always with the type and, if they’re objects, the pointer (*) character. • Objective-C method declarations go in the header file, outside the @interface block, in the following form: - (returntype) methodName: (parameter1type) parameter1 parameter2Name: (parameter2type) parameter2... ;. • Alternately, you can put a method declaration (or only its implementation) in the .m file, if you don’t want other classes to be able to see it.

  3. …more concepts to review • Objects are created in code (usually with alloc and init) or withInterfaceBuilder. • To work with objects created in Interface Builder, declare instance variables as IBOutlets and event-handling methods as returning IBAction. • Then wire the connections in the nib file with IB. Be sure to save the file in IB before you build the project in Xcode.

  4. …more concepts to review • To implement a delegate protocol, add the protocol name in angle braces to your header file’s @interface statement, after the name of the class you’re subclassing. • Then, in the implementation (.m) file, implement all required methods as well as any others you’re interested in.

  5. …additional concepts to review • Declare properties with a @property declaration in the header file, and then use @synthesize in the implementation to have the compiler create the getters and setters for you. • Add to the provided implementation of dealloc to release any instance variables your object may be retaining.

  6. View Controllers • Each screen in your iPhone application is managed by a view controller (VC) that is in charge of displaying the view and responding to just about every action the user can take on that view. • Your view controller is the C in MVC (Model View Controller). • It needs to communicate with both the view and model parts of your application.

  7. The Movie project sample • Name of project: Movie • Open the MovieViewController.xib file in Interface Builder by double-clicking it. • Add a button to this initial view and have that button invoke an action method on the view controller. • Set its title to Edit by double-clicking in the center of the button and then typing Edit.

  8. Implementing a Button Action • The button should do something when the user taps it. • Open the MovieViewController.h file in Xcode, and add this line to it just before the @end compiler directive: • - (IBAction)edit;

  9. Making a connection in IB • Save your work in Xcode, and head back to IB to connect the button to this action. • The target is the File’s Owner, and the action is the edit method. Setting that target/action pair is what causes the button to invoke the edit method when it’s clicked.

  10. Implementing the method • In the MovieView-Controller.mfile. • Open the file in Xcode, and add some code between the @implementation and @end compiler directives that looks like this: - (IBAction)edit { NSLog(@"edit method invoked" ); }

  11. Models • In iTunes, classes like Song or Podcast and the actions they support like play form the model. • In iPhoto, it’s classes like Photo or Album. • To build a serious application, you need a real model. • A real model captures the essence of an application.

  12. Building a Model • We’ll build a Movie class to play the part of our model. We will then use an instance of our Movie class to hold the data and modify the existing UI to display the data. • The view controller will be the glue that is connected to both the model and the view.

  13. Creating the Movie class • Select the Classes group in Xcode, and then right-click and select Add > New File from the pop-up menu. • In the dialog box that opens, choose Cocoa Touch Classes > Objective-C Class. • In the “Subclass of” pull-down, make sure to select NSObject, and then click the Next button.

  14. Creating the Movie class • Name the class Movie, and make sure to check the two checkboxes for adding the .h file and adding the file to the target.

  15. Adding properties to classes • Add three properties: • an NSString named title to hold the title of the movie, • an NSNumber named boxOfficeGross to hold the box-office sales, • and an NSStringnamed summary to hold the plot summary text. • Define a custom init method for our movie so we can initialize the three properties when we create a new movie.

  16. Movie Class

  17. Adding Outlets and Actions to the Controller • This process requires jumping between IB and Xcode frequently. • Add four properties and the instance variables • The movie object that will be our model.

  18. Adding Outlets and Actions to the Controller • The @class compiler directive on line 3 is a forward declaration, and it tells the Objective-C compiler that you know that it can’t find a class named Movie and that it should not report errors or warnings about not being able to find it. • We use forward declarations because the compiler provides poor error reporting on include cycles.

  19. MovieViewController.m • Add the @synthesize statement for each of the properties and the import to the implementation file. • for every @class forward declaration you do in the header file, you have to import the header file for that class in the implementation • file

  20. Updating the UI • Open MovieViewController.xib. • Add three UILabels • Connect the VC’s outlets to the labels

  21. Implementing the Controller • The movie’s data should be displayed on the screen. • Create the movie object. • Override the viewDidLoad method in the MovieView-Controller to create a new instance once the view that the MovieViewController controls is loaded.

  22. applicationDidFinishLaunching • the applicationDidFinishLaunching: method on the MovieAppDelegate class is called by the UIApplication object when it’s finished launching. • The app delegate has two outlets that are connected in the MainWindow.xib nib file: window and viewController. • The window property is connected to the main window of our application • The viewControllerproperty is connected to our movie view controller

  23. code for applicationDidFinishLaunching • The app delegate is asking the window to add the viewController’s view as a subview.

  24. When a view controller is asked for its view • First, it checks to see whether it already has one. • If so, it just returns the already loaded view. • If it does not have one, then it calls the loadView method. • That process runs more or less like this: • Is a nib filename set (typically set in IB but can be set via code)? • If so, load the nib file passing the view controller in as File’s Owner. • After the nib file is loaded, assert that the view has been set. If not, throw an exception and print an error message.

  25. LoadView • If a nib filename is not set on your view controller, then you need to build the view manually in the loadView. • We can set the nib filename via IB, and we rarely if ever need to manually code a loadView method. • Once the loadView method finishes, the viewDidLoad method is called. • This is a great place for us to do initialization (such as create our Movie object) because it’s called only when the view is loaded.

  26. viewWillAppear: method • This method is called every time the view is about to become visible. • A view can become visible and then be removed from view several times during the normal flow of an application. • For example, consider the Contacts app; the list of all your contacts is shown each time you finish looking at or editing an individual contact, and the viewWillAppear: method is called each time.

  27. Code for the viewWillAppear: method • Hence, this method is the ideal place to set the values we want to appear on the view when it does appear.

  28. Running Application

More Related