What to expect when running simulator?


#1

Hello,

I’m running all exercises via the simulator, because my iPhone has iOS 5 beta loaded, and will not accept test app from the non-beta XCode. When I click the camera button on the ItemViewDetail screen in the simulator, nothing happens. Nothing shows up such as a library to pick from, no error, or such. It’s as if the camera button has no code to execute.

Is this normal since the simulator has no camera and no library of images to choose from, or should there be some sort of “simulated” library to choose from? I’ve included my ItemDetailViewController.h & .m files. I’ve tried running Project/Analyze, and it came up clean (with the one exception from PossessionStore.m regarding the 0 retain count, which was already known about). I just wanted to see if all is happing in the Simulator as it should. Any advice is welcome! :slight_smile:

//
//  ItemDetailViewController.h
//  Homepwner
//
//  Created by Rodney Johnson on 9/14/11.
//  Copyright 2011 Tech Infected. All rights reserved.
//

#import <UIKit/UIKit.h>

@class Possession;

@interface ItemDetailViewController : UIViewController 
<UINavigationControllerDelegate, UIImagePickerControllerDelegate> 
{
    UITextField *nameField;
    UITextField *serialNumberField;
    UITextField *valueField;
    UILabel *dateLabel;    
    UIImageView *imageView;
    
    Possession *possession;
}

@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *serialNumberField;
@property (nonatomic, retain) IBOutlet UITextField *valueField;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) Possession *possession;

- (IBAction)takePicture:(id)sender;

@end

ItemDetailViewController.m Code

//
//  ItemDetailViewController.m
//  Homepwner
//
//  Created by Rodney Johnson on 9/14/11.
//  Copyright 2011 Tech Infected. All rights reserved.
//

#import "ItemDetailViewController.h"
#import "Possession.h"

@implementation ItemDetailViewController

@synthesize nameField;
@synthesize serialNumberField;
@synthesize valueField;
@synthesize dateLabel;
@synthesize possession;
@synthesize imageView;

- (IBAction)takePicture:(id)sender {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        
    } else {
        
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    
    [imagePicker setDelegate: self]; // will generate 2 warnings: ignore for now
    
    // Place imagePicker on screen
    [imagePicker presentModalViewController:imagePicker animated:YES];
    
    // ItemDetailViewController now retains it, so let it go.
    [imagePicker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Get the picked image from the dictionary
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    // Place the image on screen for viewing
    [imageView setImage:image];
    
    // Remove image picker off the screen *a must*
    [self dismissModalViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }

    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib
    [[self view]setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
}

- (void)viewDidUnload
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    [self setNameField:nil];
    [self setNameField:nil];
    [self setSerialNumberField:nil];
    [self setValueField:nil];
    [self setDateLabel:nil];
    [self setImageView:nil];

    [super viewDidUnload];    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    
    [nameField setText:[possession possessionName]];
    [serialNumberField setText:[possession serialNumber]];
    [valueField setText:[NSString stringWithFormat:@"%d",[possession valueInDollars]]];
    
    // Create date formatter that creates date in simple format
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
 
    [dateLabel setText:[dateFormatter stringFromDate:[possession dateCreated]]];
    
    // Change Nav bar to Possession name
    [[self navigationItem] setTitle:[possession possessionName]];
 }

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    // Clear the First Responder
    [[self view] endEditing:YES];
    
    // Save "changes" to Possession
    [possession setPossessionName:[nameField text]];
    [possession setSerialNumber:[serialNumberField text]];
    [possession setValueInDollars:[[valueField text] intValue]];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    
    [nameField release];
    [nameField release];
    [serialNumberField release];
    [valueField release];
    [dateLabel release];
    [possession release];
    [imageView release];

    [super dealloc];
}

@end

#2

Hi,

I think it’s this line in takePicture

[imagePicker presentModalViewController:imagePicker animated:YES];

which should be

[self presentModalViewController:imagePicker animated:YES];

HTH
Gareth


#3

[quote=“GarethR”]I think it’s this line in takePicture

[imagePicker presentModalViewController:imagePicker animated:YES];

which should be

[self presentModalViewController:imagePicker animated:YES];[/quote]

That was it, Gareth. I now get Photo Albums, with no images to select from (which makes sense). I’ll try to look harder for such mistakes before posting next time!