iosobjective-cexceptionuialertcontrolleruimodalpresentationstyle

uncaught exception 'NSGenericException: application has presented a UIAlertController of style UIAlertControllerStyleActionSheet


I am working on an app which i run on iPhone works well but when i am trying to run on iPad it crashes

Here is my code:

- (void)parseCountryStates:(NSDictionary *)json
{    
    countryPickerView.hidden = TRUE;
    NSDictionary *listing = [json objectForKey:@"country"];
    countryArray = [listing allValues];
    countryIDArray = [listing allKeys];

    [countryPickerView reloadAllComponents];
    alertController = [UIAlertController
                       alertControllerWithTitle:@"Select Service Type"
                       message:nil
                       preferredStyle:UIAlertControllerStyleActionSheet];
    int count = (int)[countryPickerView numberOfRowsInComponent:0];

    for (int i = 0; i < count; i++)
    {
        UIAlertAction* button = [UIAlertAction
                                 actionWithTitle:[[countryPickerView delegate] pickerView:countryPickerView titleForRow:i forComponent:0]
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     countryField.text = [action title];
                                     countryStr = countryField.text;
                                     if ([countryArray containsObject:countryStr]) {
                                         countryidStr = [countryIDArray objectAtIndex:[countryArray indexOfObject:countryStr]];
                                         NSLog(@"CountryidStr %@",countryidStr);
                                         [self getState];
                                     }
                                 }];
        [alertController addAction:button];

    }

    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleCancel
                             handler:^(UIAlertAction * action)
                             {
                                 //  UIAlertController will automatically dismiss the view
                             }];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:true completion:nil];
}

I am sharing the crash log of it

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () 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.


Solution

  • Add source view and source rect to your alertController.

    [[alertController popoverPresentationController] setSourceView:self.view];
    [[alertController popoverPresentationController] setSourceRect:CGRectMake(0,0,1,1)];
    [[alertController popoverPresentationController] setPermittedArrowDirections:UIPopoverArrowDirectionUp];
    
    [self presentViewController:alertController animated:true completion:nil];