【发布时间】:2012-12-15 21:11:39
【问题描述】:
您好,我正在开发一个基于 Web 视图的应用程序,我有一个 WebController,其中有一个保存当前 URL 的变量。
我最近添加了代码来启动一个视图,这个工作正常,这个视图还包含另一个网络视图,如果我将一个网站加载到这个网络视图中,这个网络视图工作正常,例如:
[[mywebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[@"http://www.google.com/search?hl=en&q=" stringByAppendingString:@"test"]]]];
但是我试图访问保存当前 URL 的变量,如下所示:
[[mywebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[@"http://www.google.com/search?hl=en&q=" stringByAppendingString:currentURL]]]];
但是,当通过链接到 WebController 来执行此操作时,currentURL 似乎不等于任何内容,但是从第一响应者执行此操作时效果很好(当然视图不再显示)
我的问题是如何让我的 currentURL 变量在从 WebController 链接时工作?
我对可可比较陌生,所以如果这是一个简单的问题,我很抱歉!
编辑:从 cmets 添加
在initWithWindowController 方法中,currentURL 设置为@"",在dealloc 中设置为nil。 currentURL 来自其他网络视图,请参见此处:
- (void)webView:(WebView *)wv didStartProvisionalLoadForFrame:(WebFrame *)frame {
if (frame != [self.webView mainFrame])
return;
self.currentUrl = [[[[frame provisionalDataSource] request] URL] absoluteString];
[self retain];
}
我在 WebController.h 中声明 currentURL
@interface WebController : NSObject <DOMEventListener>
{
IBOutlet NSString *currentURL;
}
@property (nonatomic, copy) IBOutlet NSString *currentURL;
@end
我正在尝试在 DisplayInView 函数中使用 WebController.m 中的 currentURL。
-(IBAction) DisplayInView:(id) sender
{
if ([siteview isInFullScreenMode])
{
[siteview exitFullScreenModeWithOptions:nil];
}
else
{
[[mywebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [@"google.com/search?hl=en&q="stringByAppendingString:currentURL]]]]; siteview enterFullScreenMode:[[siteview window] screen] withOptions:nil];
}
}
@synthesize siteview;
@end
【问题讨论】:
-
你能显示你设置 currentURL 变量的位置吗?
-
要回答这个问题,我们需要知道您如何声明“currentURL”以及如何为其赋值。
-
在dealloc的initWithWindowController中设置为@"": currentURL=nil; currentURL 来自另一个 Web 视图,请参见此处: - (void)webView:(WebView *)wv didStartProvisionalLoadForFrame:(WebFrame *)frame { if (frame != [self.webView mainFrame]) return; self.currentUrl = [[[[帧临时数据源] 请求] URL] absoluteString]; [自我保留]; }
-
不确定,但您可能需要 [self.currentURL retain] 吗?而不是[自我保留]
-
这并没有什么区别,我在 WebController.h 中这样声明它: [at]interface WebController : NSObject
{ NSString *currentURL; } [at]property (nonatomic, copy) NSString *currentURL; [at]end(我不允许在 cmets 中使用 [at] 符号,所以是的。)
标签: objective-c cocoa variables view controller