0

我有一個問題。在我的應用程序,我有UIActionSheet與2個按鈕之一是拍照並從庫中選擇照片。我的問題是如何檢查設備是否有相機可用,如果不是,我怎麼能禁用拍照按鈕從行動sheet.I嘗試了很多東西,但沒有爲我工作。我在下面添加代碼。請讓我知道我的代碼有什麼問題。如果我將拍照按鈕設置爲禁用,那麼我收到一條錯誤消息,說「無法識別的選擇器已發送」。如何禁用UIActionSheet相機按鈕,當設備沒有相機在目標-c

-(void)gestureRecognizer { 
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapOnImage)]; 
[recognizer setNumberOfTapsRequired:2]; 
recognizer.cancelsTouchesInView = NO; 
[self.employeeImage addGestureRecognizer:recognizer]; 
self.employeeImage.userInteractionEnabled = YES; 
} 

-(void)TapOnImage { 
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles:@"Take photo",@"Choose photo", nil]; 
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic; 

// NSString *model = [[UIDevice currentDevice]model]; 
// NSLog(@"current device:%@",model); 

// if([model isEqualToString:@"iPad1,1"]) { 
// NSString *button = (NSString *)[actionSheet buttonTitleAtIndex:0]; 
// 
//   UIButton *btn = (UIButton *)button; 
//   if ([[btn currentTitle] isEqualToString:@"Take photo"]) { 
//    // Do things with button. 
//    button.hidden = YES; 
//   } 

// } 


[actionSheet showFromRect:self.employeeImage.frame inView:self.view animated:YES]; 

} 

- (void)displayImagePicker:(UIImagePickerController *)imagePicker { 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
    // present from popover 
    self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker]; 

    [self.popover presentPopoverFromRect:self.employeeImage.frame//popOverRect 
            inView:self.view 
       permittedArrowDirections:UIPopoverArrowDirectionLeft 
           animated:YES]; 
} else { 
    [self presentModalViewController:imagePicker animated:YES]; 
} 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex { 
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
imagePicker.delegate = self; 
imagePicker.allowsEditing = YES; 

if(buttonIndex == 0) { 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
else { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"This device doesn't support camera" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil]; 
     [alert show]; 

    } 
    return; 
} 
else if(buttonIndex == 1) { 
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    } 
    else { 
     return; 
    } 
} 
else { 
    return; 

} 

[self displayImagePicker:imagePicker]; 
} 

回答

2

使用此或檢查,如果攝像頭可用您的回覆不

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 
+0

感謝。但如果沒有攝像頭,我需要禁用操作表按鈕。是否有可能? – jessy 2012-02-13 14:06:41

+0

我能解決這個問題:) – jessy 2012-03-12 21:17:15