我正在尝试将UIImage视图加载到具有全局类的tabbar应用程序中,图像在第一次加载rootviewcontroler时不加载图像,但它确实显示警报。如果您单击另一个标签栏项目以加载另一个视图并返回到第一个视图,则警报和图像都会正确显示。
#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];
答案 0 :(得分:0)
删除此行:
[viewNetworkError addSubview:viewNetworkError];
对我来说很奇怪,您如何将viewNetworkError
添加到viewNetworkError
。
修改强>
我认为你的意思是
[self addSubview:viewNetworkError];
答案 1 :(得分:0)
您需要在某处添加视图。现在,此类方法在视图层次结构上没有关联。如果你想保持这样的类方法,那么你需要将你的错误视图添加到窗口或根视图控制器视图。
[[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/