Cocoa Programming for Mac OS X - Aaron Hillegass [119]
Fill in the body of fetchClasses::
- (IBAction)fetchClasses:(id)sender
{
[activityIndicator startAnimating];
[fetchButton setEnabled:NO];
ScheduleFetcher *fetcher = [[ScheduleFetcher alloc] init];
[fetcher fetchClassesWithBlock:
^(NSArray *classes, NSError *error) {
[fetchButton setEnabled:YES];
[activityIndicator stopAnimating];
if (classes) {
ScheduleViewController *svc;
svc = [[ScheduleViewController alloc]
initWithStyle:UITableViewStylePlain];
[svc setClasses:classes];
[self.navigationController pushViewController:svc
animated:YES];
}
else
{
UIAlertView *alert;
alert = [[UIAlertView alloc]
initWithTitle:@"Error Fetching Classes"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
}
}];
}
Build and run the application. Tap on the Fetch Classes button. Once the results are received, you will see the table-view controller being pushed onto the stack and the upcoming class schedule displayed in the table view.
Challenge
Modify RanchForecastTouch so that when the user taps on a class in the schedule, a Web view for that course is shown. Use the table-view delegate method tableView:didSelectRowAtIndexPath: to detect the tap; then push a UIViewController containing a UIWebView.
Chapter 31. View Swapping
Often, instead of bringing up a new window, you will simply want to swap out a view and replace it with another. One easy way to do this is to change the content view of a box.
Putting each view in its own XIB results in a more modular design. In Mac OS 10.5, Apple added the class NSViewController to Cocoa. We will make a subclass of NSViewController for each view that we want to swap in.
This project will, in the next chapter, evolve into a relatively sophisticated Core Data application, so we will want each of our view controllers to have access to an NSManagedObjectContext. Figure 31.1 shows where we are going.
Figure 31.1. Completed Application
The pop-up button will enable the user to jump back and forth between the two views. In this chapter, you are going to make the jumping back and forth part work. All the really useful parts of this app will be done in the next chapter.
The views (controlled by view controllers) will become the content view of a box. Menu items in the pop-up button will trigger the view swapping. Figure 31.2 is a diagram of the objects involved.
Figure 31.2. Object Diagram
Get Started
In Xcode, create a new Cocoa Application. Name it Departments, and enable Create Document-Based Application and Use Core Data. Set the Class Prefix to My. Open MyDocument.xib and add a box (from the Library’s Cocoa -> Layout Views) and a pop-up button to the window. Set the box’s title position and border type to None (Figure 31.3). This will make the box invisible. As we are using the box as a container for our two views, which we will be creating shortly, invisibility is fine.
Figure 31.3. Set the Box’s Title Position and Border Type to None.
Double-click the pop up button to open its menu. Remove all the menu items from the pop up button. You will create those programmatically.
In MyDocument.h, add two outlets, an array, and an action:
#import @interface MyDocument : NSPersistentDocument { IBOutlet NSBox *box; IBOutlet NSPopUpButton *popUp; NSMutableArray *viewControllers; } - (IBAction)changeViewController:(id)sender; @end Back in MyDocument.xib, control-click on File’s Owner to bring up the Connection panel, and set the two outlets. Now Control-drag from the pop-up button to File’s Owner to set its target. The action should be changeViewController:. Create the ManagedViewController Class
Create a new Objective-C class that subclasses NSViewController, and name it ManagingViewController.