Ios 在2个视图中传递字符串,以便在XCode中将其用作URL

Ios 在2个视图中传递字符串,以便在XCode中将其用作URL,ios,objective-c,views,Ios,Objective C,Views,我正在开发一个iOS应用程序,它可以在开发的web浏览器中打开许多url。我没有为每个链接设置视图,而是决定创建一个全局NSString变量,该变量包含每次按下按钮时在浏览器url代码上设置的url,以便调用浏览器视图并设置url。我曾参考斯坦福大学关于传递数据的教程,但构建浏览器时不会加载网页。另一方面,如果我获取变量并在view Dod load方法中定义它,它将获取url 这是我的第一个视图的标题 #import <UIKit/UIKit.h> #import "MenuMai

我正在开发一个iOS应用程序,它可以在开发的web浏览器中打开许多url。我没有为每个链接设置视图,而是决定创建一个全局NSString变量,该变量包含每次按下按钮时在浏览器url代码上设置的url,以便调用浏览器视图并设置url。我曾参考斯坦福大学关于传递数据的教程,但构建浏览器时不会加载网页。另一方面,如果我获取变量并在view Dod load方法中定义它,它将获取url

这是我的第一个视图的标题

#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
现在,包含全局变量的第二个视图的.h

#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
问题是它构建正确,但是当加载第二个视图时,它不会加载网页的URL

在显示另一个视图控制器之前,是否要将“此处我设置了变量…”代码行向上移动

-(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];
另外,在使用nibName进行初始化时,测试viewDidLoad是否正确运行。如果是这样,那么您设置web URL太早了。如果是这样,您可以添加一个成员函数来设置URL,并在该成员函数中设置web视图的URL

(+几个小时后)回答我自己的问题-因此initWithNibName:bundle不会导致立即调用loadView或viewDidLoad。稍后会调用它们(当其他VC的视图显示在presentModelViewController函数中时)。这是有道理的,因为它们是惰性加载的,所以直到那时才需要它们

#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
-(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];