我正在使用RSS应用程序,在我的上一个视图(tableView)中,我想使用cell.detailTextlabel来调用WebViewController并在Safari中打开相关文章。
其实我不知道这是不是最好的方法,但我需要这个“阅读更多”来打开整篇文章。
在我的第三部分(附图)的末尾,我插入了“Read More” - detailTextLabel.text = @“Read More” - 但我不知道如何链接到WebViewController并传递正确的URL。
在网上搜索,我发现这个例子在Safari中打开:
**WebViewController.h**
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController
@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) UIWebView *webView;
- (id)initWithURL:(NSString *)postURL title:(NSString *)postTitle;
@end
**WebViewController.m**
@implementation WebViewController
@synthesize url = _url, webView = _webView;
- (id)initWithURL:(NSString *)postURL title:(NSString *)postTitle
{
self = [super init];
if (self) {
_url = postURL;
self.title = postTitle;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.url = [self.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *newURL = [NSURL URLWithString:[self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// Do any additional setup after loading the view.
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:newURL]];
}
- (void)viewDidAppear:(BOOL)animated {
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
提前致谢。
答案 0 :(得分:0)
您可以将自定义UIButton
实例添加到tableview单元格,并根据您的结构使用indexPath.row或indexPath.section对其进行标记。在按钮操作上,获取发件人的标记并从dataSource数组中获取确切的URL,并使用该URL调用WenViewController。
您可以在单元格的contentView中添加具有所需框架的按钮。例如
CGRect cellFrame = cell.contentView.frame;
UIButton *readMore = [[UIButton alloc]
initWithFrame:CGRectMake(cellFrame.origin.x+20, cellFrame.origin.y+cellFrame.size.height - yourButtonHeight -10, yourButtonWidth, yourButtonHeight)];
[readMore addTarget:self action:@selector(openWebView:) forControlEvents:UIControlEventTouchUpInside];
[readMore setTitle:@"Read More" forState:UIControlStateNormal];
[readMore setTag:indexPath.row];
[cell.contentView addSubview:readMore];
// Release readMore button if not using ARC
然后制作方法
-(void)openWebView:(UIButton*)sender{
int tag = sender.tag;
// Get URL from array using tag as index
}