将导航视图控制器单元链接到Web视图

时间:2013-06-14 12:48:51

标签: iphone objective-c xcode cocoa-touch uiwebview

我有一个带有多个单元格的导航控制器。我想将每个单元格链接到Web视图中的网页。但是,我不想为每个单元格创建另一个视图控制器。是否可以创建1个视图控制器并将所有单元格链接到同一个视图控制器,但是在每个单元格的Web视图中显示不同的网页?例如,有单元格1和单元格2.它们都链接到一个视图控制器,视图控制器中有一个Web视图。当您单击单元格1时,它会将您带到视图控制器,并在Web视图中将您带到http://example.com。单击单元格2时,它会将您带到与单元格1相同的视图控制器,但在Web视图中,它会将您带到http://example.com/1。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

只需在viewcontroller中声明一个属性,其中包含您要请求的网址。

@property (strong,nonatomic) NSString *url;

当您点击某个单元格时,会根据您的viewcontroller

的属性将该单元格的网址指定为

你的viewcontroller看起来应该是这样的。

标题

#import <UIKit/UIKit.h>

@interface RSViewController : UIViewController

@property (weak, nonatomic) NSString *url;

@end

实施

#import "RSViewController.h"

@interface RSViewController ()<UIWebViewDelegate>

@property (strong,nonatomic) IBOutlet UIWebView *webview;

@end

@implementation RSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.webview.delegate = self;

    NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.url]];
    [self.webview loadRequest:urlRequest];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"Did start loading");
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"Did finish loading");
}

@end

现在,您只需要在viewcontroller中引用tableviewcontroller,并在单击鼠标时设置url属性。

相关问题