2012-05-02 59 views
0

使用最新版本的Xcode开发iOS应用程序。应用程序将使用http请求从Web服务中获取数据,并且该应用程序有点大,所以会有很多不同的请求。所以,即时通讯这新的目标-c,所以我想知道你如何创建一个类,它可以包含方法,可以在整个应用程序中访问,只需在其他类中创建该类的新实例,并简单地调用里面的方法你刚开始的classinstance。包含可访问方法的iOS类

我的意思是说,可以说我们有一个叫做Communication的类。 通信类包含一个名称为login的方法,它需要两个参数。 Usernamepassword

然后我想从我的其他类调用该方法使用类似的东西。

Communication com = new Communication(); 
com.login(username, password) 

正如我所说的,即时通讯还挺新的Objective-C,所以我将一些并欣赏帮助。

回答

1

Communication.h(头文件)

#import <Foundation/Foundation.h> 

@interface Communication : NSObject { 
    NSString *username; 
    NSString *password; 
} 

-(void) login:(NSString *)username withPassword:(NSString *)password; 

@property (nonatomic, retain) NSString *username; 
@property (nonatomic, retain) NSString *password; 

@end 

Communication.m(试行)

@implementation Communication 
@synthesize username, password; 

-(void) login:(NSString *)username withPassword:(NSString *)password { 
// Do your login stuff here 
} 

@end 

然后使用这个类是这样的:

NSString *username = @"hugo"; 
NSString *password = @"secret!"; 
Communication *communication = |[Communication alloc] init]; 
[communication login:username withPassword:password];