2016-08-04 2 views
2

다른 클래스의 UIAlertController를 어떻게 표시합니까? 난 당신이 클래스 B에서 만든하지만 클래스 A에 발표 된 UIAlertController에 "OK"버튼의 동작을 캡처 할 수있는 방법을 알고 싶어요 Objective-C의 다른 클래스에서 현재 "컨트롤러"

이 내가 경고를 생성 한 메소드를 호출하는 방법입니다

ErrorHandler *handler = [[ErrorHandler alloc] init]; 
[self presentViewController:[handler alertWithInternetErrorCode] animated:YES completion:nil]; 

그리고 이것은 ErrorHandler.m에서 alertWithInternetErrorCode의 구현 : :를 ClassA에서 클래스 "ErrorHandler를"다시

- (UIAlertController *)alertWithInternetErrorCode{ 

    UIAlertController * alert = [UIAlertController 
           alertControllerWithTitle:@"Error" 
           message:@"No internet conneciton" 
           preferredStyle:UIAlertControllerStyleAlert]; 
    UIAlertAction * cancel = [UIAlertAction 
           actionWithTitle:@"Cancel" 
           style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction * action) { 
            NSLog(@"cancelled"); 
           }]; 

    [alert addAction:cancel]; 
    return alert; 
} 

, 내가 원하는 다른 클래스에서 이러한 종류의 객체를 생성 할 수있는 방법을 알고, 호출 한 클래스에서 계속 객체를 표현할 수 있어야합니다. 그것은 그들의 행동을 포착하는 것을 포함합니다. 이 경우 "취소 버튼"안에 NSLog 조치가 있습니다. NSLog 대신 메서드를 호출 할 수 있습니까? 델리게이트 메서드를 호출하고 클래스 A의 코드로 돌아가 보자.

+0

패스가보기 컨트롤러 같은 : - (무효) presentAlertWithInternetErrorCodeOnController : (UIViewController에 *) 컨트롤러 –

답변

0

2 선택 :

최선의 선택 :

그래서 같은 방법으로 컨트롤러에 전달이 가능하지 않은 경우 - (UIAlertController *)alertWithInternetErrorCodeInPresenter: (UIViewController *) presenter

전화 [presenter presentViewController: alert animated:YES completion:nil];

:

UIViewController *rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; 
[rootVC presentViewController:alert animated:YES completion:nil]; 

편집 - 작업을 캡처 :

- (void) presentAlertWithInternetErrorCodeInPresenter:(UIViewController<CustomAlertViewProtocol> *) presenter{ 

    UIAlertController * alert = [UIAlertController 
          alertControllerWithTitle:@"Error" 
          message:@"No internet connection" 
          preferredStyle:UIAlertControllerStyleAlert]; 
    UIAlertAction * cancel = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleCancel 
          handler:^(UIAlertAction * action) { 
           [presenter cancelPressed];//Here's the key 
          }]; 

    [alert addAction:cancel]; 
    [presenter presentViewController: alert animated:YES completion:nil]; 
} 

ErrorHandler.h 파일에서이 프로토콜을 선언해야합니다 : 사용할 .H 파일 어떤 뷰 컨트롤러 지금

@protocol CustomAlertViewProtocol 
- (void) cancelPressed; 
@end 

을 이 메서드는 CustomAlertViewProtocol을 따르고 있음을 컴파일러에 알려야합니다.

@interface MyViewController : UIViewController <CustomAlertViewProtocol> 

그리고하는 .m 당신은 프로토콜 방법을 구현해야합니다

이제
- (void) cancelPressed { 
    //Do whatever you want 
} 

실제로 경고를 표시하기를 :

ErrorHandler *handler = [[ErrorHandler alloc] init];//Or whatever initializer you use 
[handler presentAlertWithInternetErrorCodeInPresenter: self]; 
+0

클래스 B에서 AlertView를 클래스 A에서 호출했습니다. 클래스 A (클래스 B는 AlertViewController를 반환했습니다)에서 경고를 표시했습니다. 이제 Class A에서 "ok AlertButton"액션을 잡을 수 있습니까? B에서 대리인을 선언해야합니까? 또는 A? 이건 알아 내고 싶어. –

+0

@MauricioPimientaSosa 편집보기 –