2015-11-13 40 views
3

我創建的UIAlertcontroller兩個按鈕:創建行動,當我點擊一個按鈕比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]; 
} 
+3

'處理程序:^(UIAlertAction *動作){//代碼按下按鈕時的操作}];'?或者是你不知道如何打開相機或如何打開畫廊? – Larme

回答

5

處理程序是要在選擇項目時執行的塊。

UIAlertAction *openGallery = [UIAlertAction 
    actionWithTitle:@"open gallery" 
    style:UIAlertActionStyleDefault 
    handler:^(UIAlertAction * action) { 
     // Code to run when the open gallery option is pressed. 
    }]; 

BTW,我認爲在這個問題長期實線真的不幫助他們有效地隱藏關鍵參數。

+0

完美的作品! 謝謝。 – mimpami

+0

沒有人遇到處理程序內部代碼的重大延遲?與UIAlertView相比,它感覺很慢,因爲警報視圖並未等待動畫完全完成。我真的不喜歡這個,但似乎沒有其他辦法。 – nickdnk

+0

@nickdnk你的意思是如果在動畫完成之前按下按鈕?您可以不選擇動畫,也可以通過UIPresentationController自定義動畫,但在這一點上您正在轉向完全不同的問題。 –

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 

                }]; 
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]; 

} 
相關問題