我创建两个UIAlertcontroller
按钮:
One Button - "OpenCamera"
Two button - "OpenGallery"
当我点击其中一个时,我无法理解我是如何创建动作的。
- (IBAction)takePic:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet]; // 1
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"open camrea"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"open gallery"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
[alert addAction:openCamrea];
[alert addAction:openGallery];
[self presentViewController:alert animated:YES completion:nil];
}
答案 0 :(得分:5)
处理程序是在选择项目时执行的块。
UIAlertAction *openGallery = [UIAlertAction
actionWithTitle:@"open gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// Code to run when the open gallery option is pressed.
}];
顺便说一句,我认为问题中长期不间断的行确实没有帮助,因为它们有效地隐藏了关键参数。
答案 1 :(得分:1)
完整代码:
- (IBAction)takePic:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"open camrea"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"message:@"Device has no camera"delegate:nil cancelButtonTitle:@"OK"otherButtonTitles: nil];
[myAlertView show];
}
else
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"open gallery"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}];
[alert addAction:openCamrea];
[alert addAction:openGallery];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.img.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
答案 2 :(得分:0)
将代码放入要传递到handler
[UIAlertAction actionWithTitle:style:handler:]
块中
例如:
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"open camrea"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
// openCamera action code goes here
}];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"open gallery"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
// openGallery action code goes here
}];