委派和模式视图控制器
根据视图控制器编程指南,委托是关闭模态视图的首选方法。
按照苹果自己的食谱示例,我实现了以下内容,但不断收到警告,指出找不到 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];
}
我认为所有必需的元素都在那里并且在正确的位置?
谢谢
According to the View Controller Programming Guide, delegation is the preferred method to dismiss a modal view.
Following Apple's own Recipe example, i have implemented the following, but keep getting warnings that the addNameController:didAddName
method is not found...
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];
}
I think all the required elements are there and in the right place?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未指定
AddName
的delegate
属性必须符合NameDelegate
协议。在AddName.h:中使用此代码
You haven't specified that the
delegate
property ofAddName
has to conform to theNameDelegate
protocol.Use this code in AddName.h: