我正在制作一个应用程序,在该应用程序中我有一个 View ,可以在其中显示 friend 的详细信息。 所以,如果我需要更改细节。我必须检查标签才能更改它。 在这里,我为每个标签设置了标签值。
如果用户按下标签,新的文本字段将出现在 View 上,用户可以更改详细信息。
为此,我编写了触摸开始方法,其中我正在检查触摸值... 但是每当我触摸任何标签时,我都会将 View 的标签值设置为零,因为我应该获取我单击的标签的标签值。
我的代码如下所示...
txtTmp = [[UITextField alloc] init];
//txtTmp is the textfield which should appear as super view of the label where i have clicked
txtTmp.delegate=self;
self.lblName.tag=11;
UITouch *touch = [touches anyObject];
//这里即使我启用了用户交互,我得到的标签值为 0...
NSLog(@"view.tag = %d",touch.view.tag);
if(touch.view.tag==11) {
[txtTmp setFrame:CGRectMake(162, 43, 138, 50)];
txtTmp.text=lblName.text;
[self.view addSubview:txtTmp];
[txtTmp becomeFirstResponder];
temp=1;
}
如果您对我的问题有任何疑问..请回复我....
谢谢
最佳答案
您应该启用 self.lblName 的用户交互。
[self.lblName setUserInteractionEnabled:YES];
您还可以使用viewWithTag消息在touchesbegan方法中检查触摸标签:
UITouch *touch = [touches anyObject];
if(touch.view == [self.view viewWithTag:11])
{
[txtTmp setFrame:CGRectMake(162, 43, 138, 50)];
txtTmp.text=lblName.text;
[self.view addSubview:txtTmp];
[txtTmp becomeFirstResponder];
temp=1;
}
关于iphone - 如何从 View 中了解标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477190/