【问题标题】:Custom delegate not getting called自定义委托没有被调用
【发布时间】:2013-10-10 01:48:46
【问题描述】:

我正在尝试使用 Delegates 通过视图控制器进行通信。这是我的代码:

@protocol ViewControllerDelegate;
@interface ViewController : UIViewController <UITextFieldDelegate>{


    IBOutlet UIDatePicker *dateTimePicker;

}
@property (nonatomic, strong) IBOutlet UITextField *className;
@property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
@property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
@property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
@property (nonatomic, strong) IBOutlet UILabel *notificationOnOffLabel;
@property (nonatomic, assign)  id <ViewControllerDelegate> delegate;
@property (nonatomic,strong)classObject *classObject;
-(IBAction) addButton:(id)sender;


@end
@protocol ViewControllerDelegate <NSObject>

-(void)assignmentSaved:(classObject *)classObj;

在行动:

[self.delegate assignmentSaved:self.classObject];

在其他视图控制器中:

-(ViewController *)vc
{
    if (!_vc) {
        _vc = [[ViewController alloc]init];
    }

    return _vc;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.vc.delegate = self;
}

-(void)assignmentSaved:(classObject *)classObj
{
    NSLog(@"%@",classObj.description);
    classObj2 = classObj;
    [self.tableView reloadData];
}

vc 是View Controller 的属性。当我 NSLog 委托时,委托最终为零。我也尝试了@mialkan 的答案,但它仍然不起作用。如果您需要更多信息,请询问。

【问题讨论】:

  • 到底是什么问题?是self.delegatenil?你没有看到assignmentSaved:的日志吗?
  • @jszumski 问题是委托方法没有被调用。
  • @jszumski 不,我没有看到 NSLog
  • nil吗?尝试将属性更改为weakunsafe_unretained
  • @jszumski 委托为空

标签: ios delegates


【解决方案1】:

我对您的代码进行了一些更改,不要忘记 @synthesize delegate; 并且不要在您创建 ViewController 的地方访问 setDelegate:self。

@protocol ViewControllerDelegate;
@interface ViewController : UIViewController <UITextFieldDelegate>{


    IBOutlet UIDatePicker *dateTimePicker;
    id <ViewControllerDelegate> delegate;

}
@property (nonatomic, strong) IBOutlet UITextField *className;
@property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
@property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
@property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
@property (nonatomic, strong) IBOutlet UILabel *notificationOnOffLabel;
@property (nonatomic, assign)  id <ViewControllerDelegate> delegate;
@property (nonatomic,strong)classObject *classObject;
-(IBAction) addButton:(id)sender;


@end
@protocol ViewControllerDelegate

-(void)assignmentSaved:(classObject *)classObj;

@end

【讨论】:

  • 我认为你的问题是你不能 setDelegate:self。在您的 viewDidLoad 方法中检查 cv 是否为零?我尝试了代表团,它奏效了。
  • 我确实将委托设置为 self...vc 不为零。
  • 一定要使用委托方法吗?您可以使用通知中心
  • 是的,我必须使用委托
  • 你能提供样例项目来看看吗?
【解决方案2】:

在第一个视图中,您是否订阅了您的委托

MyOtherViewController : UIViewController <ViewControllerDelegate>

你只能这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (!_vc) 
        _vc = [[ViewController alloc]init];

    self.vc.delegate = self;
}

【讨论】:

  • _vc = [[ViewController alloc]init];你的初始化方法是什么?这是一个用 xib 创建的视图?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 2022-01-22
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多