Rin Rin - 1 month ago 27
iOS Question

UIAlertController reuse more times with NSObject

In my app, UIAlertController uses much time.So, I create

NSObject
Helper.

Here is Helper.h

#import <Foundation/Foundation.h>
#import "PrefixHeader.pch"
#import "LectureViewController.h"

@interface Helper : NSObject
+(void) showNotice:(NSString *) message;
+(void) showNoticeWithAction;
@end


Here is Helper.m

#import "Helper.h"
#import "AppDelegate.h"
@implementation Helper


+(void) showNotice:(NSString *) message{
NSArray *versionArray = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
if ([[versionArray objectAtIndex:0] intValue] >= 9) {

//ver.IOS >= 9
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];

alert.view.backgroundColor = [UIColor grayColor];
// [self presentViewController:alert animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[alert dismissViewControllerAnimated:YES completion:^{

}];

});

} else {
//ver.IOS < 9
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert performSelector:@selector(show) withObject:nil afterDelay:5];
}

}

@end


This line cant run in Helper:
[self presentViewController:alert animated:YES completion:nil];
because of
presentViewController
. Please help me

Answer

You are getting this error because the base class is NSObject and self represent the object of that class and NSObject does'nt have any method named presentViewController.

To resolve it you need to pass an object of UIViewController in that method and on that object use presentViewController like this

+(void) showNotice:(NSString *) message andViewController : (UIViewController *) controller;

And use it like this

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