在 2 个视图中传递一个字符串,以便它可以用作 XCode 中的 URL
我正在开发一个 iOS 应用程序,它可以在开发的网络浏览器中打开许多 url。我决定创建一个全局 NSString 变量,而不是为每个链接设置视图,其中包含每次按下按钮时要在浏览器的 url 代码上设置的 url,以便调用浏览器的视图并设置 URL 。我参考了斯坦福大学关于传递数据的教程 http://www.youtube.com /watch?v=L-FK1TrpUng 但构建浏览器时不会加载网页。另一方面,如果我采用变量并在视图 did 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
现在第一个视图的 .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。
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];
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"];
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否想在呈现其他视图控制器之前将“此处我设置变量...”代码行向上移动?
另外,测试 viewDidLoad 在执行 initWithNibName 时是否正确运行。如果是这样,那么您设置网址太早了。如果是这样,您可以添加一个成员函数来设置 URL,并在该成员函数中设置 Web 视图的 URL。
(+几个小时后)回答我自己的问题 - 所以 initWithNibName:bundle 不会导致 loadView 或 viewDidLoad 立即被调用。它们稍后会被调用(当另一个 VC 的视图出现在presentModelViewController 函数中时)。这是有道理的,因为它们是延迟加载的,因此直到那时才需要它们。
Do you want to move that "here I set the variable..." lines of code up before presenting the other view controller?
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.