这是给我一个相机模态视图的课程
@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
在app delegate运行时,我调用doSomething:
ViewController *actions = [[ViewController alloc] init];
[actions doSomething];
[actions release];
按下相机按钮后,相机视图会加载 在app委托中我调用dosomething但没有任何反应,我为BOOL返回null,返回“未加载摄像机视图”。
如果我在ViewController
类中调用doSomething,它可以正常工作,但是从另一个类中,它不起作用。
如何访问ViewController
类中的变量?
答案 0 :(得分:3)
您的问题是没有访问变量,而是您从头开始使用ViewController
/ alloc
创建新的init
,然后立即尝试使用它,就好像它是完全安装在视图层次结构中。请注意cameraView
是在viewDidLoad
内设置的,不会为新的视图控制器调用它。
听起来你已经有ViewController
设置和工作的实例,所以可能你应该使用它而不是创建一个新实例:
ViewController* actions = [self getMyExistingViewControllerFromSomewhere];
[actions doSomething];
如果不是这种情况,您需要将新创建的视图添加到相应的超级视图中,并在尝试使用之前将其全部初始化。
答案 1 :(得分:0)
添加到.h:
@property (readwrite, assign, setter=setCameraLoaded) BOOL isCameraLoaded;
添加到.m:
@synthesize isCameraLoaded;
然后你可以这样做:
if ([actions isCameraLoaded]) {
[actions setCameraLoaded:FALSE];
}