开发者

Passing a string within 2 views so it can be used as an URL in XCode

I'm developing an iOS app which opens many url's in a developed web browser. Instead of setting a view for each of the links, I've decided to create a global NSString variable containing the url to set on the browser's url code each time a button is pressed so the view with the browser is called and the URL is set. I've used as reference the Stanford's tutorial about passing data http://www.youtube.com/watch?v=L-FK1TrpUng but when building the browser doesn't loads the web page. on the other hand, if I take the variable and define it within the view did load method it does take the url...

This is my 1st view's header..

#import <UIKit/UIKit.h>
#import "MenuMailController.h"     // <------ I imported the header from the second view  that contains the variable

    @interface iTecViewController : UIViewController {

    }
    -(IBAction) switchMenuMail;
@end

now the action in .m of the first view

-(IBAction) switchMenuMail{
    MenuMailController *screenMail = [[MenuMailController alloc] initWithNibName:Nil bundle:Nil];
    scr开发者_如何转开发eenMail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:screenMail animated:YES];
    [screenMail release];

        //Here I set the variable to the URL's string... 
    screenMail.pageURL = [NSString stringWithFormat:@"http://www.google.com"];

now the .h of the second View containing the global variable...

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "iTecViewController.h"
@interface MenuMailController : UIViewController
{
    IBOutlet UIWebView *webMail;
     NSString *pageURL;                 //<---- the string variable containing the URL

}
-(IBAction) back;
@property (copy) NSString *pageURL;
@end

Now the .m of the second view

#import "MenuMailController.h"
#import "iTecViewController.h";
@implementation MenuMailController
@synthesize paginaInternet;
-(IBAction) back{
    [self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {

    [super viewDidLoad];


  //with the "pageURL" variable already set it should load the page URL but it doesn't 

[webMail loadRequest:[NSURLRequest requestWithURL:[ NSURL URLWithString:pageURL]]];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return YES;
}
@end

The problem is that it builds correctly but when loaded the second view it wont load the web page's URL.


Do you want to move that "here I set the variable..." lines of code up before presenting the other view controller?

-(IBAction) switchMenuMail{
MenuMailController *screenMail = [[MenuMailController alloc] initWithNibName:Nil bundle:Nil];
screenMail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

//Here I set the variable to the URL's string... 
screenMail.pageURL = [NSString stringWithFormat:@"http://www.google.com"];

[self presentModalViewController:screenMail animated:YES];
[screenMail release];

Also, test if viewDidLoad is run right when you do the initWithNibName or not. If so, then you are setting the web URL too early. If so, you could add a member function to set the URL and in that member function set the web view's URL.

(+ a few hours later) Answering my own question - so initWithNibName:bundle doesnt cause loadView or viewDidLoad to get called immediately. They get called later (when the other VC's view is presented in the presentModelViewController function). This makes sense as they are loaded lazily, so they aren't needed until that time.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜