2014-12-29 14 views
6

मैं इस कोड एक UIViewController में बैठे है (XCode 6.1, आईओएस 8.1.1) का उपयोग रन-टाइम त्रुटि को ठीक:कैसे UIAlertController

[UIAlertController showActionSheetInViewController:self 
         withTitle:@"Test Action Sheet" 
         message:NSLocalizedString(@"Are you sure you want to delete ALL appointments?",nil) 
         cancelButtonTitle:@"Cancel" 
         destructiveButtonTitle:@"Yes" 
         otherButtonTitles:@[@"No"] // same as Cancel 
         tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){ 
           if (buttonIndex == UIAlertControllerBlocksCancelButtonIndex) { 
           NSLog(@"Cancel Tapped"); 
          } else if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) { 
           NSLog(@"Delete Tapped"); 
          } else if (buttonIndex >= UIAlertControllerBlocksFirstOtherButtonIndex) { 
           NSLog(@"Other Action Index %ld", (long)buttonIndex - UIAlertControllerBlocksFirstOtherButtonIndex); 
          } 
         }]; 

जब मैंने इसे चलाने के लिए, मैं इस रन-टाइम त्रुटि मिलती है:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7fdfe3324f00>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

इस काम को करने के लिए मुझे क्या करने की ज़रूरत है? (मैंने एसओ और Google को देखा है और कुछ भी विशिष्ट नहीं पाया है)। मैं इस पर प्राप्त किसी भी मदद की सराहना करता हूं ...

अद्यतन मैंने इसे तृतीय-पक्ष कोड के बिना फिर से लिखा; इस कोड को जोड़ा, और अब यह काम करता है!

UIAlertController * view= [UIAlertController 
          alertControllerWithTitle:@"My Title" 
          message:@"Select your Choice" 
          preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* ok = [UIAlertAction 
        actionWithTitle:@"OK" 
        style:UIAlertActionStyleDefault 
        handler:^(UIAlertAction * action) 
        { 
         //Do some thing here 
         [view dismissViewControllerAnimated:YES completion:nil]; 

        }]; 
UIAlertAction* cancel = [UIAlertAction 
         actionWithTitle:@"Cancel" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 
          [view dismissViewControllerAnimated:YES completion:nil]; 

         }]; 


[view addAction:ok]; 
[view addAction:cancel]; 

view.popoverPresentationController.sourceView = self.view; 
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0, 1.0, 1.0); 
[self presentViewController: view animated:YES completion:nil]; 

उत्तर

1

यहां पर जाने के लिए कीमती कम जानकारी है ...

यह आप https://github.com/ryanmaxwell/UIAlertController-Blocks उपयोग कर रहे हैं प्रकट होता है, नहीं मानक UIAlertController, जिस स्थिति में अपवाद परिवर्तन पता चलता है कि कोड के संस्करण आप ' फिर से उपयोग में शामिल नहीं है या एक उपयोग केस है जिसके लिए आपके हिस्से पर अतिरिक्त काम की आवश्यकता है।

मैंने कभी भी इस तृतीय-पक्ष कोड का उपयोग नहीं किया है, लेकिन त्वरित जांच दस्तावेज़ों में कोई भी स्पष्ट "यह" नहीं दिखाती है। मेरी प्रारंभिक सिफारिश प्रश्न में दृश्य विधि को लागू करने के लिए होगी और उसे जो चाहिए वह दे - वह स्थान जहां पॉपओवर पेश करना है।

+0

मैंने सही कोड के साथ प्रश्न अद्यतन किया। आपकी मदद के लिए धन्यवाद, ब्रैड! मैं इसकी सराहना करता हूं। – SpokaneDude

7

आपको प्राप्त त्रुटि संदेश दिखाई दिया क्योंकि आपने आईपैड पर आईफोन कोड चलाया था। आईपैड पर उपयोग के लिए, आपको अलर्ट कंट्रोलर के पॉपओवर प्रस्तुति नियंत्रक को सेट करना होगा। स्रोत आयताकार भी मैला आयाम गणना के बिना उत्पन्न किया जा सकता है। नीचे, एक पूर्ण विधि है, यह दिखाती है कि बटन दबाकर आप कोड का सामना कैसे करेंगे। अलर्ट कंट्रोलर को जिस तरह से आप चाहते हैं उसे स्थापित करने के बाद, आपको इसके पॉपओवर प्रस्तुति नियंत्रक मिलते हैं और इसे आईपैड के साथ उपयोग के लिए सेट अप करते हैं। नीचे दी गई विधि में, जो बटन दबाया गया था वह प्रेषक है। इसलिए हमने प्रेषक को उस बटन पर वापस डाला, फिर आयताकार सेट करने के लिए बटन का उपयोग करें। कोई गन्दा आयाम की गणना की आवश्यकता नहीं है। अब, यदि आप आईपैड पर कोड चलाते हैं, तो पॉपओवर रद्द बटन प्रदर्शित नहीं करेगा, (जो आईफोन पर दिखाई देता है)। यह डिजाइन द्वारा है। यदि आप ऐप्पल UIPopoverController दस्तावेज़ देखते हैं, तो आप देखते हैं कि पॉपओवर इसके बाहर टैप करके रद्द कर दिया गया है।

- (IBAction)showImagePickerButtonTapped:(id)sender; 
{ 
    BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 
    BOOL isPhotoLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]; 

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; 

    if (isCameraAvailable) { 
     [alertController addAction:[UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
      [self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera]; 
     }]]; 
    } 

    if (isPhotoLibraryAvailable) { 
     [alertController addAction:[UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
      [self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     }]]; 
    } 

    // The following lines are needed for use with the iPad. 
    UIPopoverPresentationController *alertPopoverPresentationController = alertController.popoverPresentationController; 
    UIButton *imagePickerButton = (UIButton*)sender; 
    alertPopoverPresentationController.sourceRect = imagePickerButton.frame; 
    alertPopoverPresentationController.sourceView = self.view; 

    [self showDetailViewController:alertController sender:sender]; 
} 
संबंधित मुद्दे