I have a subclass of UILabel called AlertLabel
AlertLabel.h
#import <UIKit/UIKit.h>
@interface AlertLabel : UILabel
- (id)initWithFrame:(CGRect)frame;
- (void)alertTitle:(NSString *)string;
@end
AlertLabel.m
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.textColor = [UIColor blackColor];
}
return self;
}
- (void)alertTitle:(NSString *)string {
self.text = string;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeLabelFromView) userInfo:nil repeats:NO];
}
- (void)removeLabelFromView {
[self removeFromSuperview];
}
In a viewcontroller called AVController, it can be called like this
- (void)buttonClick:(UIButton *)button {
AlertLabel *alert = [[AlertLabel alloc] initWithFrame:CGRectMake(50, 100, 200, 30)];
[alert alertTitle:@"Success"];
[self.view addSubview:alert];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(callThisActionAfterAlertClosed) userInfo:nil repeats:NO];
}
- (void)callThisActionAfterAlertClosed {
NSLog(@"works");
}
It works, but it doesn't work perfectly, especially the second NSTimer(call the callThisActionAfterAlertClosed
), sometime, the alertlabel doesn't close, but the second nstimer function begins.
It should be popup, automatically close after one second, call another function after closed, all steps like this are performed in an orderly manner.
Is there any other way to do this?