根据View Controller编程指南,委派是解除模态视图的首选方法。
按照Apple自己的Recipe示例,我已实现以下内容,但不断收到警告:addNameController:didAddName
方法未找到...
NameDelegate.h
@protocol NameDelegate
- (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name;
@end
AddName.h
@interface AddName : UIViewController {
UITextField *nameField;
id delegate;
}
- (IBAction)doneAction;
- (id)delegate;
- (void)setDelegate:(id)newDelegate;
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@end
AddName.m
- (IBAction)doneAction {
[delegate addNameController:self didAddName:[nameField text]];
}
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
ItemViewController.h
#import "NameDelegate.h"
@interface ItemViewController : UITableViewController <NameDelegate>{
}
@end
ItemViewController.m
- (void)addItem:(id)sender {
AddName *addName = [[AddName alloc] init];
addName.delegate = self;
[self presentModalViewController:addName animated:YES];
}
- (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name {
//Do other checks before dismiss...
[self dismissModalViewControllerAnimated:YES];
}
我认为所有必需的元素都存在于正确的位置?
由于
答案 0 :(得分:4)
您尚未指定delegate
的{{1}}属性必须符合AddName
协议。
在 AddName.h:
中使用此代码NameDelegate