2013-10-28 63 views
6

我正在根据本教程构建应用程序(http://bit.ly/NI9kQe),该应用程序使用自定义Web API连接到Web服务器。其中一个要求是检测登录或注册按钮是否被轻敲。这是通过使用在界面生成器中为按钮设置的“标签”完成的(注册按钮的标签为1)。在'_strong id'类型的对象上找不到属性“标记”

这段代码位于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)[email protected]"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“

我没有找到一个m使用xcode 5.0为iOS7部署。

感谢,

回答

15

属性的语法不能与通用id类型的变量使用。

因此,无论该方法调用替换sender.tag[sender tag]更好, 使用实际类型sender参数的方法定义:

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

提示:当创建与“控制行动-Drag“, 使用”类型“字段中的弹出窗口来选择发件人的实际类型。 然后,使用正确的参数类型创建操作方法。

enter image description here

+0

真棒..感谢这么多! – Cybernetic

+0

@ user1639594:不客气! –

相关问题