为什么代表不工作?

时间:2014-03-19 08:11:34

标签: ios objective-c delegates delegation

我的代表:

@class UpdateUserSummary;
@protocol UpdateSummaryDelegate <NSObject>

- (void)updateSummaryDidCancel:(UpdateUserSummary*)controller;
- (void)updateSummaryDone:(UpdateUserSummary*)controller;

@end

在界面:

@interface UpdateUserSummary : UIViewController
@property (nonatomic, weak) id<UpdateSummaryDelegate> delegate;
@end


- (IBAction)done:(id)sender {
    [self.delegate updateSummaryDidCancel:self];
}

代表回复:

-(void)updateSummaryDidCancel:(UpdateUserSummary *)controller
{
    // just close modal vc
    [self dismissViewControllerAnimated:YES completion:nil];
 }

2 个答案:

答案 0 :(得分:2)

使用这个, UpdateUserSummary.delegate = self; 你在实现类中调用delegate的地方。

答案 1 :(得分:1)

@protocol MUpdateSummaryDelegate;

@interface MUpdateSummary : NSObject

@property (assign) id<UpdateSummaryDelegate> delegate; //You need this

@end


@protocol UpdateSummaryDelegate <NSObject>

-(void)updateSummaryDidCancel:(UpdateUserSummary *)controller;

@end

在您调用委托的类中,您需要设置委托。

MUpdateSummary *mUpdateSummary = [[MUpdateSummary alloc] init];
mUpdateSummary.delegate = self;