【发布时间】: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吗?尝试将属性更改为weak或unsafe_unretained。 -
@jszumski 委托为空