2012-05-02 21 views
0

最新バージョンのXcodeを使用してiOSアプリケーションを開発しています。アプリケーションは、HTTPリクエストを使用してWebサービスからデータを取得しようとしています。アプリケーションはちょっと大きいので、さまざまなリクエストがあります。だから、私はこの目的で新しいことを教えています。私は他のクラスにそのクラスの新しいインスタンスを作成するだけで、アプリケーション全体にアクセスできるメソッドを含むことができるクラスをどのように作成し、単に内部のメソッドあなたがちょうど始めたクラスインスタンス。iOSアクセス可能なメソッドを含むクラス

私はコミュニケーションというクラスがあると言っています。 通信クラスには、名前がloginである1つのメソッドが含まれており、2つのパラメータが必要です。 Usernameおよびpassword

次に、他のクラスのメソッドから、そのようなメソッドを呼び出す必要があります。

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

私が言ったように、私はちょっと客観的に新しいと思ったので、私はいくつかの助けを訴えるでしょう。

答えて

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]; 
関連する問題