当我点击图片时,我会更改个人资料图片第一个屏幕,它将进入下一个屏幕,并在此请求从图库中选择或拍照。假设我从图库中选择,它进入第二个屏幕并且图像出现。我的问题是,当我在第二个屏幕上点击完成按钮时,图像将在第一个屏幕中更改,即个人资料图片。如何在第一个屏幕中更改,请回复我。
答案 0 :(得分:0)
在SecondViewController
课程中创建委托。在FirstViewController
类中分配并实现此委托。
当用户从图库中选择图片或点击照片时,请调用委托方法将所选图片传递给FirstViewController
课程。
实现此目的的示例代码如下
// Declare delegate
@protocol ImageSelectionDelegate <NSObject>
- (void) imageSelected:(UIImage*)image;
@end
@interface SecondViewController : UIViewController
// Delegate property
@property (nonatomic,assign) id<ImageSelectionDelegate> delegate;
@end
@implementation SecondViewController
// In case you are using image picker, this delegate is called once image selection is complete.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Use either according to your setting, whether you allow image editing or not.
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
//For edited image
//UIImage *myImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if([_delegate respondsToSelector:@selector(imageSelected:)]) {
[self.delegate imageSelected:myImage];
}
}
//OR
// Done button click
- (IBAction)doneButtonClick:(id)sender
{
// You can store the image selected by user in a UIImage pointer.
// Here I have UIImage *selectedImage as an ivar.
// Call the delegate and pass the selected image.
if([_delegate respondsToSelector:@selector(imageSelected:)]) {
[self.delegate imageSelected:selectedImage];
}
// Pop the view controller here
}
@end
#import "SecondViewController.h"
@interface FirstViewController : UIViewController <ImageSelectionDelegate>
@end
@implementation FirstViewController
- (void) onImageClick {
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
// Assign delegate
controller.delegate = self;
[self.navigationController pushViewController:controller animated:YES];
}
// Implement delegate method
- (void) imageSelected:(UIImage *)image {
// Use image
}
@end