我得到了下一个问题我有3个视图(UITableView和2 View Controller)我制作了下两个协议:AddViewControllerDelegate和ViewAlarmControllerDelegate: AddViewController.h:
@protocol AddViewControllerDelegate <NSObject>
- (void)addAlarm:(AlarmObject *)alarm;
@end
@interface AddViewController : UIViewController
{
id <AddViewControllerDelegate> delegate;
}
@property (retain) id delegate;
ViewAlarmController.h:
@protocol ViewAlarmControllerDelegate <NSObject>
- (void)changeAlarm:(AlarmObject *)alarm atIndex:(int) alarmID;
- (void)deleteAlarmAtIndex:(int) alarmID;
@end
@interface ViewAlarmController : UIViewController
{
id <ViewAlarmControllerDelegate> delegate;
}
@property (retain) id delegate;
然后在HomeViewController.h中导入为fallow:
#import "AddViewController.h"
#import "ViewAlarmController.h"
#import "AlarmObject.h"
@interface HomeViewController : UITableViewController <AddViewControllerDelegate, ViewAlarmControllerDelegate>
我在HomeViewController.m中有下一个方法声明
- (void)addAlarm:(AlarmObject *)alarm{
[self.items addObject:alarm];
[self.tableView reloadData];
NSLog (@"added");
}
- (void)deleteAlarmAtIndex:(int)alarmID{
NSLog(@"alarmID: %d", alarmID);
[self.items removeObjectAtIndex:alarmID];
NSLog(@"items: %@", items);
[self.tableView reloadData];
}
- (void)changeAlarm:(AlarmObject *)alarm atIndex:(int)alarmID{
NSLog(@"change");
}
ViewAlarmController.m调用协议的方法代码块:
- (IBAction)save:(id)sender {
AlarmObject* alarmObj = [[AlarmObject alloc]init];
if(![self.alarmNameField.text isEqual: @""]){
[alarmObj setValue:self.alarmNameField.text forKey:@"alarmName"];
NSLog (@"change alarm id = %d", alarmID);
[self.delegate changeAlarm:alarmObj atIndex:alarmID];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)reverse:(id)sender {
alarmNameField.text = [alarm valueForKey:@"alarmName"];
}
- (IBAction)deleteAlarm:(id)sender {
NSLog (@"delete alarm id: %d",alarmID);
[self.delegate deleteAlarmAtIndex:alarmID];
[self dismissViewControllerAnimated:YES completion:nil];
}
问题是addAlarm有效,但当我进入ViewAlarm并按下按钮删除或保存更改时,不会调用deleteAlarmAtIndex和changeAlarm方法。
问题出在下一个方法中:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"addAlarmSegue"]){
AddViewController *avc = segue.destinationViewController;
avc.delegate = self;
}else if ([segue.identifier isEqualToString:@"viewAlarmSegue"]){
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
ViewAlarmController *vac = segue.destinationViewController;
[vac getObject:[self.items objectAtIndex:selectedRowIndex.row] withId:selectedRowIndex.row];
vac.delegate = self;
}
}
我没有写vac.delegate = self;
答案 0 :(得分:0)
确保符合您协议的对象不是nil
My1Object *a = [My1Object new];
My2Object <Myprotocol> *b = [My2Object new];
a.delegate = b;
@interface My1Object <MyProtocol>
@property (nonatomic, weak /* or 'unsafe_unretained' for iOS less than 5.0*/) id <MyProtocol> delegate;
@end
@implementation My1Object
...
- (void)somethingHappened {
// just for check as I said:
NSLog(@"self.delegate = %@", self.delegate); // make sure this is not nil!
if ([self.delegate respondsToSelector:@selector(myProtocolMethod:)]) {
[self.delegate myProtocolMethod:self];
}
}
...
@end
请记住,你不应该保留(强)你的代表!