我必须查看控制器(UpcomingReleaseViewController 和 ReleaseController)。
我正在使用 JSON 来填充我的应用程序。
我的应用程序的主页是 UpcomingReleasesViewController,一旦你点击一个单元格,它应该带你到发布页面并向你显示更多信息,但由于某种原因,每次你点击一个单元格时,它都会带你到一个空白页面。
发布视图.h
@interface ReleaseViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *releaseView;
@property (strong, nonatomic) NSString *release_name;
@end
发布视图.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
即将到来的ReleaseViewController.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)。也许这与即将到来的ReleasesArray 有关?我是iOS新手,不知道是什么问题。