iPhone-如何从第二个UIViewController获取URL以加载到第一个UIViewController的WebView中

iPhone-如何从第二个UIViewController获取URL以加载到第一个UIViewController的WebView中,iphone,url,uiview,uiviewcontroller,uiwebview,Iphone,Url,Uiview,Uiviewcontroller,Uiwebview,我想做的是我有一个书签系统,但我的问题是从第二个UIViewController中的UITableView获取URL,如果可能的话,将所选URL加载到第一个UIViewController中的WebView。我确实有一些代码,但这不起作用,我需要一个解决我的问题的办法 这是我目前拥有的代码 -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ ViewCont

我想做的是我有一个书签系统,但我的问题是从第二个UIViewController中的UITableView获取URL,如果可能的话,将所选URL加载到第一个UIViewController中的WebView。我确实有一些代码,但这不起作用,我需要一个解决我的问题的办法

这是我目前拥有的代码

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ViewController *controller = [[ViewController alloc]init];

    NSString *urlWeb = [subtitles objectAtIndex:indexPath.row];
    [controller.igiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlWeb]]];

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];     
}试试这个

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

ViewController *controller = [[ViewController alloc]init];

NSString *urlWeb = [subtitles objectAtIndex:indexPath.row];
[controller.igiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlWeb]]];

[self.navigationController pushViewController:controller animated:YES];

 }

您要做的是初始化上一个视图控制器的另一个实例,而不是使用加载到导航堆栈中的现有实例。因此,当您调用dismissViewController方法时,您的视图将被解除,并且您既没有使用新创建的实例,也没有使用现有实例。 我建议创建一个委托,并使用该委托将所选URL传递回FirstViewController。 在FirstViewController.h中,在类FirstViewController的
@接口
块之外使用以下代码

@protocol UserSelectionDelegate
-(void)userSelected:(NSString*)selectedURLString;
@end
在SecondViewController的
@接口
块内

id<UserSelectionDelegate> delegate; //Make a property of this as well
现在,您所要做的就是返回FirstViewController.m,在创建SecondViewController的实例时添加
SecondvInstance.delegate=self
然后实现
userSelected:
方法将URL加载到FirstViewController的webView中

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *urlWeb = [subtitles objectAtIndex:indexPath.row];
[self.delegate userSelected:urlWeb];
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}