UITextField的隐藏属性不起作用

时间:2012-02-21 09:20:11

标签: iphone objective-c click uitextfield hidden

在我的iPhone / iPad应用程序中(在Objective-C中),有一些UITextFields我正在逐步添加,这些进一步添加到数组中。

我想在某个按钮点击上设置hidden属性(我通过遍历数组找到了特定的UITextField。)

当我设置textfilled.hidden = true(按钮点击事件)时,它不会被隐藏在禁用模式中,如果我再次设置textfilled.hidden = false,则启用它。

我尝试在同一级别更改其他属性,如文本,背景颜色等,除隐藏属性外,所有工作都正常。

注意:如果在添加文本字段(使用textfilled.hidden = true的相同对象)后设置UITextField,则它会完全隐藏。

更新:我使用了以下代码:

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];
        noteText.text=@"Hi";
        noteText.hidden=true;
    }
}
}

但它没有隐藏文本字段。

如果有人有任何想法或解决方案,请告诉我。

更新1:通过捕获WebView的当前视图的屏幕截图来拍摄图像

 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];
        }

4 个答案:

答案 0 :(得分:1)

您可以使用alpha属性。

隐藏:

textfield.alpha = 0;

再次展示:

textfield.alpha = 1;

答案 1 :(得分:0)

属性不能设置为活动和停用所以如果您根据按下按钮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)

答案 2 :(得分:0)

我刚刚在我的项目中尝试过您的代码,它的工作非常完美。一开始它没有用,因为我没有分配noteTextArray。

所以调试并检查UIControlEventEditingDidEndOnExit控件是否移动到方法“handleEnterPressed”,就是这样。如果它在handleEnterPressed中并且noteTextArray不为空则那么工作正常,隐藏属性没有任何问题。我已经告诉你问题在别处。

所以检查noteTextArray是否为空

答案 3 :(得分:0)

不需要标签检查 - 您拥有发件人的所有信息。 您可能想要更改您的委托方法,如下所示:

-(void)handleEnterPressed:(id)sender
{
  NSLog (@"delegate called fot textfield with tag %d",(UITextField *)sender.tag);

  (UITextField *)sender.backgroundColor = [UIColor purpleColor];
  (UITextField *)sender.text=@"Hi";
  (UITextField *)sender.hidden=YES;
}

并检查控制台是否完全调用了委托方法。

相关问题