Graphics Programs Reference
In-Depth Information
In
DetailViewController.h
, declare this initializer.
}
- (id)initForNewItem:(BOOL)isNew;
@property (nonatomic, strong) BNRItem *item;
If the
DetailViewController
is being used to create a new
BNRItem
, we want it
to show a
Done
button and a
Cancel
button on its navigation item. Implement this method
in
DetailViewController.m
.
- (id)initForNewItem:(BOOL)isNew
{
self = [super initWithNibName:@"DetailViewController" bundle:nil];
if (self) {
if (isNew) {
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(save:)];
[[self navigationItem] setRightBarButtonItem:doneItem];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel:)];
[[self navigationItem] setLeftBarButtonItem:cancelItem];
}
}
return self;
}
In the past, when you've changed the designated initializer of a class from its superclass's
designated initializer, you've overridden the superclass's initializer to call the new one. In
this case, you're just going to make it illegal to use the superclass's designated initializer
by throwing an exception when anyone calls it.
In
DetailViewController.m
, override
UIViewController
's designated initial-
izer.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
@throw [NSException exceptionWithName:@"Wrong initializer"
reason:@"Use initForNewItem:"
userInfo:nil];
return nil;
}
This code creates an instance of
NSException
with a name and a reason and then
throws the exception. This halts the application and shows the exception in the console.