无法在iPad上点击UIAlertAction

时间:2017-04-07 08:02:02

标签: objective-c uialertcontroller uialertaction

我创建了一个带有3个UIAlertAction的UIAlertController。它在iPhone上运行良好,但我无法点击iPad中的任何UIAlertAction。以前它工作正常,我没有对代码进行任何更改

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@“Select Item” message:@“Please choose 1.”                                                                  preferredStyle:UIAlertControllerStyleActionSheet];


[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    _logoView.alpha = 1;
}]];


UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [UIViewController new];
window.windowLevel = UIWindowLevelAlert + 1;

NSString *buttonTitle = [sessionArray objectAtIndex:0];

UIAlertAction *session1 = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
{
    window.hidden = YES;
    [self sessionPickerSelected:0];
}];
[alert addAction:session1];

buttonTitle = [sessionArray objectAtIndex:1];
UIAlertAction *session2 = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
{
    window.hidden = YES;
    [self sessionPickerSelected:1];
}];
[alert addAction:session2];

buttonTitle = [sessionArray objectAtIndex:2];
UIAlertAction *session3 = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
{
    window.hidden = YES;
    [self sessionPickerSelected:2];         
}];
[alert addAction:session3];

[alert setModalPresentationStyle:UIModalPresentationPopover];

UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = _btnLogin;
popPresenter.sourceRect = _btnLogin.bounds;

[window makeKeyAndVisible];
[window.rootViewController presentViewController:alert animated:YES completion:nil];

1 个答案:

答案 0 :(得分:0)

要在iPad上使用以下方法

- (void) actionSelect:(UIButton *)sender {


    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Confirm Add Photo"
                                                                   message:@"Are you sure to select photo?"
                                                            preferredStyle:UIAlertControllerStyleActionSheet]; // 1



    UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"Take Photo"
                                                          style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                              NSLog(@"You pressed invitation num ");
                                                              [self dismissViewControllerAnimated:YES completion:nil];
                                                              [self takePhoto:sender];

                                                          }]; // 2
    [alert addAction:firstAction]; // 4


    UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"Select Photo"
                                                           style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                               NSLog(@"You pressed invitation num ");
                                                               [self dismissViewControllerAnimated:YES completion:nil];
                                                               [self selectPhoto:sender];


                                                           }]; // 2
    [alert addAction:secondAction]; // 4


    UIAlertAction *thirdAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                          style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
                                                              NSLog(@"You pressed cancel button ");
                                                          }]; // 3


    [alert addAction:thirdAction]; // 5


    if ([Utility isUserInterfaceiPad]) {
        // Remove arrow from action sheet.
        [alert.popoverPresentationController setPermittedArrowDirections:0];

        //For set action sheet to middle of view.
        alert.popoverPresentationController.sourceView = self.view;
        alert.popoverPresentationController.sourceRect = self.view.bounds;

    }


    [self presentViewController:alert animated:YES completion:nil]; // 6


}

检测iPad

+ (BOOL)isUserInterfaceiPad{

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        //do ur  ipad logic
        return  YES;

    }else
    {
        //do ur  iphone logic
        return NO;

    }

}