views:

297

answers:

2

This seems like a really simple idea... I have a button that says "load a picture". When that button is clicked I want to remove the current view, load a new one, open the image picker, get the selected picture, and display it in the view that I just loaded. I have read several documents that say UIImagePicker does not work in viewDidLoad, and found one thread that suggested using awakeFromNib instead but even that does not work... here is code. Please help.

In MenuViewController

-(IBAction)loadPictureButtonPressed {

UIView *parent = [self.view superview]; if(self.loadPictureViewController.view.superview == nil) { if(self.loadPictureViewController == nil) { LoadPictureViewController *picController = [[LoadPictureViewController alloc] initWithNibName:@"LoadPictureView" bundle: nil]; self.loadPictureViewController = picController; [picController release]; } }

[parent addSubview:self.loadPictureViewController.view]; [self.view removeFromSuperview]; }

In LoadPictureViewController

-(void)awakeFromNib{

if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:picker animated:YES]; [picker release]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Device does not support photo library"; delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; } }

Again I have tried placing this code in both viewDidLoad and awakeFromNib. Please help

A: 

I simply set an NSTimer in viewDidLoad with a timer of 0 to select the method where I load it.

[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(camera) userInfo:nil repeats:NO];
iSkythe
A: 

Instead of NSTimer, you probably want to use the following in -viewDidLoad,

[self performSelector:@selector(camera) withObject:nil afterDelay:0];
Jim Hillhouse

related questions