0

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

@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 的答案,但它仍然不起作用。如果您需要更多信息,请询问。

4

2 回答 2

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
于 2013-10-09T06:29:13.590 回答
0

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

MyOtherViewController : UIViewController <ViewControllerDelegate>

你只能这样做:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    self.vc.delegate = self;
}
于 2013-10-09T17:48:46.000 回答