본문 바로가기
iOS, Swift 개발

UIAlertController 사용의 구분-아이폰, 아이패드 Device구분 항목(Object C)

by Nin J 2024. 3. 14.

안녕하세요

Xcode Object-C 로 구성된 소스에서

아이폰과 아이패드에서  UIAlertController 구현하는 중

각각 다르게 주어지지 안으면 Crash가 발생되는 경우

각각의 향(Device)의 구분을 지어 주어야 한다

특히 아이패드 에서

popoverPresentationController 항목을 넣어서 ActionSheet 로 사용 하지 않으면 100% Crush 가 발생 한다

또한 sourceRect (ActionSheet의 위치), sourceView (ActionSheet를 띄울 View) ,permittedArrowDirections(ActionSheet의 화살표 방향 설정 ) 등등의 옵션을 같이 해주어야 한다.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

// 아이폰

  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Select Language" message:nil       preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        for (NSDictionary *dic in theDataManager.languageList)

        {

            NSString *lang = [dic objectForKey:@"title"];

            UIAlertAction *defaultAct = [UIAlertAction actionWithTitle:lang style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                

                NSString *language = [dic objectForKey:@"code"];

                theDataManager.currentLanguage = language;

                [theMenu loadMenu];

                [self logoChangeConstraint:theDataManager.currentLanguage];

                

                if (![[theDelegate.navigationController.viewControllers objectAtIndex:0] isKindOfClass:[MainViewController class]]){

                    theDelegate.navigationController.viewControllers = @[[[MainViewController alloc] init]];

                    [theDelegate.navigationController popToRootViewControllerAnimated:NO];

                }

                [thePlayerView loadPlayerPopupList];

            }];

            [alert addAction:defaultAct];

        }

        UINavigationController *navi = [(AppDelegate *)[[UIApplication sharedApplication] delegate] navigationController];

        [navi presentViewController:alert animated:YES completion:nil];

}else{

// 그외 아이패드

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Select Language" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        

        for (NSDictionary *dic in theDataManager.languageList)

        {

            NSString *lang = [dic objectForKey:@"title"];

            UIAlertAction *defaultAct = [UIAlertAction actionWithTitle:lang style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                

                NSString *language = [dic objectForKey:@"code"];

                theDataManager.currentLanguage = language;

                [theMenu loadMenu];

                NSLog(@"languages %@", language);

                

                //언어변경에 따른 로고이미지 변경 2022.01.07 ktw

                [self logoChangeConstraint:theDataManager.currentLanguage];

                

                if (![[theDelegate.navigationController.viewControllers objectAtIndex:0] isKindOfClass:[MainViewController class]]){

                    theDelegate.navigationController.viewControllers = @[[[MainViewController alloc] init]];

                    [theDelegate.navigationController popToRootViewControllerAnimated:NO];

                }

                

                [thePlayerView loadPlayerPopupList];

            }];

            [alert addAction:defaultAct];

        }

        

        UINavigationController *navi = [(AppDelegate *)[[UIApplication sharedApplication] delegate] navigationController];



        

        // show action sheet

        alert.popoverPresentationController.permittedArrowDirections = 0;

        CGRect rect = navi.view.frame;

        rect.origin.x = 0;

        //rect.origin.y = 0;

        rect.origin.y = navi.view.frame.size.height / 10;

        alert.popoverPresentationController.sourceRect = rect;

        alert.popoverPresentationController.sourceView = navi.view;

        [navi presentViewController:alert animated:YES completion:nil];

}