Iphone 无法从另一个viewcontroller加载webview请求
我正在实现FB的一种功能Iphone 无法从另一个viewcontroller加载webview请求,iphone,ios5,xcode4.2,Iphone,Ios5,Xcode4.2,我正在实现FB的一种功能 从左侧的数据库填充菜单列表 单击任一菜单可在webview中加载相关网页 菜单和Webview都位于不同的viewcontroller中 单击菜单时,我向第一个viewcontroller发送通知消息,并在通知消息中向loadRequest写入代码 然而,这是行不通的。我能够从第一个Viewcontroller成功加载请求。但是,无法从另一个Viewcontroller执行此操作 这是我的密码: 视图控制器1: NSNotificationCenter *note =
NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note postNotificationName:@"ILikeTurtlesEvent" object:self];
在视图中加载
NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note addObserver:self selector:@selector(eventDidFire:) name:@"ILikeTurtlesEvent" object:nil];
- (void) eventDidFire:(NSNotification *)note {
web.delegate=self;
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
}
在视图控制器2中:
NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note postNotificationName:@"ILikeTurtlesEvent" object:self];
这可能是您尝试执行的另一个选项:
viewcontroller1.h:
-(void)clickmenu:(id)sender;
viewcontroller1.m:
-(void)clickmenu:(id)sender
{
viewcontroller2 *obj = [[viewcontroller2 alloc] initWithNibName: @"viewcontroller2" bundle: [NSBundle mainBundle]];
[obj Loadwebview:[NSURL urlwithstring:@"http://www.google.com"]];
}
ViewController2.h:
-(void)Loadwebview:(NSURL *)url;
viewcontroller2.m:
-(void)Loadwebview:(NSURL *)url
{
web.delegate=self;
[web loadRequest:[NSURLRequest requestWithURL:url]];
}
我找到了解决方案,我删除了该行: web.delegate=self; 代码工作了!!奇怪的但这是真的。这是我的工作代码:
- (void) eventDidFire:(NSNotification *)note {
// web.delegate=self;
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.11.77:5200/test/test.html"]]];
}
希望它能帮助别人!!!“:)在您的代码处,需要调用“单击菜单”以执行所有进程。但这不是我的问题的情况。请求是从Viewcontroller2而不是viewcontroller1启动的。