2013-09-10 39 views
0

我正在重构我的代码以使其更易于管理我想创建一个包含可以加载到其他类中的函数的类。导入一类函数头

我创建了一个名为functions的类,将funtions.h导入到我的ViewController类的.h中,将函数.m导入到ViewController.m中,但编译器在调用和崩溃时无法识别hasInternetconnection方法。

我没有完全丧失,为什么我不能在这个类

这里是我的代码中调用这个方法,我曾通过S/O良好的外观和谷歌,我还看不到有什么我做错了

Functions.h

#import <Foundation/Foundation.h> 

@interface Functions : NSObject 


-(BOOL)hasInternetConection; 
@end 

Functions.m

#import "Functions.h" 

@implementation Functions 



-(BOOL)hasInternetConection{ 
    NSURL *url=[NSURL URLWithString:@"www.google.com"]; 
    NSURLRequest *req=[NSMutableURLRequest requestWithURL:url]; 
    NSHTTPURLResponse *res=nil; 
    [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:NULL]; 
    if (res!=nil) { 
     return NO; 
    }else{ 
     return YES;} 

} 

@end 

HomeViewController.h

... 
#import <QuartzCore/QuartzCore.h> 
#import "multiShotViewController.h" 
#import "Functions.h" 
... 

@interface HomeViewController:{的UIViewController

UIGlossyButton *b; 

HomeViewController.m

... 
#import "detailsViewController.h" 
#import "Functions.h" 
#define Kwelome @"welcomeread" 


@interface HomeViewController() 

@end 

@class Functions; 
@implementation HomeViewController 
@synthesize tripName; 
@synthesize databasePath, deathtrail; 
@synthesize lampingbtn,deerstalkingbtn,boundarybtn, optionsbtn,shootingbtn; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     self.navigationItem.title = @"Home"; 
     UIColor *backg=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bgcamo.png"]]; 
     self.view.backgroundColor=backg; 
     [backg release]; 
    } 
    return self; 
} 
... 
+0

你要在你的HomeViewcontroller中调用' - (BOOL)hasInternetConection'吗? – Alex

回答

1

我觉得@class功能;至少不是必需的。您已经导入头文件,因此您没有重新声明它。

你在哪里调整这些方法?你确定你打电话给他们的那个类的实例吗?

我怀疑你正在试图做

[Functions hasInternetConection] 

,而不是

Functions * func = [[Functions alloc] init]; 
[func hasInternetConection]; 
[func release]; 

如果你不喜欢它在第一个例子比变化宣言“+”,而不是“一个问题 - “在你的函数中 - 所以它可以用作静态方法。

+0

非常感谢Grzegorz我有很多东西混合在一起,现在所有的工作都欢呼哥们 –