如何在objective-c中使UIWebView不可见

时间:2011-11-01 12:34:25

标签: objective-c uiwebview hide show

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

在.h中我创建了与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
在.m中我试图以这种方式隐藏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];

出了什么问题?

2 个答案:

答案 0 :(得分:3)

您需要等到调用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];
}

答案 1 :(得分:1)

一些建议:

确保实际调用了initWithNibName:bundle:,因为如果直接从nib文件加载View Controller,可能不会这样做。

添加子视图时,请务必同时设置它的框架。