UITextField 的隐藏属性不起作用

发布于 2025-01-07 05:16:03 字数 2547 浏览 5 评论 0原文

在我的 iPhone/iPad 应用程序(在 Objective-C 中)中,有一些 UITextFields 我正在以编程方式添加它们,并将它们进一步添加到数组中。

我想在某些按钮单击时设置 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];
        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];
        }

In my app for iPhone/iPad (in Objective-C), there are some UITextFields which I am adding progrmatically and these further added to array.

I want to set the hidden property on some button click (where I found the particular UITextField by traversing the array).

When I set the textfilled.hidden = true (at button click event), then it is not hidden instead in disabled mode and if I again set textfilled.hidden = false, then it enables.

I have tried changing other properties like text, background color etc. at the same level all works fine except the hidden property.

Note: If it set textfilled.hidden = true after adding the text field (with the same object of UITextField) then it hides perfectly.

UPDATE: I have used the following code:

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

In this I am creating a UITextField and adding it to array(noteTextArray) and the calling the .hidden property here:

-(void)handleEnterPressed:(UITextField *)textField 
{
for(UITextField *noteText in noteTextArray)
{
    if(noteText.tag==textField.tag)
    {
        noteText.backgroundColor=[UIColor purpleColor];
        noteText.text=@"Hi";
        noteText.hidden=true;
    }
}
}

But it is not hiding the text field.

Please let me know if somebody has any idea or solution.

UPDATE 1: Image has been taken by capturing the screenshot of current view of WebView

 UIGraphicsBeginImageContextWithOptions(webview.frame.size,NO,0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    [webview.layer renderInContext: context];
    capturedImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

And the image further added to 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];
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撕心裂肺的伤痛 2025-01-14 05:16:03

您可以使用 alpha 属性。

隐藏:

textfield.alpha = 0;

再次显示:

textfield.alpha = 1;

You can use alpha property.

To hide:

textfield.alpha = 0;

To show again:

textfield.alpha = 1;
ヤ经典坏疍 2025-01-14 05:16:03

属性无法设置为活动和停用,因此如果您根据按下按钮 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:请下载 示例代码 (testTextField)

Property cannot set as active and deactivate so This will be better approach if u set the property according to the situation like on button 1 pressed

textField.textAlignment = UITextAlignmentCenter;

and on button pressed

textField.textAlignment = UITextAlignmentLeft;

update:

   field.selected = TRUE;

    if([field isHidden])
{
    [field setHidden:FALSE];
}
else  
{
    [field setHidden:TRUE];
}

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


}

update 3: please download the sample code (testTextField)

柠檬色的秋千 2025-01-14 05:16:03

我刚刚在我的项目中尝试了你的代码,它运行完美。一开始它就不起作用,因为我没有分配 noteTextArray。

因此,调试并检查 UIControlEventEditingDidEndOnExit 上的控件是否移动到方法“handleEnterPressed”,就是这样。如果它在handleEnterPressed中并且noteTextArray不为空,那么将正常工作,隐藏属性没有任何问题。我已经告诉过你问题出在其他地方。

所以检查 noteTextArray 是否为空

I just tried your code in my project, its working perfect. on very start it was not working because i didn't allocated noteTextArray.

so debug and check whether on UIControlEventEditingDidEndOnExit control moves to Method "handleEnterPressed", that's it. If it is in handleEnterPressed and noteTextArray is not empty then will work fine, there is nothing wrong with hidden property. I already told you problem is somewhere else.

so check whether noteTextArray is empty or not

墟烟 2025-01-14 05:16:03

无需进行标签检查 - 您拥有发件人中的所有信息。
您可能想像这样更改您的委托方法:

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

并在控制台中检查您的委托方法是否被调用。

There's no need for tag checking - you have all the information in sender.
You might want to change your delegate method like this:

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

And check in console if your delegate method is called at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文