Jayyrus
Jayyrus

Reputation: 13051

How to make an UIWebView invisible in objective-c

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?

Upvotes: 2

Views: 2833

Answers (2)

DHamrick
DHamrick

Reputation: 8488

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

Upvotes: 3

Ash Furrow
Ash Furrow

Reputation: 12421

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.

Upvotes: 1

Related Questions