Created
December 30, 2013 13:27
-
-
Save crespoxiao/8182089 to your computer and use it in GitHub Desktop.
this is CrespoXiao's gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TCNetworkAlertView.h | |
// KanKanIphone | |
// | |
// Created by CrespoXiao on 13-11-26. | |
// | |
// | |
#import <UIKit/UIKit.h> | |
@interface TCNetworkAlertView : UIView { | |
UIButton* _netErrorButton; | |
UILabel * _textLabel; | |
} | |
@property (nonatomic,assign)BOOL _is_try_again; | |
@property (nonatomic,assign)BOOL is_showing; | |
@property (nonatomic, copy) void(^tryAgainBlock)(); | |
+ (TCNetworkAlertView *)sharedInstance; | |
- (void)showAlertWithText:(NSString*)str; | |
- (void)hide; | |
@end | |
// | |
// TCNetworkAlertView.m | |
// KanKanIphone | |
// | |
// Created by CrespoXiao on 13-11-26. | |
// | |
// | |
#import "TCNetworkAlertView.h" | |
#import "TCAppDelegate.h" | |
static TCNetworkAlertView* netAlert = nil; | |
//static int try_count = 0; | |
@implementation TCNetworkAlertView | |
@synthesize _is_try_again; | |
@synthesize is_showing; | |
+ (TCNetworkAlertView*) sharedInstance{ | |
if (!netAlert) { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
netAlert = [[TCNetworkAlertView alloc]initWithFrame:CGRectMake(0, 20+44, SCREEN_WIDTH, SCREEN_HEIGHT-20-44-49)]; | |
}); | |
} | |
return netAlert; | |
} | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
_netErrorButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
_netErrorButton.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-20-44); | |
_netErrorButton.backgroundColor = [UIColor clearColor]; | |
[_netErrorButton setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted]; | |
[_netErrorButton addTarget:self action:@selector(tryAgain) forControlEvents:UIControlEventTouchUpInside]; | |
[self addSubview:_netErrorButton]; | |
UIImageView * image = [[UIImageView alloc]initWithFrame: | |
CGRectMake(70, (SCREEN_HEIGHT-20-44-49)*(2.0/5.0), 24, 24)]; | |
image.image = [UIImage imageNamed:@"Warning.png"]; | |
[self addSubview:image]; | |
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, (SCREEN_HEIGHT-20-44-49)*(2.0/5.0), 200, 28)]; | |
_textLabel.backgroundColor = [UIColor clearColor]; | |
_textLabel.font = [UIFont boldSystemFontOfSize:14.0]; | |
_textLabel.textColor = TitleColor; | |
[self addSubview:_textLabel]; | |
} | |
return self; | |
} | |
- (void)showAlertWithText:(NSString*)str { | |
if ([TCNetworkAlertView sharedInstance]) { | |
[[TCNetworkAlertView sharedInstance] removeFromSuperview]; | |
} | |
[[TCAppDelegate sharedInstance].window.rootViewController.view addSubview:[TCNetworkAlertView sharedInstance]]; | |
[[TCNetworkAlertView sharedInstance] show:str]; | |
[TCNetworkAlertView sharedInstance].is_showing = YES; | |
} | |
- (void)show:(NSString*)str { | |
_textLabel.text = str; | |
self.hidden = NO; | |
} | |
//user block instead of kvo | |
//- (void)tryAgain { | |
// //_is_try_again = YES; | |
// self._is_try_again = YES; | |
// | |
//// ++try_count; | |
//// if (try_count < 2) { | |
//// _is_try_again = YES; | |
//// }else { | |
//// try_count = 0; | |
//// //_is_try_again = YES; //only try 1 time ? | |
//// } | |
// [self hide]; | |
//} | |
- (void)tryAgain { | |
if (_tryAgainBlock) { | |
_tryAgainBlock(); | |
_tryAgainBlock = nil; | |
} | |
[self hide]; | |
} | |
- (void)hide { | |
self.is_showing = NO; | |
[self removeFromSuperview]; | |
} | |
@end | |
//how to use ? see below | |
[[TCNetworkAlertView sharedInstance]showAlertWithText:NSLocalizedString(@"home.network.error", nil)]; | |
//__block SubLoginView* weakSelf = self; //for no arc | |
//__weak typeof(self) weakSelf = self; //for arc | |
__block __typeof(self) weakSelf = self; //for all | |
[[TCNetworkAlertView sharedInstance] setTryAgainBlock:^{ | |
[weakSelf onConfirmButtonClicked]; | |
}]; | |
// [[TCNetworkAlertView sharedInstance] addObserver:self | |
// forKeyPath:@"_is_try_again" | |
// options:NSKeyValueObservingOptionNew | |
// context:NULL]; | |
//- (void)observeValueForKeyPath:(NSString *)keyPath | |
// ofObject:(id)object | |
// change:(NSDictionary *)change | |
// context:(void *)context | |
//{ | |
// DLog(@"%s", __FUNCTION__); | |
// | |
// if ([keyPath isEqualToString:@"_is_try_again"]) | |
// { | |
// if (self.view.window) { | |
// [self onConfirmButtonClicked]; | |
// } | |
// | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment