2011-11-01 44 views
2

看不見我不得不隱藏的UIView的UIWebView的onload事件,並說明它被點擊一個的UIButton只有當......我試試這個:如何使一個UIWebView在Objective-C

在.hi創建IBOutlet中對應於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 

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

錯在何處?

+1

把'gif.hidden = YES;'放入'loadView'或'viewDidLoad'方法 – beryllium

回答

3

你需要等到viewDidLoad:被調用來隱藏UIWebView。這是因爲您的筆尖中的項目尚未實例化,並且調用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]; 
} 
+0

謝謝!但我使用方法initwithnibname初始化singleviewcontroller,然後使用此代碼。 :) – JackTurky

1

一對夫婦的建議:

確保initWithNibName:包:實際上是被調用,因爲如果視圖控制器從筆尖文件直接裝也未必。

當你添加你的子視圖時,一定要設置它的框架。