2013-04-30 4 views
3

UITableView에 게시글을 볼 수있는 부모보기가 있습니다. 그것의 Navigation Bar에서 나는 눌렀을 때 UIView subclass을 선물하고 스크린의 상단에 그것을 보여주는 포스트 버튼이 있습니다. 나는 UIView에 대한 이미지를 가지고 있었고, 사용자가 서비스에 게시하기 위해 이미지를 선택할 수 있도록 UIImagePickerController을 선물하려고합니다. 내 하위보기 UIImagePickerController 제시 할 수없는보기 컨트롤러가 아니기 때문에이 작업을 수행 할 수 있습니다.UIImagePickerController를 표시하려고하는 UIView (하위 클래스)

아래는 내 서브 뷰 코드입니다.

#import "PostView.h" 

    @implementation PostView 

    @synthesize attachedLabel; 
    @synthesize postButton; 
    @synthesize textView; 
    @synthesize characterLimit; 
    @synthesize attachImage; 

    - (id)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 
      originalFrame = frame; 
      NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"PostView" owner:self options:nil]; 
      PostView *view = [xib objectAtIndex:0]; 
      [view setBackgroundColor:[UIColor whiteColor]]; 
      [view setAlpha:0.7f]; 
      attachedLabel = [[UILabel alloc] initWithFrame:CGRectMake(204, 212, 56, 21)]; 
      attachedLabel.textColor = [UIColor blackColor]; 
      [attachedLabel setText:@"Attached"]; 
      attachedLabel.backgroundColor = [UIColor clearColor]; 
      attachedLabel.font = [UIFont fontWithName:text_font_name size:12.0]; 
      characterLimit = [[UILabel alloc] initWithFrame:CGRectMake(246, 13, 50, 21)]; 
      [characterLimit setTextAlignment:NSTextAlignmentRight]; 
      characterLimit.textColor = [UIColor blackColor]; 
      characterLimit.backgroundColor = [UIColor clearColor]; 
      characterLimit.font = [UIFont fontWithName:text_font_name size:12.0]; 
      attachImage = [[UIImageView alloc] initWithFrame:CGRectMake(270, 208, 30, 30)]; 
      [attachImage setImage:[UIImage imageNamed:@"attachphoto30x30.png"]]; 
      [self.textView setDelegate:self]; 
      [self.textView setAlpha:0.7f]; 
      [self.textView setTextColor:[UIColor whiteColor]]; 
      [self.textView setBackgroundColor:[UIColor clearColor]]; 
      self.layer.cornerRadius = 10.0f; 
      self.layer.masksToBounds = YES; 
      [self addSubview:view]; 
      [self addSubview:characterLimit]; 
      [self addSubview:attachedLabel]; 
      [self addSubview:attachImage]; 
     } 
     return self; 
    } 

    - (IBAction)openCamera:(id)sender 
    { 
     UIImagePickerController *controller = [[UIImagePickerController alloc] init]; 
     controller.delegate = self; 
     //[self presentViewController:controller animated:YES completion:nil]; 
     NSLog(@"%@", @"Image Tapped"); 
    } 

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info 
    { 
     /*[picker dismissViewControllerAnimated:YES completion:nil]; 
     UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage]; 
     UIImage *scale = [image scaleToSize:CGSizeMake(320.0f, 548.0f)]; 
     imageData = UIImageJPEGRepresentation(scale, 1); 
     encodedImage = [self Base64Encode:imageData]; 
     [attachedLabel setHidden:NO]; 
     */ 
    } 

    #pragma mark Custom alert methods 

    - (IBAction)postAction:(id)sender 
    { 
     [self hide]; 
    } 

    - (void)show 
    { 
     //prepare attachImage 
     attachImage.userInteractionEnabled = YES; 
     UITapGestureRecognizer *tapAttach = [[UITapGestureRecognizer alloc] 
              initWithTarget:self action:@selector(openCamera:)]; 
     tapAttach.numberOfTapsRequired = 1; 
     [self.attachImage addGestureRecognizer:tapAttach]; 


     isShown = YES; 
     self.transform = CGAffineTransformMakeScale(0.1, 0.1); 
     self.alpha = 0; 
     [UIView beginAnimations:@"showAlert" context:nil]; 
     [self setBackgroundColor:[UIColor clearColor]]; 
     [UIView setAnimationDelegate:self]; 
     self.transform = CGAffineTransformMakeScale(1.1, 1.1); 
     self.alpha = 1; 
     [UIView commitAnimations]; 
    } 

    - (void)hide 
    { 
     isShown = NO; 
     [UIView beginAnimations:@"hideAlert" context:nil]; 
     [UIView setAnimationDelegate:self]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"hidePostView_Notification" object:nil]; 
     self.transform = CGAffineTransformMakeScale(0.1, 0.1); 
     self.alpha = 0; 
     [UIView commitAnimations]; 
    } 

    - (void)toggle 
    { 
     if (isShown) 
     { 
      [self hide]; 
     } else 
     { 
      [self show]; 
     } 
    } 

    #pragma mark Animation delegate 

    - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
    { 
     if ([animationID isEqualToString:@"showAlert"]) 
     { 
      if (finished) 
      { 
       [UIView beginAnimations:nil context:nil]; 
       self.transform = CGAffineTransformMakeScale(1.0, 1.0); 
       [UIView commitAnimations]; 
      } 
     } else if ([animationID isEqualToString:@"hideAlert"]) 
     { 
      if (finished) 
      { 
       self.transform = CGAffineTransformMakeScale(1.0, 1.0); 
       self.frame = originalFrame; 
      } 
     } 
    } 

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
    { 
     return YES; 
    } 


    - (BOOL)textView:(UITextView *)textViewer shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string 
    { 
     if ([string isEqualToString:@"\n"]) 
     { 
      [textViewer resignFirstResponder]; 
     } 
     return [self isAcceptableTextLength:textViewer.text.length + string.length - range.length]; 
    } 

    -(IBAction)checkIfCorrectLength:(id)sender 
    { 
     if (![self isAcceptableTextLength:self.textView.text.length]) 
     { 
      // do something to make text shorter 
     } 
    } 

    - (BOOL)isAcceptableTextLength:(NSUInteger)length 
    { 
     return length <= 160; 
    } 


    - (void)textViewDidChange:(UITextView *)textViewer 
    { 
     NSString *characters = [[NSString stringWithFormat:@"%d", textViewer.text.length] stringByAppendingString:@"/160"]; 
     NSLog(@"%@", characters); 
     [self updateDisplay:characters]; 
    } 

    -(void) updateDisplay : (NSString *)str 
    { 
     [self.characterLimit performSelectorOnMainThread : @ selector(setText :) withObject:str waitUntilDone:YES]; 
    } 

    @end 
+0

UIImagePickerController를 제시하기 위해 시도하고 나에게 결과를 알려 subview.m 파일 내부에 parentview의 controller.h를 가져올 수 있는지 확인 [(YourParentViewController *) self.superview nextResponder] presentViewController : 컨트롤러가 애니메이션 : 예 완료 : 없음]; –

+0

어떻게 uView 및 내 parentviewcontroller 하위보기에서 FirstViewController 같은 다른 viewController "ViewController"... –

답변

5

예, 당신이 UIView의 서브 클래스에서의 ViewController을 제시 할 수 없다.

이 문제를 해결하려면 하위보기의 수퍼 뷰의 viewcontroller 클래스를 사용할 수 있습니다.
하위 뷰에 [self.superview nextResponder]을 호출하면 superview의 viewcontroller가 반환됩니다.
이것을 사용하여 UIImagePicker보기 컨트롤러를 표시 할 수 있습니다.

presentViewController 메소드를 사용하려면 [self.superview nextResponder]를 parentviewcontroller 클래스 유형으로 캐스팅해야합니다. 또한
당신이

[(YourParentViewController *)[self.superview nextResponder] presentViewController:controller animated:YES completion:nil]; 
+0

하위 뷰 m 파일 내에서 상위 뷰를 가져오고 h 파일을 가져 오지 않도록하십시오. 그렇지 않으면 컴파일 오류가 발생합니다. – Jarod

+0

예, 물론입니다. 고맙습니다. –

+0

다른 질문을해야하는지, 아니면 조금 쌓을 지 모르겠습니다. 나는 이걸 가지고 놀고 있는데, 이미지를 선택하면 imageData = UIImageJPEGRepresentation (image, 1); . 그런 다음 base64 인코딩을 사용하여 이미지의 NSString 표현을 가져와 json 메시지를 사용하여 편안한 서비스로 보냅니다. self.encodedImage = [[NSMutableString alloc] initWithString : [self Base64Encode : self.imageData]]; .....이 함수에서는 모든 것이 잘 설정되지만 포스트 버튼을 누르면 encodedImage는 null이되고 호가 유지되는 것을 허용하지 않습니다. – Jarod

0

당신은 UIViewController 서브 클래스보다는 UIView 서브 클래스를 제공한다.

나는 또한 UIViewController이보기를 위해 데이터 및 운영 논리를 처리해야한다고 말하고 싶습니다. 워드 프로세서의 일부를 체크 아웃 :

보기 컨트롤러 기본 사항 : http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html

UIViewController 클래스 참조 : http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html

+0

내가 CGRectMake (5, 50, 310, 245) 수 있도록 그 UIViewController 하위 프레임을 설정할 수 있습니다 변경할 경우 아래에서 다루지 않습니다. 나는 효과를 극복하고 아직도 데이터가 그 아래에 보이고 있음을 보여주고 싶다. – Jarod

+0

그렇게 할 수는 있지만,'UIViewController'를 서브 클래스하는 법과 그 내용을 구성하는 뷰가 어떻게/어떻게 그려지는지에 관해 읽을 필요가 있습니다. UIViewController 만 표현하는 대신 UIViewController를 사용해야한다고 확신합니다. – Aaron

+0

보기 컨트롤러를 하위보기로 표시 할 수 없기 때문에 이것이 옳다는 것을 확신하지 못합니다. 즉, 제시하고 밀어 넣어야 할 것입니다. – Jarod

관련 문제