7

カスタム Web API を使用して Web サーバーに接続するこのチュートリアル ( http://bit.ly/NI9kQe )に従ってアプリを構築しています。要件の 1 つは、[ログイン] または [登録] ボタンがタップされたかどうかを検出することです。これは、インターフェイス ビルダーでボタンに設定された「タグ」を使用して行われます (登録ボタンのタグは 1 です)。

コードのチャンクは、次のように btnLoginRegisterTapped メソッド内にあります (エラーは行で発生します -> NSString* command = (sender.tag==1)?@"register":@"login";):

- (IBAction)btnLoginRegisterTapped:(id)sender {

//form fields validation
if (fldUserName.text.length < 4 || fldPassword.text.length < 4) {
  //  [UIAlertView error:@"Enter username and password over 4 chars each."];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!" message:@"Enter  username and password over 4 chars each." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    // optional - add more buttons:
    [alert addButtonWithTitle:@"Yes"];
    [alert show];
    return;

}

//salt the password
NSString* saltedPassword = [NSString stringWithFormat:@"%@%@", fldPassword.text, kSalt];

//prepare the hashed storage
NSString* hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];

//hash the pass
NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
    hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!" message:@"Password cannot be reset!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    // optional - add more buttons:
    [alert addButtonWithTitle:@"Yes"];
    [alert show];
    return;
}

//************ THIS IS WHERE THE ERROR OCCURS *****************//
//check whether it's a login or register 
NSString* command = (sender.tag==1)?@"register":@"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                              command, @"command",
                              fldUserName.text, @"username",
                              hashedPassword, @"password",
                              nil];

//make the call to the web API
[[API sharedInstance] commandWithParams:params
                           onCompletion:^(NSDictionary *json) {
                               //handle the response
                               //result returned
                               NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];

                               if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
                                   //success
                                   [[API sharedInstance] setUser: res];
                                   [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

                                   //show message to the user
                                   [[[UIAlertView alloc] initWithTitle:@"Logged in"
                                                               message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"] ]
                                                              delegate:nil
                                                     cancelButtonTitle:@"Close"
                                                     otherButtonTitles: nil] show];

                               } else {
                                   //error

                                   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!" message:@"Server down? Try Again" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
                                   // optional - add more buttons:
                                   [alert addButtonWithTitle:@"Yes"];
                                   [alert show];
                                   return;

                               }

                           }];

}

プロジェクト(実際にはワークスペース)をビルドしようとすると、エラーが発生します:

タイプ「_strong id」のオブジェクトにプロパティ「タグ」が見つかりません

iOS7用に展開するxcode 5.0を使用しています。

ありがとう、

4

1 に答える 1

18

idプロパティ構文は、ジェネリック型の変数では使用できません。

sender.tagしたがって、メソッド呼び出し[sender tag]またはより良い方法で置き換えるかsender、メソッド定義で引数の実際の型を使用します。

- (IBAction)btnLoginRegisterTapped:(UIButton *)sender { ... }

ヒント: Xcode で「Control-Drag」を使用してアクションを作成する場合、「タイプ」フィールドのポップアップを使用して、送信者の実際のタイプを選択します。次に、アクション メソッドが正しい引数の型で作成されます。

ここに画像の説明を入力

于 2013-10-28T17:59:52.223 に答える