UITableView prepareForSegue没有推送到UIWebView

时间:2013-05-04 22:12:07

标签: ios objective-c xcode uitableview uiwebview

我试图从WebListViewController推送到WebPageViewController,因此当按下一个单元格时,它会推送到UIWebView以显示与其关联的网络文章。但我无法让它发挥作用 -

WebListViewController.m

#import "WebListViewController.h"
#import "Feed.h"

@interface WebListViewController ()
@property (strong, nonatomic) NSArray *headlinesArray;
@end

@implementation WebListViewController
@synthesize tableView = _tableView;
@synthesize headlinesArray;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"standardCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
    NSString *headlineText = [NSString stringWithFormat:@"%@", feedLocal.headline];
    cell.textLabel.text = headlineText;

    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"webpage"]) {
    UITableViewCell *cell = (UITableViewCell*)sender;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    WebPageViewController *wpvc = (WebPageViewController *) [segue destinationViewController];
    wpvc.buttonNumber = _buttonNumber;
    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
    NSString *stringWeb = [NSString stringWithFormat:@"%@", feedLocal.links.web.href];
    wpvc.stringWeb = stringWeb;
}

}

WebPageViewController.h

@interface WebPageViewController : UIViewController <UIWebViewDelegate>
@property (nonatomic) int buttonNumber;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) NSString *stringWeb;
@end

WebPageViewController.m

#import "WebPageViewController.h"
#import "Feed.h"

@interface WebPageViewController ()
@property (strong, nonatomic) NSArray *headlinesArray;
@end
@implementation WebPageViewController
@synthesize stringWeb;
@synthesize headlinesArray;

-(void)viewWillAppear:(BOOL)animated {
    NSURL *url = [NSURL URLWithString:stringWeb];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

-(void)viewWillDisappear:(BOOL)animated {
    [self.webView stopLoading];
}

JSON响应

{
    "timestamp": "2013-05-04T16:38:48Z",
    "feed": [{
        "headline": "Text of headline",
        "links": {
            "web": {
                "href": "http://website.com/article"
            },

我今天刚刚添加了<UIWebViewDelegate>部分因为我认为可能发生了什么,但它仍然无法正常工作。我知道cellForRowAtIndexPath是正确的,因为我在控制台中看到了它的所有正确数据。当我运行程序时,它似乎永远不会到达WebPageViewController,所选单元格会保持突出显示。如果问题是prepareForSegue或需要添加didSelectRowAtIndexPath或其他不同的内容,我无法弄清楚问题是什么。

(我从API中提取JSON数据。)

任何建议都会非常有用,我会添加所需的任何代码,谢谢!

1 个答案:

答案 0 :(得分:1)

我猜thinkCell是你在故事板中定义为原型单元格的单元格?如果是这样,请确保右键单击该单元格并将“选择”属性拖动到希望单元格推送到导航控制器的视图控制器上。

How to create a segue connection to another view controller

此外,请确保第一个视图嵌入在导航控制器中。如果不是,请在故事板中选择它,然后转到编辑菜单,选择“嵌入”,然后选择“导航控制器”。

How to embed a view in a navigation controller