开发者

Understanding custom Delegate

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)finishedAddingFoodItemFro开发者_JAVA技巧mDetail:(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


In the code you posted, you dont set the delegate. You need to set it similar to this detailView.delegate = self, otherwise it is nil. You can send messages to a nil-object without any warning and error, nothing will happen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜