了解自定义委托
所以我有一个应用程序,应用程序中有一个 tableView
,我有一个 uinavigationbarbutton
来呈现模式 viewController
。当用户点击模态界面中的“执行”按钮时,我希望它关闭模态视图并在模态视图中获取一些信息。我会将这些信息放入 tableView 中。为此,我编写了一个自定义委托,但它不起作用。我在下面包含了我的代码。感谢您的任何帮助。
TrackerMainViewController.h //tableView
#import "NewItemViewController.h"
@interface TrackerMainViewController : UITableViewController <UITableViewDelegate, DetailDelegate>
TrackerMainViewController.m
-(void)finishedAddingFoodItemFromDetail:(NSDate *)date whatWasEaten:(NSString *)whatFood whichMeal:(NSString *)meal {
NSLog(@"in delegate method here");
[self.tableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}
NewItemViewController.h // 模态视图
@protocol DetailDelegate <NSObject>
-(void)finishedAddingFoodItemFromDetail:(NSDate *)date whatWasEaten:(NSString *)whatFood whichMeal:(NSString *)meal;
@end
@interface NewItemViewController : UIViewController {
id <DetailDelegate> _delegate;
}
@property (nonatomic, retain) id <DetailDelegate> delegate;
@end
NewItemViewController.h
@implementation NewItemViewController
@synthesize delegate = _delegate;
//the go button in the modal view
- (IBAction)Go:(id)sender {
[self.delegate finishedAddingFoodItemFromDetail:[NSDate date] whatWasEaten:@"chicken" whichMeal:@"breakfast"];
}
我在 go 按钮和 tableview 中的委托实现中都放置了一个日志,但只有 go 日志被调用。
谢谢
So I have an app, and in the app there is a tableView
, I have a uinavigationbarbutton
that presents a modal viewController
. When the user hits a go button in the modal interface, I want it dismiss the modal view and get some of the information in the modal view. I will than put that info in the tableView. To do this, I wrote a custom delegate, but it doesn’t work. I included my code below. Thanks for any help.
TrackerMainViewController.h //the tableView
#import "NewItemViewController.h"
@interface TrackerMainViewController : UITableViewController <UITableViewDelegate, DetailDelegate>
TrackerMainViewController.m
-(void)finishedAddingFoodItemFromDetail:(NSDate *)date whatWasEaten:(NSString *)whatFood whichMeal:(NSString *)meal {
NSLog(@"in delegate method here");
[self.tableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}
NewItemViewController.h // the modal view
@protocol DetailDelegate <NSObject>
-(void)finishedAddingFoodItemFromDetail:(NSDate *)date whatWasEaten:(NSString *)whatFood whichMeal:(NSString *)meal;
@end
@interface NewItemViewController : UIViewController {
id <DetailDelegate> _delegate;
}
@property (nonatomic, retain) id <DetailDelegate> delegate;
@end
NewItemViewController.h
@implementation NewItemViewController
@synthesize delegate = _delegate;
//the go button in the modal view
- (IBAction)Go:(id)sender {
[self.delegate finishedAddingFoodItemFromDetail:[NSDate date] whatWasEaten:@"chicken" whichMeal:@"breakfast"];
}
I put a log in both the go button and in the implementation of the delegate in the tableview, but only the go log is being called.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您发布的代码中,您没有设置委托。你需要像这个
detailView.delegate = self
一样设置它,否则它是nil
。您可以向nil
对象发送消息,而不会出现任何警告和错误,什么也不会发生。In the code you posted, you dont set the delegate. You need to set it similar to this
detailView.delegate = self
, otherwise it isnil
. You can send messages to anil
-object without any warning and error, nothing will happen.