在這裏我有一些代碼,檢查如果iPhone有一個相機閃光燈。然後檢查,如果我的交換機是通過使用此代碼:Objective-C:爲什麼我的if語句不起作用?
if(switch1.on){
}
的指示燈亮起罰款沒有這個代碼,我也嘗試過這種代碼以及:
if(!(switch1.on)){
}
,併成功地令我在相機閃光燈也打開它,甚至當開關被設置爲關閉
這裏是我的全碼:
-(IBAction)torchon:(id)sender{
AVCaptureDevice *flashlight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([flashlight isTorchAvailable] & [flashlight isTorchModeSupported:AVCaptureTorchModeOn]) {
BOOL success = [flashlight lockForConfiguration:Nil];
if(success){
if(switch1.on){ //// or (!(switch1.on))
on.hidden = YES;
[UIScreen mainScreen].brightness = 1.0;
[flashlight setTorchMode:AVCaptureTorchModeOn];
[flashlight unlockForConfiguration];
}
}
}
}
任何幫助,將理解
**EDIT:**
下面是其中i將開關設置爲沒有使用ibactions當開關的值已經改變的代碼。此代碼成功:
-(IBAction)switch1{
if (switch1.on) {
switch1.on = YES;
switch2.on = NO;
} else{
switch2.on = YES;
switch1.on = NO;
}
}
-(IBAction)switch2{
if (switch2.on) {
switch2.on = YES;
switch1.on = NO;
} else{
switch1.on = YES;
switch2.on = NO;
}
}
回答
加上一個布爾值:
bool yes;
然後設置布爾在viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
yes = YES;
}
之後,將其設置爲當ibaction被調用並設置時爲YES爲NO一旦開關被關閉:
-(IBAction)switch1{
if (swich1.on) {
swich1.on = YES;
swich2.on = NO;
yes = YES;
}
}
-(IBAction)switch2{
if (swich2.on) {
swich2.on = YES;
swich1.on = NO;
yes = NO;
}
}
最後,它檢查if(switch1.on){}
你需要將其更改爲if(yes==YES){}
這樣在一起,如果聲明是這樣的:
-(IBAction)torchon:(id)sender{
AVCaptureDevice *flashlight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([flashlight isTorchAvailable] & [flashlight isTorchModeSupported:AVCaptureTorchModeOn]) {
BOOL success = [flashlight lockForConfiguration:Nil];
if(success){
if(yes==YES){
on.hidden = YES;
[UIScreen mainScreen].brightness = 1.0;
[flashlight setTorchMode:AVCaptureTorchModeOn];
[flashlight unlockForConfiguration];
}
}
}
}
不是Xcode的問題。任何機會'switch1'都是'nil'? – 2013-06-30 11:19:17
我們可以看到你設置'on = NO'的代碼嗎? –
@Anoop Vaidya確定檢查我的編輯! – user2534692