小编Sco*_*ott的帖子

为什么这个代表 - 协议没有响应?

我有一个名为dateSelectViewController的方法在我的.h文件中声明为协议:

@class DateSelectViewController;
@protocol DateSelectViewControllerDelegate 

- (void)dateSelectViewController:(DateSelectViewController *)sender
                         theDate:(id)stringDate;

@end
Run Code Online (Sandbox Code Playgroud)

在协议下面,我宣布一个代表:

@property (nonatomic, weak) id <DateSelectViewControllerDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

在实现文件中,我合成了委托,并在我的视图中按下完成按钮时向委托发送消息:

- (IBAction)DonePressed:(id)sender {
    NSDate *chosen = [datePicker date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"MM/dd/yyyy"];
    NSString *formatedDate = [formatter stringFromDate:chosen];

    //sending a message to the delegate
    [self.delegate dateSelectViewController:self theDate:formatedDate];

    [self.navigationController popViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)

在被委派给的.h文件中,我正在导入委托人.h文件.在.m文件中我符合协议:

@interface MakePlantTVC ()<DateSelectViewControllerDelegate>
- (void)dateSelectViewController:(DateSelectViewController *)sender
                     theDate:(id)stringDate
{
    self.displayDate.text = stringDate;
    NSLog(@"delegate working");
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,这一点正在发挥作用.当在我的委托者类中按下完成按钮时,该按钮会按预期执行并弹出视图控制器,但就像消息永远不会发送给代理一样.起初我以为我可以向nil发送消息,但是它的类型为id,所以情况应该不是这样.为什么没有发送消息?

delegates protocols objective-c ios

0
推荐指数
1
解决办法
2690
查看次数

标签 统计

delegates ×1

ios ×1

objective-c ×1

protocols ×1