UITextField的隐藏属性不起作用
在我的iPhone/iPad应用程序中(在Objective-C中),有一些UITextFields
,我正在编程添加并将这些添加到数组中。UITextField的隐藏属性不起作用
我想设置hidden
属性的某些按钮单击(其中我通过遍历数组找到特定的UITextField
)。
当我设置textfilled.hidden = true
(在按钮单击事件)时,它不会隐藏,而是在禁用模式下,如果我再次设置textfilled.hidden = false
,则启用它。
我已经尝试改变其他属性,如文本,背景颜色等在同一级别的所有工作正常,除了隐藏的属性。
注意:如果在添加文本字段(与UITextField
相同的对象)后设置textfilled.hidden = true
,则它完全隐藏。
更新:我已经使用了下面的代码:
UITextField *textField=[[[UITextField alloc] initWithFrame:CGRectMake(lastPoint.x, lastPoint.y, 60, 20)] autorelease];
textField.backgroundColor=[UIColor greenColor];
textField.textColor=[UIColor blackColor];
[textField addTarget:self action:@selector(handleEnterPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];
[capturedImage addSubview:textField];
[noteTextArray addObject:textField];
在此我创建的UITextField并将其添加到阵列(noteTextArray)和这里的调用.hidden属性:
-(void)handleEnterPressed:(UITextField *)textField
{
for(UITextField *noteText in noteTextArray)
{
if(noteText.tag==textField.tag)
{
noteText.backgroundColor=[UIColor purpleColor];
[email protected]"Hi";
noteText.hidden=true;
}
}
}
但它没有隐藏文本字段。
如果有人有任何想法或解决方案,请让我知道。
UPDATE 1:图像已采取通过捕捉web视图
UIGraphicsBeginImageContextWithOptions(webview.frame.size,NO,0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[webview.layer renderInContext: context];
capturedImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
的当前视图,并进一步添加到uiscrollviewer的图像的屏幕截图:
scrollViewer.delegate=self;
scrollViewer.contentOffset = CGPointZero;
capturedImage.contentMode= UIViewContentModeScaleAspectFit;
scrollViewer.userInteractionEnabled=true;
scrollViewer.contentMode= UIViewContentModeScaleAspectFit;
scrollViewer.scrollEnabled=YES;
[scrollViewer setBouncesZoom:YES];
scrollViewer.clipsToBounds= YES;
scrollViewer.contentSize = capturedImage.image.size;
scrollViewer.minimumZoomScale=0.1;
scrollViewer.maximumZoomScale=5.0;
scrollViewer.zoomScale=0.5;
if(capturedImage.superview != scrollViewer)
{
[scrollViewer addSubview:capturedImage];
}
属性不能设置为激活和停用所以这将是更好的方法如果u按照像按钮的情况设置属性1按下
textField.textAlignment = UITextAlignmentCenter;
并按下按钮
textField.textAlignment = UITextAlignmentLeft;
更新:
field.selected = TRUE;
if([field isHidden])
{
[field setHidden:FALSE];
}
else
{
[field setHidden:TRUE];
}
更新2:
- (void)viewDidLoad
{
[super viewDidLoad];
field = [[UITextField alloc] initWithFrame:CGRectMake(120, 20, 150, 20)];
[self.view addSubview:field];
field.placeholder = @"placeholder";
field.backgroundColor = [UIColor redColor];
field.textColor = [UIColor whiteColor];
}
-(IBAction) btnPressed:(id)sender
{
field.selected = TRUE;
if([field isHidden])
{
[field setHidden:FALSE];
}
else
{
[field setHidden:TRUE];
}
}
更新3:请下载sample代码(testTextField)
嗨@Kulss谢谢你的回复,但我不是问如何设置textAlignment,我问如何显示/隐藏它。 tfilled.hidden不工作,因为它应该工作。 – 2012-02-21 09:42:44
检查我的上述更新的代码,这一定会帮助你..我已经测试这 – kulss 2012-02-21 10:49:04
我已经应用您的代码,但不起作用。 – 2012-02-21 11:07:54
可以使用alpha属性。
要隐藏:
textfield.alpha = 0;
要再次显示:
textfield.alpha = 1;
我一直在使用textfield.alpha尝试,但它没” t工作......... – 2012-02-21 09:48:04
兄弟听,我做了Xcode项目,然后加入一些UITextFields programmaticaly。然后按钮显示/隐藏,在每种情况下是否使用true/false或YES/NO UITextField是隐藏/显示。
所以我觉得问题是某处else.Please对你的代码看看或张贴在这里这样就可以纠正,其中实际问题。
我在原始帖子的更新区域中添加了我在我的应用中使用的代码。 – 2012-02-21 09:59:27
这是您的代码中的逻辑错误 – Dhilip 2012-02-21 10:41:18
@Dhilip这里的逻辑错误是什么? – 2012-02-21 11:12:56
我只是想你的代码在我的项目,它的工作完美。在开始时它不工作,因为我没有分配noteTextArray。
所以调试和检查是否对UIControlEventEditingDidEndOnExit控制移动到方法“handleEnterPressed”,仅此而已。如果它在handleEnterPressed和noteTextArray不是空的,那么会正常工作,隐藏属性没有任何问题。我已经告诉过你问题在别的地方。
所以检查noteTextArray是否为空或不
我已经检查了所有的输入和所有有他们各自的价值观和事件工作正常。但问题仍然存在。如果我在创建时隐藏文本字段,那么它将隐藏,但如果它不起作用。 – 2012-02-21 11:07:02
没有必要为标签检查 - 你必须在发送的所有信息。 你可能想改变你的委托方法是这样的:
-(void)handleEnterPressed:(id)sender
{
NSLog (@"delegate called fot textfield with tag %d",(UITextField *)sender.tag);
(UITextField *)sender.backgroundColor = [UIColor purpleColor];
(UITextField *)[email protected]"Hi";
(UITextField *)sender.hidden=YES;
}
而且在控制台检查,如果您的委托方法被调用的。
我已经试过这个,并没有工作......它只是隐藏我的文本,并禁用所有的操作,但背景颜色仍然存在:( – 2012-02-21 12:03:33
这很奇怪,试试[(UITextField *)sender setHidden:YES ];而不是 – 2012-02-21 12:13:47
基本上代码是写。我已经检查我的应用程序,我将文本字段添加到uiimageview(capturedImage),但我将它添加到self.view而不是capturedImage然后它工作正常。问题是,如果我不把它添加到图像捕捉图像,它将不会与图像一起移动(根据功能) – 2012-02-21 12:20:42
嗨谢谢你们所有的时间和帮助。其实问题在于我(保存图像的意思)。我不需要每次都将图像保存在对象中。
再次感谢!
@Kulss谢谢你的时间。我将你的答案标记为决赛,因为这也有助于我解决另一个小问题。
谢谢Aadhira让我正确!你知道这个问题的答案吗?如是。请张贴答案。 – 2012-02-21 09:26:20
你在设置textfilled.hidden = true/false ......你应该使用textfilled.hidden = YES/NO来代替。 – Ravin 2012-02-21 09:28:19
@Ravin我同时设置的方式,但它并没有在所有的工作:( – 2012-02-21 09:31:36