在我的应用程序中,我正在尝试将数据从父视图传递到子视图。但是,当我运行应用程序时,没有调用委托方法。 以下是我如何实现自定义委托的代码: parent.h
@protocol SLBWallViewControllerDelegate <NSObject>
- (void) pictureToBeUploaded:(id)picture;
@end
@interface SLBWallViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)createPotButtonPressed:(id)sender;
@property (weak, nonatomic) id <SLBWallViewControllerDelegate> delegate;
@end
parent.m
[self.delegate pictureToBeUploaded:info[UIImagePickerControllerEditedImage]];//i'm taking the pic from a uiimagepicker
child.h
@interface SLBPostViewController : UIViewController <SLBWallViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *picture;
@end
child.m
#pragma mark - Wall View Controller Delegate
- (void)pictureToBeUploaded:(id)picture{
self.picture.image = picture;
}
有什么不对或丢失吗?
答案 0 :(得分:1)
嗯,问题可能是(编辑:在评论中确认)是self.delegate
此时为nil
。因此,您向nil
发送消息,当然没有任何反应。您要做的是确保在尝试发送消息之前将Child
实例分配给delegate
属性。像这样的东西:
//...
self.delegate = //.. a 'Child' instance
//...
[self.delegate pictureToBeUploaded:info[UIImagePickerControllerEditedImage]];
答案 1 :(得分:0)
//(1)在.h文件中的Child中创建一个属性
@property(非原子,强)UIImage * someImage;
//(2)在.m文件中的Child中覆盖属性
- (void)setSomeImage:(UIImage *)someImage {
_someImage=someImage;
self.picture.image = self.someImage;
}
//(3)在.m文件中的父级中将图像设置为子级的'someImage'属性
childsObject.someImage = UIImagePickerControllerEditedImage;