prepareForSegue将我带到一个空白页面

时间:2013-08-28 23:32:43

标签: iphone ios objective-c json

我必须查看控制器(UpcomingReleaseViewController和ReleaseController)。

我正在使用JSON填充我的应用。

我的应用程序的主页面是UpcomingReleasesViewController,一旦你单击一个单元格它应该带你到Release页面并显示更多信息,但由于某些原因每次你点击一个单元格它会带你到一个空白页。

ReleaseView.h

@interface ReleaseViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *releaseView;
@property (strong, nonatomic) NSString *release_name;
@end

ReleaseView.m

@interface ReleaseViewController ()

@end

@implementation ReleaseViewController

@synthesize releaseView = _releaseView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.releaseView.text = self.release_name;
}

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

@end

UpcomingReleaseViewController.m

#import "UpcomingReleasesViewController.h"
#import "UpcomingRelease.h"
#import "ReleaseViewController.h"

@interface UpcomingReleasesViewController ()

@end

@implementation UpcomingReleasesViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *upcomingReleasesURL = [NSURL URLWithString:@"http://obscure-lake-7450.herokuapp.com/upcoming.json"];

    NSData *jsonData = [NSData dataWithContentsOfURL:upcomingReleasesURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.upcomingReleases = [NSMutableArray array];

    NSArray *upcomingReleasesArray = [dataDictionary objectForKey:@"upcoming_releases"];

    for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
        UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithReleaseName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
        upcomingRelease.release_price = [upcomingReleaseDictionary objectForKey:@"release_price"];
        upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:@"release_date"];
        upcomingRelease.thumbnail = [upcomingReleaseDictionary objectForKey:@"thumbnail"];
        upcomingRelease.url = [NSURL URLWithString:[upcomingReleaseDictionary objectForKey:@"url"]];
        [self.upcomingReleases addObject:upcomingRelease];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UpcomingRelease *upcomingRelease = [self.upcomingReleases objectAtIndex:indexPath.row];

    NSData *imageData = [NSData dataWithContentsOfURL:upcomingRelease.thumbnailURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
    cell.textLabel.text = upcomingRelease.release_name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ — $%@",[upcomingRelease formattedDate], upcomingRelease.release_price];

    return cell;
}

#pragma mark - View Upcoming Release

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ( [segue.identifier isEqualToString:@"showRelease"] ) {
        ReleaseViewController *rvc = (ReleaseViewController *)[segue destinationViewController];
        rvc.release_name = [self.upcomingReleasesArray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
    }
}


@end

Storyboard Segue命名正确(showRelease),我将UITextView链接到我的ReleaseViewController(releaseView)。也许是与即将到来的阵容有关?我是iOS的新手,我不知道问题是什么。

1 个答案:

答案 0 :(得分:0)

虽然您想通过单击表格单元格来导航,但您应该使用tableView的didSelectRowAtIndexPath委托方法。在该方法中,只需使用您在prepareForSegue中使用的内容并删除此prepareForSegue。因为在转到下一个View之前将调用prepareForSegue。点击单元格时,didSelect将被调用。如果要在调用下一个视图之前设置任何数据,则应使用prepareForSegue。