Я создаю контроллер представления, который будет использоваться для ввода текстовой информации.
Сам вид состоит из метки, текстового поля и двух кнопок на панели навигации: «отмена» и «ОК».
Когда пользователь нажимает «Отмена», я просто возвращаюсь к контроллеру корневого представления.
Но когда он нажимает ОК, я хочу сначала вызвать функцию из контроллера корневого представления и только после этого вернуться обратно.
Я пытался реализовать это следующим образом:
Заголовок:
@interface UserInputViewController : UIViewController {
UILabel *textLabel;
UITextField *textField;
SEL OKButtonAction;
}
-(NSString*) getEnteredText;
-(UserInputViewController*) initWithTitle: (NSString*)title Text: (NSString*)text andOKButtonSelector: (SEL) OKButtonSelector;
@end
Реализация:
@implementation UserInputViewController
-(UserInputViewController*) initWithTitle: (NSString*)title Text: (NSString*)text andOKButtonSelector: (SEL) OKButtonSelector
{
self = [self init];
self.title = title;
OKButtonAction = OKButtonSelector;
textLabel = [ [UILabel alloc] initWithFrame: CGRectMake(20, 20, 280, 50)];
[textLabel setText: text];
[ [self view] addSubview: textLabel];
textField = [ [UITextField alloc] initWithFrame: CGRectMake(20, 100, 280, 50)];
[ [self view] addSubview: textField];
return self;
}
-(NSString*) getEnteredText
{
return [textField text];
}
-(void) popToRootViewController
{
[ [self navigationController] popToRootViewControllerAnimated: YES ];
}
-(void) popToRootWithOKAction
{
[ [self navigationController] popToRootViewControllerAnimated: YES ];
[self performSelector: OKButtonAction];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Cancel button
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"cancel button", @"") style: UIBarButtonSystemItemCancel target: self action: @selector(popToRootViewController) ];
[ [self navigationItem] setLeftBarButtonItem: cancelButton animated: NO];
[cancelButton release];
//OK button
UIBarButtonItem *OKButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"ok button", @"") style: UIBarButtonSystemItemSave target: self action: @selector(popToRootWithOKAction) ];
[ [self navigationItem] setRightBarButtonItem: OKButton animated: NO];
[OKButton release];
}
А вот методы контроллера корневого представления:
-(void) OKButtonAction
{
NSLog(@"text: %@", [newProfileDialog getEnteredText]);
[newProfileDialog release];
}
-(void) add_clicked {
newProfileDialog = [ [UserInputViewController alloc] initWithTitle: @"User name" Text: @"Please enter a new user name:" andOKButtonSelector: @selector(OKButtonAction)];
[ [self navigationController] pushViewController: newProfileDialog animated: YES];
}
Но когда я скомпилировал его и нажал кнопку OK в режиме просмотра, я получил исключение.
Я еще не знаком с программированием селекторов, поэтому мне трудно понять, что я делаю неправильно.
Как мне достичь этой цели?
Спасибо.