IOS How to make static alert
////////////////////////////////////////////////
CustomAlert.h
#import <Foundation/Foundation.h>
@interface CustomAlert : UIAlertView {
}
@property (nonatomic) BOOL isShow;
+ (CustomAlert*)sharedAlert;
- (void) createAlert:(NSString*) title message:(NSString*) message
buttonTitle:(NSString*) btnTitle;
- (void) showAlert;
@end
////////////////////////////////////////////////
CustomAlert.m
////////////////////////////////////////////////
#import "CustomAlert.h"
@implementation CustomAlert
@synthesize isShow;
static CustomAlert *shared = NULL;
+ (CustomAlert*) sharedAlert {
if (!shared || shared == NULL){
// allocate the shared instance, because it hasn't been done yet
shared = [[CustomAlert alloc] init];
[shared addButtonWithTitle:@"OK"];
}
return shared;
}
- (void) createAlert:(NSString*) title message:(NSString*) message
buttonTitle:(NSString*) btnTitle {
if (!isShow) {
self.message = message ;
self.title = title;
for (UIView *buttonView in [self subviews]) {
if ([buttonView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton*)buttonView;
[button setTitle:btnTitle forState:UIControlStateNormal];
break;
}
}
}
}
- (void) showAlert {
[self show];
isShow = YES;
}
@end
Using static alert
CustomAlert *share1Alert = [CustomAlert sharedAlert];
[share1Alert createAlert:@"Warning" message:@"Do you want to delete ?" buttonTitle:@"ok"] ;
share1Alert.delegate = self;
[share1Alert showAlert];
Hander click button on alert
- (void)alertView:(CustomAlert *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
alertView.isShow = NO;
}
CustomAlert.h
#import <Foundation/Foundation.h>
@interface CustomAlert : UIAlertView {
}
@property (nonatomic) BOOL isShow;
+ (CustomAlert*)sharedAlert;
- (void) createAlert:(NSString*) title message:(NSString*) message
buttonTitle:(NSString*) btnTitle;
- (void) showAlert;
@end
////////////////////////////////////////////////
CustomAlert.m
////////////////////////////////////////////////
#import "CustomAlert.h"
@implementation CustomAlert
@synthesize isShow;
static CustomAlert *shared = NULL;
+ (CustomAlert*) sharedAlert {
if (!shared || shared == NULL){
// allocate the shared instance, because it hasn't been done yet
shared = [[CustomAlert alloc] init];
[shared addButtonWithTitle:@"OK"];
}
return shared;
}
- (void) createAlert:(NSString*) title message:(NSString*) message
buttonTitle:(NSString*) btnTitle {
if (!isShow) {
self.message = message ;
self.title = title;
for (UIView *buttonView in [self subviews]) {
if ([buttonView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton*)buttonView;
[button setTitle:btnTitle forState:UIControlStateNormal];
break;
}
}
}
}
- (void) showAlert {
[self show];
isShow = YES;
}
@end
Using static alert
CustomAlert *share1Alert = [CustomAlert sharedAlert];
[share1Alert createAlert:@"Warning" message:@"Do you want to delete ?" buttonTitle:@"ok"] ;
share1Alert.delegate = self;
[share1Alert showAlert];
Hander click button on alert
- (void)alertView:(CustomAlert *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
alertView.isShow = NO;
}
Comments
Post a Comment