我正在开发一个iOS应用程序,它可以在开发的Web浏览器中打开许多URL。我没有为每个链接设置视图,而是决定创建一个全局NSString变量,其中包含每次按下按钮时在浏览器的url代码上设置的url,因此调用浏览器的视图并设置URL 。我已经将Stanford关于传递数据http://www.youtube.com/watch?v=L-FK1TrpUng的教程作为参考,但是在构建浏览器时没有加载网页。另一方面,如果我接受变量并在视图中定义它确实加载方法它确实采取了网址...
这是我的第一个视图标题..
#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
现在是第一个视图的.m中的操作
-(IBAction) switchMenuMail{
MenuMailController *screenMail = [[MenuMailController alloc] initWithNibName:Nil bundle:Nil];
screenMail.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"];
现在是包含全局变量的第二个视图的.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
现在是第二个视图的.m
#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
问题是它正确构建但是当加载第二个视图时它不会加载网页的URL。
答案 0 :(得分:0)
你想在呈现其他视图控制器之前移动“在这里我设置变量......”代码行吗?
-(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];
另外,测试是否在执行initWithNibName时正确运行viewDidLoad。如果是这样,那么您过早地设置Web URL。如果是这样,您可以添加成员函数来设置URL,并在该成员函数中设置Web视图的URL。
(+几个小时后)回答我自己的问题 - 所以initWithNibName:bundle不会导致loadView或viewDidLoad立即被调用。稍后调用它们(当另一个VC的视图出现在presentModelViewController函数中时)。这是有意义的,因为它们被懒惰地加载,所以直到那个时候才需要它们。