UIView (subclass) trying to present UIImagePickerController
ios, objective-c, uiimage, uiimagepickercontroller, uiview
Solution
Yes, you can not present a viewcontroller from a UIView subclass.
To solve this problem, you can use your subview's superview's viewcontroller class. calling [self.superview nextResponder] in your subview will return you the superview's viewcontroller. Using that you can present your UIImagePicker view controller. To use the presentViewController method, you should cast [self.superview nextResponder] to your parentviewcontroller's class type. Also make sure you import parentview controller.h inside subview.m file
[(YourParentViewController *)[self.superview nextResponder] presentViewController:controller animated:YES completion:nil];
Problem
I have a parent view that allows you to see post in a `UITableView`. In its `Navigation Bar` I have a post button that when pressed presents a `UIView subclass` and shows it on the top of the screen. I have an image on that `UIView` that when tapped I want to present the `UIImagePickerController` to allow users to pick an image to post to the service. How can I do this since my subview is not a view controller it cannot present the UIImagePickerController. Below is my subview code. ``` #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 ```