more iphone 3 development phần 2 docx

57 470 0
more iphone 3 development phần 2 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

41 41 Chapter A Super Start: Adding, Displaying, and Deleting Data Well, if that last chapter didn’t scare you off, then you’re ready to dive in and move beyond the basic template we explored in Chapter 2. In this chapter, we’re going to create an application designed to track some superhero data. Our application will be based on the Window-based Application template. We’ll use the data model editor to design our superhero entity. And then we’ll create a new controller class, derived from UIViewController, that will allow us to add, display, and delete superheroes. In Chapter 4, we’ll extend our application further and add code to allow the user to edit their superhero data. Take a look at Figure 3-1 to get a sense of what our app will look like when it runs. Looks a lot like the template app. The major differences lie in the entity at the heart of the application and in the addition of a tab bar at the bottom of the screen. Let’s get to work. 3 CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 42 Figure 3-1. The SuperDB application as it will look once we’ve finished this chapter Setting up the Xcode Project Time to get our hands dirty. Launch Xcode if it’s not open, and type N to bring up our old friend, the new project assistant (Figure 3-2). Figure 3-2. Our dear old friend, Xcode’s new project assistant CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 43 In the last chapter, we started with the Navigation-based Application template. When you create your own navigation applications, that’s a good template to use, as it gives you a lot of the code you’re likely to need in your application. However, to make it easier to explain where to add or modify code and also to reinforce your understanding of how applications are constructed, we’re going to build the SuperDB application from scratch, just as we did throughout most of Beginning iPhone 3 Development (Apress, 2009). Select Window-based Application, and make sure that the Use Core Data for storage check box is checked. When prompted for a project name, type in SuperDB. When the project window appears, expand both the Classes and the Resources groups to make it easier to get to the main files with which we’ll be working. Application Architecture As you can see from Figure 3-1, we’re going to create an application with both a tab bar and a navigation controller. Before we start writing code, we need to put a little thought into our application’s structure. We need to know, for example, whether our application’s root view controller will be a navigation controller, tab bar controller, or something else entirely. There’s not a single right architecture for every application. One obvious approach would be to make the application’s root view controller a UITabBarController, and then add a separate navigation controller for each tab. In a situation where each tab corresponds to a completely different view showing different types of data, that approach would make perfect sense. In Beginning iPhone 3 Development, in Chapter 7, we used that exact approach because every single tab corresponded to a different view controller with different outlets and different actions. In our case, however, we’re going to implement two tabs (with more to be added in later chapters), but each tab will show exactly the same data, just ordered differently. When one tab is selected, the table will be ordered by the superhero’s name. If the other tab is selected, the same data will be shown, ordered by the superhero’s secret identity. Regardless of which tab is selected, tapping a row on the table will do the same thing: drill down to a new view where you can edit the information about the superhero you selected (which we will add in the next chapter). Regardless of which tab is selected, tapping the add button will add a new instance of the same entity. When you drill down to another view to view or edit a hero, the tabs are no longer relevant. For our application, the tab bar is just modifying the way the data in a single table is presented. There’s no need for it to actually swap in and out other view controllers. Why have multiple navigation controller instances all managing identical sets of data and responding the same way to touches? Why not just use one table controller, and have it change the way it presents the data based on which tab is selected? That’s the approach we’re going to take in this application. As a result, we won’t be using UITabBarController at all. CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 44 Our root view controller will be a navigation controller, and we’ll use a tab bar purely to receive input from the user. The end result that is shown to the user will be identical to what they’d see if we created separate navigation controllers and table view controllers for each tab, but behind the scenes, we’ll be using less memory and won’t have to worry about keeping the different navigation controllers in sync with each other. Our application’s root view controller will be an instance of UINavigationController. We’ll create our own custom view controller class, HeroListViewController, to act as the root view controller for this UINavigationController. HeroListViewController will display the list of superheroes along with the tabs that control how the heroes are displayed and ordered. Here’s how the app will work. When the application starts, the UINavigationController instance is created from the nib file and the navigation controller’s view is added as a subview to the application’s window so it can be seen. The rest of the window will be taken up by a content pane for its subcontroller views. Next, the instance of HeroListViewController will be loaded from the nib, and the view from its associated nib file will be added as a subview to the navigation controller’s content pane. This view (the one associated with HeroListViewController) contains our tab bar and our superhero table view. In Chapter 4, we’ll add a table view controller into the mix that implements a detail superhero view. When the user taps on a superhero in the superhero list, this detail controller will be pushed onto the navigation stack and its view will temporarily replace the HeroListViewController’s view in the UINavigationController’s content view. No need to worry about the detail view now, we just wanted you to see what’s coming. Modifying the Application Delegate Interface Given our approach, we need to declare an outlet to our application’s root view controller on our application delegate. Single-click on SuperDBAppDelegate.h and add the code in bold: @interface SuperDBAppDelegate : NSObject <UIApplicationDelegate> { NSManagedObjectModel *managedObjectModel; NSManagedObjectContext *managedObjectContext; NSPersistentStoreCoordinator *persistentStoreCoordinator; UIWindow *window; UINavigationController *navController; } @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; @property (nonatomic, retain) IBOutlet UIWindow *window; CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 45 @property (nonatomic, retain) IBOutlet UINavigationController *navController; - (NSString *)applicationDocumentsDirectory; @end As you probably realized, the navController outlet will point to an instance of UINavigationController that will act as our application’s root view controller. Other view controllers will be pushed onto the navigation stack when they need to be displayed, and will be popped off of the stack when they are done. Adding to the Application Delegate Implementation Before we head over to Interface Builder, let’s quickly finish up with our Application delegate by adding the following code at the beginning of SuperDBAppDelegate.m: #import "SuperDBAppDelegate.h" @implementation SuperDBAppDelegate @synthesize window; @synthesize navController; #pragma mark - #pragma mark Application lifecycle - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch [window insertSubview:navController.view atIndex:0]; [window makeKeyAndVisible]; } … There shouldn’t be too much there that’s unfamiliar to you. We synthesize our new property, just as we always do. In our applicationDidFinishLaunching: method, we add the view property from navController, our application’s root view controller, as a subview of contentView so that it will be displayed to the user. Now, scroll down to the bottom of SuperDBAppDelegate.m. We need to add a few lines to the dealloc method to make sure we’re being good memory citizens. Make the following additions at the bottom of the file: … - (void)dealloc { [managedObjectContext release]; [managedObjectModel release]; [persistentStoreCoordinator release]; [window release]; [navController release]; [super dealloc]; CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 46 } @end Make sure you save both SuperDBAppDelegate.h and SuperDBAppDelegate.m before continuing. Creating the Table View Controller Our application’s root view controller is going to be a stock UINavigationController, so we don’t need to define a class for the app’s root view controller, but we do need to create a controller class to display the list of heroes and act as the root of the navigation controllers’ stack. Even though we will be using a table to display the list of heroes, we’re not going to subclass UITableViewController. Because we also need to add a tab bar to our interface, we’re going to create a subclass of UIViewController and create our interface in Interface Builder. The table that will display the list of heroes will be a subview of our view controller’s content pane. Single-click the Classes folder in the Groups & Files pane, then type N to bring up the new file assistant or select New File… from the File menu. When the new file assistant pops up (Figure 3-3), select Cocoa Touch Class from under the iPhone OS heading in the upper-left pane, then select UIViewController subclass from the upper-right pane. Now make sure the UITableViewController subclass check box is not checked, but the check box labeled With XIB for user interface check box is checked since, unlike with most table-based views, we will need a nib file. With that done, click the Next button. Figure 3-3. Selecting the Objective-C subclass template in the new file assistant CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 47 When prompted for a filename, type in HeroListViewController.m and make sure the check box labeled Also create “HeroListViewController.h” is checked. Press return to add the files to your project. After the files are created, click and drag HeroListViewController.xib from the Classes folder, where Xcode created it, to the Resources folder where it belongs. For now, that’s all we need in this controller class. In order to create an instance of the class in Interface Builder, we first needed the class definition to exist in Xcode. Setting up MainWindow.xib Interface Builder should now be open and should look something like Figure 3-4. You’re probably well-acquainted with Interface Builder by now, but let’s just quickly review the names of the various windows so that we’re all on the same page. The top-left window, the one with MainWindow.xib in the title bar, is the nib file’s main window. Below that, the window with the imaginative name of Window represents our application’s one and only instance of UIWindow. Double-clicking the Window icon in the nib file’s main window will reopen this if it gets closed. The window with the small title bar to the right of the nib’s main window is the context- sensitive Inspector where you can change the attributes of whatever item is currently selected in the active window. And finally, the right-most window is the Library, which contains pre-configured items that you can add to a nib. Figure 3-4. MainWindow.xib in Interface Builder CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 48 In the library, select the Controllers folder in the top-most pane (inside Library, then inside Cocoa Touch). With Controllers selected in the top pane, look in the middle pane for the Navigation Controller icon (Figure 3-5). Drag one of these to your nib file’s main window. Once you do that, your nib’s main window will gain an additional icon called Navigation Controller (or Navigation Co… if you’re in icon view mode, which truncates longer names), and a new window should have just popped up (Figure 3-6). Figure 3-5. The Navigation Controller icon. Depending on the version of Interface Builder you are using, the Library may default to displaying items in one of two ways. You might see just the icon (left), or the icon and a short description (right). You can change how the library items are displayed by right-clicking on the middle pane. Figure 3-6. Adding a Navigation Controller to your nib causes a new window to pop up The new window has a grey rounded rectangle with a dashed outline labeled View and a title of Root View Controller. This is Interface Builder’s way of reminding us that a navigation controller needs at least one child view controller in order to function. We can CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 49 set the root view controller right here in Interface Builder. The easiest way to do this is to put our nib’s main window in list mode by clicking the middle of the three View Mode icons (Figure 3-7). Figure 3-7. The nib’s main window in list view mode With your nib in list view mode, you should notice that Navigation Controller has a disclosure triangle next to it. That means it has sub-items of some form. Different items can contain different types of sub-items. Instances of view classes, for example, can contain subviews. View controller classes generally have either the views they control or the subordinate view controllers they’re responsible for managing (or both). Expand Navigation Controller by single-clicking its disclosure triangle. Underneath it, you’ll find a navigation bar instance, and a view controller with the rather long and unwieldy name of View Controller (Root View Controller). The view controller represents the navigation controller’s root view controller. As we said earlier, HeroListViewController was designed to act as the navigation controller’s root view controller. We need to change the class of the root view controller to HeroListViewController. Single-click View Controller (Root View Controller) and press 4 to bring up the identity inspector (Figure 3-8). Change the underlying class in the identity inspector from UIViewController to HeroListViewController. This will cause a single instance of HeroListViewController to get created when our application launches. CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 50 Figure 3-8. The identity inspector allows us to change the underlying class for the navigation controller’s root view controller to our custom controller class. Now we’ve got a navigation controller and an instance of our custom controller class in our nib. Connecting the Outlets Earlier we created an outlet in our application delegate for the navigation controller. We’ve added an instance of UINavigationController to our nib, so let’s connect the outlet. Control-drag from SuperDB App Delegate in the nib’s main window to the Navigation Controller also in the nib’s main window. When the black menu pops up, select the outlet called navController to connect that outlet. And with that, we have received final clearance to land our nib. Save and head on back to Xcode to pick up your luggage. Designing the Data Model As we discussed in Chapter 2, Xcode’s data model editor is where you design your application’s data model. In your project window’s Resources group, single-click on SuperDB.xcdatamodel. This should bring up the data model editor (Figure 3-9). [...]... a thousand, make sure to select Integer 16 rather than Integer 32 or Integer 64 The minimum and maximum values that these three datatypes are capable of storing is as follows: Datatype Minimum Maximum Integer 16 32 ,768 32 , 767 Integer 32 2, 147,4 83, 648 2, 147,4 83, 647 Integer 64 9 ,2 23 , 3 72, 036 ,854,775,808 9 ,2 23 , 3 72, 036 ,854,775,807 CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data At runtime,... testString; it's not needed Well, that is, it’s not needed if you’re compiling 64-bit Cocoa applications or iPhone applications Unfortunately, though, the iPhone simulator is a 32 -bit Mac application, and 32 -bit Mac applications cannot take advantage of instance variable synthesis That means iPhone programs running in the simulator cannot take advantage of this feature either In other words, the following... this to the space above the tab bar It should resize automatically to fit the space available After you drop it, it should look like Figure 3- 20 Figure 3- 19 The Table View in the library 65 66 CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data Figure 3- 20 The HeroListViewController interface after dropping the table on it With the table in place, the HeroListViewController interface is complete,... pane Once you click on it, you will be presented with a drop-down menu that looks like Figure 3- 12 Since we want to add an attribute, select Add Attribute from the menu Figure 3- 12 Clicking the plus button in the property pane gives you a menu from which you can select the type of property you wish to add CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data Editing the Attribute The Hero entity... for a tab bar (Figure 3- 16) Make sure you’re grabbing a tab bar and not a tab bar controller We only want the user interface item Figure 3- 16 The tab bar in the Library Drag a tab bar from the library to the window called View, and place it snugly in the bottom of the window, as we’ve done in Figure 3- 17 Figure 3- 17 The tab bar placed snugly against the bottom of the screen CHAPTER 3: A Super Start: Adding,... using regular expressions, which CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data are special text strings that you can use to express patterns You could, for example, use an attribute to store an IP address in text and then ensure that only valid numerical IP addresses are entered by entering the regular expression \b\d{1 ,3} \.\d{1 ,3} \.\d{1 ,3} \ \d{1 ,3} \b We’re not going to use regular expressions... displayed in the detail pane Also just like before, the Name field should have focus, so you can just type the new name for the attribute Type name now so that your detail pane looks like Figure 3- 13 Figure 3- 13 The detail pane after typing the new attribute’s name TIP: It’s not an accident that we chose to start our entity Hero with a capital H, but our attribute name with a lowercase n This is the... Title of By Name, and an Image of name_icon.png (Figure 31 8) Now click on the three dots above the word More on the tab bar to select the right tab Using the inspector, give this tab a Title of By Secret Identity and an Image of secret_icon.png Figure 3- 18 Setting the attributes of the left tab Back in the library, look for a Table View (Figure 3- 19) Again, make sure you’re getting the user interface... identity, date of birth, and sex We’ll add more data elements in future chapters, but this will give us a basic foundation upon which to build 51 52 CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data In the entity pane, which is the upper-left pane of the data model editor, you should notice buttons with a plus and a minus icon in the lower-left corner (Figure 3- 10) As you might have guessed, the...CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data Figure 3- 9 The empty data model editor awaiting your application’s data model Unlike the template we used in Chapter 2, this template provides us with a completely empty data model, so we can just dive right in and start building without . Integer 16 32 ,768 32 , 767 Integer 32 2, 147,4 83, 648 2, 147,4 83, 647 Integer 64 9 ,2 23, 3 72, 036 ,854,775,808 9 ,2 23, 3 72, 036 ,854,775,807 CHAPTER 3: A Super Start: Adding, Displaying, and Deleting. 32 or Integer 64. The minimum and maximum values that these three datatypes are capable of storing is as follows: Datatype Minimum Maximum Integer 16 32 ,768 32 , 767 Integer 32 2, 147,4 83, 648. project assistant (Figure 3- 2) . Figure 3- 2. Our dear old friend, Xcode’s new project assistant CHAPTER 3: A Super Start: Adding, Displaying, and Deleting Data 43 In the last chapter, we

Ngày đăng: 12/08/2014, 21:20

Từ khóa liên quan

Mục lục

  • Part 1: Core Data

    • A Super Start: Adding, Displaying, and Deleting Data

      • Setting up the Xcode Project

      • Creating the Table View Controller

      • Setting up MainWindow.xib

      • Designing the Data Model

        • Attribute Types

        • The Integer Datatypes

        • Setting the Name Attributes’s Type

        • Adding the Rest of the Attributes

        • Creating HeroListViewController

        • Let ‘Er Rip

        • Done, but Not Done

        • The Devil in the Detail View

          • Table-Based vs. Nib-Based Detail Views

          • Detail Editing View Challenges

          • Controlling Table Structure with Arrays

          • Paired Arrays

            • Representing

            • Formatting of Attributes

            • Creating the Detail View Controller

Tài liệu cùng người dùng

Tài liệu liên quan