开发者

Defined outlets, connected them, they all returns (null)

开发者 https://www.devze.com 2022-12-23 16:54 出处:网络
I\'m trying to play with a WebView. I made an outlet: IBOutlet UIWebView *browser; Defined it as a property:

I'm trying to play with a WebView.

I made an outlet:

IBOutlet UIWebView *browser;

Defined it as a property:

@property (nonatomic, retain) IBOutlet UIWebView *browser;

Synthethized it:

@synthesize browser;

Finally, I connected it in Interface Builder, really it is.

Then I try to do something with it i.e.:

[browser loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apple.com"]]];

Or also:

Etape *etape = [[Etape alloc] init];

NSString *html = [etape generateHTMLforEtape:[current_etape objectAtIndex:0]];
[browser loadHTMLString:html baseURL:nil];

[etape release];

I get no errors, I tried to Build & Analyse, no notices or warnings or errors.. I've been searching for one whole day, please help me :/

Thanks a lot!

EDIT: Here's screenshots of my connections for my WebView: Connections http://pousli.net/uploads/crap/outlet.png

EDIT: That is how I call the view:

    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];

    dvController.workflow_id = parent_id;

    Etape *etape = [[Etape alloc] init];
    dvController.etapes = [etape getEtapes:开发者_Go百科parent_id];
    [etape release];

    [self.navigationController pushViewController:dvController animated:YES];

    [dvController release];


Some thoughts:

Depending on which method you're running this code in, the outlets might not be hooked up yet. The most common place to begin interacting with your outlets is in a viewDidLoad, viewWillAppear: or viewDidAppear: method, after the call to super's implementation. If you're trying to do this in your init method, your outlets are probably still nil. The nib isn't loaded and the outlets aren't hooked up until somebody requests the view of your viewcontroller. Might this be what you're experiencing?

If you NSLog(@"%@", browser); in a method where you know the view should exist, but get (null) logged, then it's a sign that your outlets have lost their connections somehow.

EDIT #1 Are you sure you have the name of your xib spelled correctly when you call initWithNibName:bundle:? (btw, you can pass in nil for the bundle, and it will assume [NSBundle mainBundle])


One thing is that you may have wired it to the @property OR to the IBOutlet (in my experience Interface Builder gets confused easily). Maybe try declaring as:

UIWebView *browser_;
@property (nonatomic, retain) IBOutlet UIWebView *browser;
@synthesize browser=browser_;


One explanation is that the browser is not being retained because your not using the "self.attribute" construction to call the synthesized setter that does the retaining. The browser is loading in the nib but by the time you get around to using it has been released.

Try switching "browser" to "self.browser" and see if that resolves the problem.


It could be due to the way you're initing the view. If the view controller is being loaded in code then you'll need to ensure that its' view is being loaded too.

To do this init the view controller with initWithNibName:bundle: and ensure that 'file owner' in the NIB which contains the view is set to the class name of the view controller.


I've had this problem before. It LOOKS connected, but things just don't work. I hate to say it, but have you tried restarting Xcode? (I've had this problem before, and, well, restarting Xcode resolved the issue.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号