我正在尝试使用代表通过视图控制器进行通信。这是我的代码:
@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 的答案,但它仍然不起作用。如果您需要更多信息,请询问。