如何使 UIWebView 在 Objective-C 中不可见

发布于 2024-12-12 23:52:35 字数 1319 浏览 1 评论 0原文

我必须隐藏 UIView 的 UIWebView onload,并仅在单击 UIButton 时显示它...我尝试这样做:

在 .hi 中创建与 xib 对应的 IBOutlet。

#import <UIKit/UIKit.h>

@interface SingleViewController : UIViewController{

    UIWebView *gif;
    UIButton *go;
}
@property(nonatomic,retain) IBOutlet UIWebView *gif;
@property(nonatomic,retain) IBOutlet UIButton *go;
@end

在 .mi 中尝试以这种方式隐藏 uiwebview:

@implementation SingleViewController
@synthesize sug;
@synthesize gif;
@synthesize avvia;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        gif.hidden=YES;
        UIImage *keys = [UIImage imageNamed:@"chiave.png"];
        [self.go setImage:chiave forState:UIControlStateNormal];               
        [self.go setImage:chiave forState:UIControlStateHighlighted];          
        [self.go setImage:chiave forState:UIControlStateDisabled];             
        [self.go setImage:chiave forState:UIControlStateSelected];
    }
    return self;
}

我以这种方式加载此 uiview:

SingleViewController *single = [[SingleViewController alloc]initWithNibName:@"SingleViewController" bundle:nil];
[self.view addSubview:single.view];

出了什么问题?

I have to hide an UIWebView onload of an UIView, and show it only when an UIButton is clicked... i try this:

In .h i create the IBOutlet corresponding to xib.

#import <UIKit/UIKit.h>

@interface SingleViewController : UIViewController{

    UIWebView *gif;
    UIButton *go;
}
@property(nonatomic,retain) IBOutlet UIWebView *gif;
@property(nonatomic,retain) IBOutlet UIButton *go;
@end

in .m i try to hide uiwebview in this way:

@implementation SingleViewController
@synthesize sug;
@synthesize gif;
@synthesize avvia;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        gif.hidden=YES;
        UIImage *keys = [UIImage imageNamed:@"chiave.png"];
        [self.go setImage:chiave forState:UIControlStateNormal];               
        [self.go setImage:chiave forState:UIControlStateHighlighted];          
        [self.go setImage:chiave forState:UIControlStateDisabled];             
        [self.go setImage:chiave forState:UIControlStateSelected];
    }
    return self;
}

and i load this uiview in this way:

SingleViewController *single = [[SingleViewController alloc]initWithNibName:@"SingleViewController" bundle:nil];
[self.view addSubview:single.view];

What goes wrong?

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

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

发布评论

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

评论(2

旧夏天 2024-12-19 23:52:36

您需要等到调用 viewDidLoad: 才能隐藏 UIWebView。这是因为 nib 中的项目尚未实例化,并且对 nil 对象调用方法将不起作用。

- (void)viewDidLoad
{
        gif.hidden=YES;
        UIImage *keys = [UIImage imageNamed:@"chiave.png"];
        [self.go setImage:chiave forState:UIControlStateNormal];               
        [self.go setImage:chiave forState:UIControlStateHighlighted];          
        [self.go setImage:chiave forState:UIControlStateDisabled];             
        [self.go setImage:chiave forState:UIControlStateSelected];
}

You need to wait until viewDidLoad: is called to hide the UIWebView. This is because the items in your nib have not been instantiated yet and calling methods on a nil object will have no effect.

- (void)viewDidLoad
{
        gif.hidden=YES;
        UIImage *keys = [UIImage imageNamed:@"chiave.png"];
        [self.go setImage:chiave forState:UIControlStateNormal];               
        [self.go setImage:chiave forState:UIControlStateHighlighted];          
        [self.go setImage:chiave forState:UIControlStateDisabled];             
        [self.go setImage:chiave forState:UIControlStateSelected];
}
你的呼吸 2024-12-19 23:52:36

一些建议:

确保 initWithNibName:bundle: 实际上被调用,因为如果直接从 nib 文件加载视图控制器,则可能不会被调用。

添加子视图时,请确保也设置它的框架。

A couple of suggestions:

Make sure that initWithNibName:bundle: is actually being called, since it may not be if the View Controller is loaded from a nib file directly.

When adding your subview, make sure to set it's frame, too.

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