2017-05-16 4 views
2

더 많은 페이지에 UIAlertController가 표시됩니다.iOS 경고 컨트롤러 개체 표시

그래서 한 페이지에서 메소드와 클래스에 경고 코드를 작성해야합니다.

클래스를 사용하여 모든 ViewController에서 경고를 표시 할 수있는 메서드를 호출하고 싶습니다.

어떻게 클래스에 presentviewcontroller를 쓸 수 있습니까?

내 헤더 파일은 다음과 같습니다 : 파일을 구현 내

#import <Foundation/Foundation.h> 
#import "VisionAPI.h" 

@interface VisionAPI : NSObject 

+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done; 

@end 

은 다음과 같습니다 :

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

어떻게 :

#import "VisionAPI.h" 
#import <UIKit/UIKit.h> 
@implementation VisionAPI 

+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done{ 

    UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert]; 
UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done style:UIAlertActionStyleDefault 
                 handler:nil]; 
     [showMsgAlertController addAction:showMsgAlertControllerOkAction]; 
dispatch_async(dispatch_get_main_queue(), ^{ 

     [self presentViewController:showMsgAlertController animated:YES completion:nil]; 
    }); 
} 
@end 

그러나 위 코드는 다음 줄에 오류가 표시됩니다 presentViewController NSObject, 또는 문제를 해결하는 방법.

감사합니다.

+1

당신이 alertviewcontroller 제시하는 뷰 컨트롤러가 필요합니다. @Krunal이 아래에 제안한대로 –

답변

0

이 함수에서 매개 변수의 인수로 뷰 컨트롤러를 추가하고 '자기' 컨트롤러를 볼 수있는 (뷰 컨트롤러 인스턴스/객체)를 통과, 당신은 당신의 경고 컨트롤러를 제시하고자합니다. 이 기능마다의 매개 변수 인수로 뷰 컨트롤러를 통과하지 않으려면

+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done fromViewController: (UIViewController)viewController{ 

    UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert]; 
UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done style:UIAlertActionStyleDefault 
                 handler:nil]; 
     [showMsgAlertController addAction:showMsgAlertControllerOkAction]; 
dispatch_async(dispatch_get_main_queue(), ^{ 

     [viewController presentViewController:showMsgAlertController animated:YES completion:nil]; 
    }); 
} 
@end 

는 또한, 응용 프로그램의 루트 뷰 컨트롤러를 사용할 수 있습니다. 다음과 같이 다음과 같이

+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done{ 

     UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert]; 
    UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done style:UIAlertActionStyleDefault 
                  handler:nil]; 
      [showMsgAlertController addAction:showMsgAlertControllerOkAction]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 

RootViewController *rootController = (RootViewController*)[[(AppDelegate*) 
            [[UIApplication sharedApplication]delegate] window] rootViewController]; 

      [rootController presentViewController:showMsgAlertController animated:YES completion:nil]; 
     }); 
    } 
    @end 
0

당신은 NSObject의에서 최상위 뷰 컨트롤러를 얻을 수 있습니다 :

- (UIViewController*)topMostController 
     { 
      UIViewController *topController = [self rootViewController]; 

      while ([topController presentedViewController]) topController = [topController presentedViewController]; 

      // Returning topMost ViewController 
      return topController; 
     } 


+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done{ 

     UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert]; 
    UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done style:UIAlertActionStyleDefault 
                  handler:nil]; 
      [showMsgAlertController addAction:showMsgAlertControllerOkAction]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 

      [[self topMostController] presentViewController:showMsgAlertController animated:YES completion:nil]; 
     }); 
    }