xcode 在“_strong id”类型的对象上找不到属性“标签”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19641751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:12:49  来源:igfitidea点击:

Property 'tag' not found on object of type '_strong id'

iosxcodepropertiestags

提问by Cybernetic

I am building an App according to this tutorial (http://bit.ly/NI9kQe) which uses a custom web api to connect to the web server. One of the requirements is to detect whether or not the Login or Register button has been tapped. This is done using a "tag" which has been set for the button in interface builder (the register button has a tag of 1).

我正在根据本教程 ( http://bit.ly/NI9kQe)构建一个应用程序,它使用自定义 Web api 连接到 Web 服务器。要求之一是检测是否已点击登录或注册按钮。这是使用在界面构建器中为按钮设置的“标签”完成的(注册按钮的标签为 1)。

The chunk of code sits inside the btnLoginRegisterTapped method as follows (the error occurs on the line -> NSString* command = (sender.tag==1)?@"register":@"login";):

代码块位于 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;

                               }

                           }];

}

}

when I try to build the project (workspace actually) I get the error:

当我尝试构建项目(实际上是工作区)时,出现错误:

Property 'tag' not found on object of type '_strong id'

在“_strong id”类型的对象上找不到属性“标签”

I am using xcode 5.0 deploying for iOS7.

我正在使用为 iOS7 部署的 xcode 5.0。

Thanks,

谢谢,

回答by Martin R

Property syntax cannot be used with variables of the generic idtype.

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

So either replace sender.tagby the method call [sender tag]or better, use the actual type of the senderargument in the method definition:

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

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

Tip:When creating the action with "Control-Drag" in Xcode, use the pop-up in the "Type" field to select the actual type of the sender. Then the action method is created with the correct argument type.

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

enter image description here

在此处输入图片说明