исключение uncaught 'NSGenericException: приложение представило UIAlertController стиля UIAlertControllerStyleActionSheet - PullRequest
0 голосов
/ 21 сентября 2018

Я работаю над приложением, которое запускаю на iPhone, работает хорошо, но когда я пытаюсь запустить на iPad, оно вылетает

Вот мой код:

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

Я делюсьего журнал сбоев

*** Завершение работы приложения из-за необработанного исключения «NSGenericException», причина: «Ваше приложение представило UIAlertController () стиля UIAlertControllerStyleActionSheet.ModalPresentationStyle UIAlertController с этим стилем - UIModalPresentationPopover.Вы должны предоставить информацию о местонахождении для этого всплывающего окна через контроллер popoverPresentationController контроллера оповещений.Вы должны предоставить либо sourceView и sourceRect, либо barButtonItem.Если эта информация неизвестна при представлении контроллера предупреждений, вы можете предоставить ее в методе UIPopoverPresentationControllerDelegate -prepareForPopoverPresentation.

Ответы [ 2 ]

0 голосов
/ 21 сентября 2018

Добавить исходный вид и исходный прямоугольник в свой 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];
0 голосов
/ 21 сентября 2018

в настоящее время на ipad alertcontrollers не разрешены, вместо этого вы можете использовать всплывающие окна для отображения вида оповещения

Программно

UIViewController *newViewCont = [[UIViewController  alloc] init];
newViewCont.view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 180, 180)];
newViewCont.modalPresentationStyle = UIModalPresentationPopover;

[self presentViewController:newViewCont animated:YES completion:nil];

UIPopoverPresentationController *pop = [newViewCont popoverPresentationController];
pop.permittedArrowDirections = UIPopoverArrowDirectionAny;

[pop setSourceView:myButton];
[pop setSourceRect:myButton.bounds];

Использование раскадровок

// grab the view controller we want to show
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Pop"];

// present the controller
// on iPad, this will be a Popover
// on iPhone, this will be an action sheet
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];

// configure the Popover presentation controller
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popController.delegate = self;

// in case we don't have a bar button as reference
popController.sourceView = self.view;
popController.sourceRect = CGRectMake(30, 50, 10, 10);

dismiss popover

[self dismissViewControllerAnimated:YES completion:nil];

Существует новый протокол под названием UIPopoverPresentationControllerDelegate, который вызывается при увольнении и изменении положения из-за поворота или изменения интерфейса.Мы можем даже предотвратить смещение Popover, если захотим.Вот три метода, которые мы можем реализовать:

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {

// called when a Popover is dismissed
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {

// return YES if the Popover should be dismissed
// return NO if the Popover should not be dismissed
return YES;
}

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing  _Nonnull *)view {

// called when the Popover changes position
}

Не забудьте соответствовать протоколу и установить делегата для вашего реагирующего класса.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...