UIView (subclass) intentando presentar UIImagePickerController

Tengo una vista principal que te permite ver la publicación en una UITableView . En su Navigation Bar , tengo un button de publicación que cuando se presiona presenta una UIView subclass y la muestra en la parte superior de la pantalla. Tengo una image en esa UIView que, cuando se UIView , quiero presentar el UIImagePickerController para permitir a los usuarios elegir una image para publicar en el service. ¿Cómo puedo hacer esto ya que mi subvista no es un controller de vista? No puede presentar el UIImagePickerController.

A continuación se muestra mi código de subvista.

  #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.numberOfTapsRequinetworking = 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 

Sí, no puede presentar un viewcontroller desde una subclass UIView.

Para resolver este problema, puede usar la class viewcontroller de su supervisora ​​de subview.
llamar a [self.superview nextResponder] en su subvista le devolverá el controller de vista de la supervisión.
Utilizando eso, puede presentar su controller de vista UIImagePicker.

Para usar el método presentViewController, debe convertir [self.superview nextResponder] en el tipo de class de su controller de vista padre.
También asegúrese de importar parentview controller.h dentro del file subview.m

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

Debería presentar una subclass UIViewController lugar de una subclass UIView .

También diría que UIViewController debería ser responsable de manejar los datos y la lógica operacional de sus vistas. Echa un vistazo a algunos de los documentos:

Vea los conceptos básicos del controller: http://developer.apple.com/library/ios/#featunetworkingarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html

Referencia de la class UIViewController : http://developer.apple.com/library/ios/#featunetworkingarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html