2013-06-20 3 views
1

프로필 사진을 변경하면 이미지를 클릭하면 다음 화면으로 넘어 가서 갤러리에서 선택하거나 사진을 찍으라는 메시지가 표시됩니다. 갤러리에서 선택한다고 가정하면, 두 번째 화면으로 들어가고 이미지가 나타납니다. 제 질문은 두 번째 화면에서 완료 버튼을 클릭하면 이미지가 첫 번째 화면에서 변경 될 것입니다. 즉 프로필 사진입니다. 첫 화면에서 변경 될 수있는 방법은 무엇입니까?이미지에서 완료 버튼을 클릭하면 이전 UIviewController에서 변경됩니까?

+0

두 번째보기 컨트롤러에서 이미지 선택 대리자를 만들고 첫 번째보기 컨트롤러로 할당합니다. 사용자가 이미지를 선택하면 해당 이미지를 대리자 콜백에 전달합니다. – Amar

+0

이미지 위임을 만드는 방법은 무엇입니까? – Vijayendra

+0

아래 내 대답을 확인 – Amar

답변

0

SecondViewController 클래스에 대리인을 만듭니다. 이 대리자를 FirstViewController 클래스에 할당하고 구현합니다.

사용자가 갤러리에서 이미지를 선택하거나 사진을 클릭하면 대리자 메서드를 호출하여 선택한 이미지를 FirstViewController 클래스로 전달합니다. 이를 구현하기 위해

샘플 코드는

다음과 같이 SecondViewController.h

// Declare delegate 
@protocol ImageSelectionDelegate <NSObject> 
- (void) imageSelected:(UIImage*)image; 
@end 

@interface SecondViewController : UIViewController 
// Delegate property 
@property (nonatomic,assign) id<ImageSelectionDelegate> delegate; 
@end 

SecondViewController.m

@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 

FirstViewController.h

#import "SecondViewController.h" 

@interface FirstViewController : UIViewController <ImageSelectionDelegate> 

@end 

FirstView입니다 Controller.m

@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 
+0

왜 secondviewcontroller 밀어. secondviewcontroller 완료 단추가 있습니다. 그래서 우리는 secondviewcontroller에서 팝업 할 수 있습니까? – Vijayendra

+0

두 번째보기 컨트롤러를 모달로 표시하면 괜찮습니다! 네비게이션 스택을 밀어 넣을 필요가 없습니다. – Amar

+0

아니요. secondviewcontroler에서 done 버튼을 클릭하면 firstrviewcontroler에서 변경되므로 popviewcontroller를 사용할 수 있습니까? – Vijayendra

관련 문제