Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 11: iPad Considerations

391

recommendation is generally to let your users choose for themselves which way is up. Unless you’re making a game, where you want to force the display to a specific orientation, iPad apps nearly always want this method to return YES.

The Detail View Controller

The final class created for us by Xcode is BIDDetailViewController, which takes care of the actual display of the item the user chooses. Here’s what BIDDetailViewController.h looks like:

#import <UIKit/UIKit.h>

@interface BIDDetailViewController : UIViewController <UISplitViewControllerDelegate> @property (strong, nonatomic) id detailItem;

@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel; @end

Apart from the detailItem property that we’ve seen referenced before (in the

BIDMasterViewController class), BIDDetailViewController also has an outlet for connecting to a label in the storyboard (detailDescriptionLabel).

Switch over to BIDDetailViewController.m, where you’ll find the following (once again, somewhat abridged):

#import "BIDDetailViewController.h"

@interface BIDDetailViewController ()

@property (strong, nonatomic) UIPopoverController *masterPopoverController; - (void)configureView;

@end

@implementation BIDDetailViewController

@synthesize detailItem = _detailItem;

@synthesize detailDescriptionLabel = _detailDescriptionLabel; @synthesize masterPopoverController = _masterPopoverController;

- (void)setDetailItem:(id)newDetailItem

{

if (_detailItem != newDetailItem) { _detailItem = newDetailItem;

// Update the view. [self configureView];

}

if (self.masterPopoverController != nil) { [self.masterPopoverController dismissPopoverAnimated:YES];

}

}

- (void)configureView

www.it-ebooks.info

392

CHAPTER 11: iPad Considerations

{

// Update the user interface for the detail item.

if (self.detailItem) {

self.detailDescriptionLabel.text = [self.detailItem description];

}

}

.

.

.

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib. [self configureView];

}

.

.

.

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations return YES;

}

-(void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController

{

barButtonItem.title = NSLocalizedString(@"Master", @"Master"); [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; self.masterPopoverController = popoverController;

}

-(void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem

{

//Called when the view is shown again in the split view, invalidating the button and popover controller.

[self.navigationItem setLeftBarButtonItem:nil animated:YES]; self.masterPopoverController = nil;

}

@end

Much of this should look familiar to you, but this class contains a few items worth going over. The first of these is something called a class extension, declared near the top of the file:

@interface BIDDetailViewController ()

@property (strong, nonatomic) UIPopoverController *masterPopoverController; - (void)configureView;

@end

www.it-ebooks.info

CHAPTER 11: iPad Considerations

393

We’ve talked a bit about class extensions before, but their purpose is worth mentioning again. Creating a class extension lets you define some methods and properties that are going to be used within your class but that you don’t want to expose to other classes in a header file. Here, we’ve declared a popoverController property, which will make use of the instance variable we declared earlier, and a utility method, which will be called whenever we need to update the display. We still haven’t told you what the masterPopoverController property is meant to be used for, but we’re getting there!

Just a bit farther down, you’ll see this method:

- (void)setDetailItem:(id)newDetailItem

{

if (_detailItem != newDetailItem) { _detailItem = newDetailItem;

// Update the view. [self configureView];

}

if (self.masterPopoverController != nil) { [self.masterPopoverController dismissPopoverAnimated:YES];

}

}

The setDetailItem: method may seem surprising to you. We did, after all, define detailItem as a property, and we synthesized it to create the getter and setter for us, so why create a setter in code? In this case, we need to be able to react whenever the user calls the setter (by selecting a row in the master list on the left) so that we can update the display, and this is a good way to do it. The first part of the method seems pretty straightforward, but at the end it diverges into a call to dismiss the current masterPopoverController, if there is one. Where in the world is that hypothetical masterPopupController coming from? Scroll down a bit, and you’ll see that this method contains the answer:

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController

{

barButtonItem.title = NSLocalizedString(@"Master", @"Master"); [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; self.masterPopoverController = popoverController;

}

This is a delegate method for UISplitViewController. It’s called when the split view controller is no longer going to show the left side of the split view as a permanent fixture (that is, when the iPad is rotated to portrait orientation). The first thing this method does is configure the title displayed in barButtonItem’s title, using the NSLocalizedString function, which gives you a chance to make use of text strings in other languages, if you’ve prepared any. We’ll talk more about localization issues in Chapter 21, but for now, all you need to know is that one parameter is basically a key that the function uses to retrieve a localized string from a dictionary, and the other is a fallback value that will be used in case no other value is found.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]