所以我制作了一个自定义 UIButton 并将其添加到代码中并在 interfacebuiler 中建立了连接。我希望按钮用作开关,我该如何正确执行此操作?我是 iphone 开发的初学者,这是我在暑假期间参加的这门课的学校项目,以便为下学期抢占先机。
因此,如果有人可以帮助我理解如何以正确的方式做到这一点,也许可以在代码中写下注释。感谢所有的帮助。大卫·H。
这是我的代码:
//
// FlashlightViewController.h
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface FlashlightViewController : UIViewController {
AVCaptureSession *torchSession;
IBOutlet UIButton *button;
}
-(IBAction)pressButton:(id) sender;
@property (nonatomic, retain) AVCaptureSession *torchSession;
@property (nonatomic, retain) IBOutlet UIButton *button;
@end
这是.m文件
//
// FlashlightViewController.m
//
#import "FlashlightViewController.h"
@implementation FlashlightViewController
@synthesize torchSession;
@synthesize button;
- (void)viewDidLoad {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
if (flashInput){
[session addInput:flashInput];
}
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
[output release];
[session commitConfiguration];
[session startRunning];
}
[self setTorchSession:session];
[session release];
[super viewDidLoad];
}
- (void)viewDidUnload {
self.button = nil;
}
- (void)dealloc {
[TorchSession release];
[button release];
[super dealloc];
}
-(IBAction)pressButton : (id) sender{
}
@end