2013-04-30 53 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试试这个,让我知道结果,[(YourParentViewController *)[self.superview nextResponder] presentViewController:控制器动画:是完成:无]; – 2013-04-30 16:45:41

+0

如何从uiview的子类和我的parentviewcontroller呈现另一个viewController像FirstViewController是“ViewController”... – 2014-10-30 12:32:24

回答

5

是的,你不能从一个UIView子类呈现视图 - 控制。

要解决此问题,您可以使用您的子视图的超级视图的viewcontroller类。
在您的子视图中调用[self.superview nextResponder]将返回您superview的viewcontroller。
使用它可以呈现您的UIImagePicker视图控制器。

要使用presentViewController方法,您应该将[self.superview nextResponder]强制转换为您的parentviewcontroller的类类型。
还要确保您导入parentview或者Controller.h subview.m文件中

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

确保您在子视图m文件中导入父视图,而不是h文件,否则您将收到编译错误。 – Jarod 2013-04-30 17:14:24

+0

是的,当然。谢谢 – 2013-04-30 17:15:40

+0

我不知道我是否应该问另外一个问题,或者如果我可以堆一点。我仍然在玩这个和他们选择图像后我设置imageData = UIImageJPEGRepresentation(图像,1); 。然后,我使用base64encoding获取该图像的NSString表示,以将其放入要发送给restful服务的json消息中。 .....在这个函数中它都设置得很好,但是当我按下post按钮时,encodedImage为null,就像arc不允许它被保留一样。 – Jarod 2013-04-30 18:26:40

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

如果我改变,我可以将该UIViewController子类的框架设置为CGRectMake(5,50,310,245)所以它不包括在下面。我想做一个弹出效果,并仍然显示数据显示在它下面。 – Jarod 2013-04-30 16:40:47

+0

你应该可以做到这一点,但是你需要阅读如何子类'UIViewController'以及如何/何时组成它的内容的视图被绘制。我很确定你应该提供一个'UIViewController'而不是一个简单的'UIView'。 – Aaron 2013-04-30 16:43:39

+0

我不确定这是否正确,因为您无法将视图控制器显示为子视图,因此这意味着我必须将其呈现并推送给它,我不能将它弹出顶部。 – Jarod 2013-04-30 16:48:02