Here is the class which gives me a modal view of the camera
@interface ViewController : UIViewController <UIImagePickerControllerDelegate> {
UIImagePickerController *cameraView; // camera modal view
BOOL isCameraLoaded;
}
@property (nonatomic, retain) UIImagePickerController *cameraView;
- (IBAction)cameraViewbuttonPressed;
- (void)doSomething;
@end
@implementation ViewController
@synthesize cameraView;
- (void)viewDidLoad {
cameraView = [[UIImagePickerController alloc] init];
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraView.cameraOverlayView = cameraOverlayView;
cameraView.delegate = self;
cameraView.allowsEditing = NO;
cameraView.showsCameraControls = NO;
}
- (IBAction)cameraViewbuttonPressed {
[self presentModalViewController:cameraView animated:YES];
isCameraLoaded = YES;
}
- (void)doSomething {
[cameraView takePicture];
if ([cameraView isCameraLoaded]) printf("camera view is laoded");
else {
printf("camera view is NOT loaded");
}
}
- (void)dealloc {
[cameraView release];
[super dealloc];
}
@end
In the app delegate when it’s running, I call doSomething:
ViewController *actions = [[ViewController alloc] init];
[actions doSomething];
[actions release];
After I press the camera button, the cameraview loads
In the app delegate I call dosomething but nothing happens and I get null for the BOOL which returns “camera view is NOT loaded”.
If I call doSomething in the ViewController class, it works fine, but from another class, it doesn’t work.
How can I access the variables in ViewController class?
Your problem isn’t accessing the variables, it’s that you’re creating a new
ViewControllerfrom scratch withalloc/initand then immediately trying to use it as if it were fully installed in the view hierarchy. Note thatcameraViewis set up insideviewDidLoad, which won’t ever get called for the new view controller.It sounds like you already have an instance of
ViewControllerset up and working, so probably you should use that rather than creating a new one:If that is not the case, you’ll need to add the newly created view to the appropriate superview and let it all initialise properly before trying to use it.