2013-05-04 76 views
1

我试图从WebListViewController推到WebPageViewController,所以当一个单元格被按下时,它会推送到一个UIWebView来显示与之相关的网页文章。但我就是不能让它working-UITableView prepareForSegue不推送到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

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

How to create a segue connection to another view controller

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

How to embed a view in a navigation controller

+0

感谢您的回应!是的,standardCell是我在故事板中定义为原型的单元格。即使我从TableView推送 - > WebView(您的图像是TableView - > TableView),这是否会处理您的建议工作? – Realinstomp 2013-05-04 23:13:47

+0

是的,它适用于所有视图控制器,无论类型如何。 – TheQ 2013-05-04 23:28:55

+0

你是最棒的。完美工作! – Realinstomp 2013-05-04 23:46:40