iphone - 苹果手机 : Calling Webview from anther View Controller class

标签 iphone objective-c ipad uiwebview webview

我对此很陌生,因为我有 C++ 和 Perl 背景。试图从书本上学习,但他们缺少 Objective-C 的一些核心概念。我需要有关此代码的一些帮助来提取网页:

RootViewController.m

    - (void)fetchWebsite:(NSString*)website{
    NSLog(@"address: %@", website);

    NSString *urlAddress = website;
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webImageDisplay loadRequest:requestObj];
    [webImageDisplay release];
}

- (void)viewDidLoad {
    [self fetchWebsite:@"website here"];
    [super viewDidLoad];
}

这段代码工作得很好,但我遇到的问题是从另一个类调用这个方法,如下面的代码:

我正确地从第二个 View Controller 调用详细 View Controller ,如下所示:

SubViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    switch (indexPath.row) {
        case 0: { 
            AnotherViewController *cViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
            cController.contentSizeForViewInPopover = CGSizeMake(320, 350);
            [self.navigationController pushViewController:cViewController animated:YES];
            [cViewController release];
            break;
        }
        case 1: {
            break;
        }
    }
}

然后我有一个第三个 View Controller ,其中包含指向我想要在 WebView 上显示的新网页的链接。当我调用这里的方法访问RootViewController中的函数时,它似乎没有响应。

另一个ViewController.m

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

    NSString *itemRequested = [conusItems objectAtIndex:indexPath.row];
    NSLog(@"logging: %@", itemRequested);

    RootViewController *parent = [[RootViewController alloc] init];
    [parent fetchWebsite:@"another website here"];

    [itemRequested release];

}

我单击 TableView 上的按钮,我看到 RootViewController 中正在使用 NSLog 调用代码,但它似乎没有显示新页面。没有错误或警告,有什么想法吗?谢谢

最佳答案

如果可以的话,如果您将 Web View 旋转为它自己的 View Controller ,您的生活会更轻松,您的应用程序也会更灵活。

真正的问题是您没有导航到 Web View Controller (并且您可能没有正确初始化它。)

代码可能看起来更像这样:

MyWebViewController *webVC = [[MyWebViewController alloc] initWithNibName:@"MyWebViewContorller" bundle:nil];
webVC.address = @"whatever";
[self.navigationController pushViewController:webVC animated:YES];
[webVC release];

关于iphone - 苹果手机 : Calling Webview from anther View Controller class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5108938/

相关文章:

android - 移动设备和 HTML5 应用程序的合理缩略图大小是多少?

iphone - 分页和 UITableView?

ios - 方法被多次调用

ios - cocos2d 2.0-rc2 : end the director and restart

iphone - PurgeIdleCellConnections 导致应用程序卡住然后崩溃

objective-c - 比较 objective-c 中的数组

ios - scrollview 在 autolayout xcode 6.4 中不滚动

iphone - 试图了解iOS平台上的内存管理

iphone - 按下主页按钮后动画不起作用

ipad - jquery-ui 可排序 |如何让它在 iPad/触摸设备上工作?