我试图加载一个UIImage视图到一个选项卡应用程序与一个全局类,图像不加载图像第一次rootviewcontroller被加载,但它确实显示警报。如果单击另一个选项卡项来加载另一个视图并返回到第一个视图,则警告和图像都会正确显示。
#import "globalNetworkCheck.h"
#import "CheckNetworkAvaliblity.h"
#import "ViewController.h"
@implementation globalNetworkCheck
static globalNetworkCheck *getNetworkStatus = nil;
static UIView *viewNetworkError = nil;
+(globalNetworkCheck *)getNetworkStatus
{
@synchronized(self)
{ if(getNetworkStatus==nil) {
// getNetworkStatus= [globalNetworkCheck new];
if ([CheckNetworkAvaliblity CheckNetwork])
{
{
[(UIView *)viewNetworkError removeFromSuperview];
viewNetworkError.backgroundColor=[UIColor clearColor];
viewNetworkError.hidden=YES;
[viewNetworkError removeFromSuperview];
[[viewNetworkError subviews]
makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSLog(@"Success, Your network is available");
}
}
else
{
UIImageView *imgNoConnection = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70, 320 , 449)];
viewNetworkError = [[UIView alloc]init];
[imgNoConnection setImage:[UIImage imageNamed:@"Noconnection.png"]];
viewNetworkError.frame = CGRectMake(0, 0, 320 , 449);
[viewNetworkError addSubview: imgNoConnection];
viewNetworkError.hidden = NO;
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:viewNetworkError];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Connection Failed"
message: @"You are not connected to the internet. Please check your network settings and try again."
delegate: self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
viewNetworkError.hidden = NO;
NSLog(@"Sorry, Network connection is unavailable");
}}}
return getNetworkStatus;
}
@end
我在
上调用类方法 -(void)viewWillAppear:(BOOL)animated {
globalNetworkCheck *obj=[globalNetworkCheck getNetworkStatus];
删除这一行:
[viewNetworkError addSubview:viewNetworkError];
我很奇怪,你打算怎么把viewNetworkError
加到viewNetworkError
。
编辑
我想你是指
[self addSubview:viewNetworkError];
您需要在某处添加视图。现在这个类方法在视图层次上没有关联。如果你想让它像这样保持一个类方法,那么你要么需要添加你的错误视图到窗口或根视图控制器视图。
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:viewNetworkError];
或[[UIApplication sharedApplication].keyWindow addSubview:viewNetworkError];
如果不了解完整的视图堆栈,就很难进一步回答。或者,通过更多的返工,你可以把这个职责放在AppDelegate上,而不是像这样的类方法。创建一个视图,你的App委托可以根据需要添加到层次结构中,在你的App委托上创建一个公共getter来检查当前网络状态,等等。下面是一个如何优化HUD类型视图的例子:http://blog.mugunthkumar.com/coding/ios-code-tweetbot-like-alertpanels/