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

前端之家收集整理的这篇文章主要介绍了ios – 在’_strong id’类型的对象上找不到属性’标记’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在根据本教程( http://bit.ly/NI9kQe)构建一个App,它使用自定义web api连接到Web服务器.其中一个要求是检测是否已点击“登录”或“注册”按钮.这是使用为接口构建器中的按钮设置的“标记”完成的(注册按钮的标记为1).

代码块位于btnLoginRegisterTapped方法内部,如下所示(错误发生在行 – > NSString * command =(sender.tag == 1)?@“register”:@“login”;):

  1. - (IBAction)btnLoginRegisterTapped:(id)sender {
  2.  
  3. //form fields validation
  4. if (fldUserName.text.length < 4 || fldPassword.text.length < 4) {
  5. // [UIAlertView error:@"Enter username and password over 4 chars each."];
  6.  
  7. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!" message:@"Enter username and password over 4 chars each." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
  8. // optional - add more buttons:
  9. [alert addButtonWithTitle:@"Yes"];
  10. [alert show];
  11. return;
  12.  
  13. }
  14.  
  15. //salt the password
  16. NSString* saltedPassword = [NSString stringWithFormat:@"%@%@",fldPassword.text,kSalt];
  17.  
  18. //prepare the hashed storage
  19. NSString* hashedPassword = nil;
  20. unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];
  21.  
  22. //hash the pass
  23. NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
  24. if (CC_SHA1([data bytes],[data length],hashedPasswordData)) {
  25. hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
  26. } else {
  27.  
  28. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!" message:@"Password cannot be reset!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
  29. // optional - add more buttons:
  30. [alert addButtonWithTitle:@"Yes"];
  31. [alert show];
  32. return;
  33. }
  34.  
  35. //************ THIS IS WHERE THE ERROR OCCURS *****************//
  36. //check whether it's a login or register
  37. NSString* command = (sender.tag==1)?@"register":@"login";
  38. NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
  39. command,@"command",fldUserName.text,@"username",hashedPassword,@"password",nil];
  40.  
  41. //make the call to the web API
  42. [[API sharedInstance] commandWithParams:params
  43. onCompletion:^(NSDictionary *json) {
  44. //handle the response
  45. //result returned
  46. NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];
  47.  
  48. if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
  49. //success
  50. [[API sharedInstance] setUser: res];
  51. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  52.  
  53. //show message to the user
  54. [[[UIAlertView alloc] initWithTitle:@"Logged in"
  55. message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"] ]
  56. delegate:nil
  57. cancelButtonTitle:@"Close"
  58. otherButtonTitles: nil] show];
  59.  
  60. } else {
  61. //error
  62.  
  63. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps!!" message:@"Server down? Try Again" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
  64. // optional - add more buttons:
  65. [alert addButtonWithTitle:@"Yes"];
  66. [alert show];
  67. return;
  68.  
  69. }
  70.  
  71. }];

}

当我尝试构建项目(实际工作空间)时,我收到错误

在’_strong id’类型的对象上找不到属性’tag’

我正在使用xcode 5.0部署iOS7.

谢谢,

解决方法

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

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

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

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

猜你在找的iOS相关文章