ios – 在’_strong id’类型的对象上找不到属性’标记’

我正在根据本教程( http://bit.ly/NI9kQe)构建一个App,它使用自定义web api连接到Web服务器.其中一个要求是检测是否已点击“登录”或“注册”按钮.这是使用为接口构建器中的按钮设置的“标记”完成的(注册按钮的标记为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’类型的对象上找不到属性’tag’

我正在使用xcode 5.0部署iOS7.

谢谢,

解决方法

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

因此,要么通过方法调用[sender tag]替换sender.tag,要么更好,
方法定义中使用sender参数的实际类型:

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

提示:在Xcode中使用“Control-Drag”创建动作时,使用“类型”字段中的弹出窗口选择发件人的实际类型.然后使用正确的参数类型创建action方法.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...