我必須查看控制器(UpcomingReleaseViewController和ReleaseController)。prepareForSegue帶我到一個空白頁面
我正在使用JSON來填充我的應用程序。
從我的應用程序的主頁是UpcomingReleasesViewController,一旦你點擊一個單元格它應該帶你到發佈頁面,並顯示更多的信息,但由於某種原因,每次你點擊一個單元格,它會帶你到一個空白頁。
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
故事板Segue命名正確(showRelease),並將UITextView鏈接到我的ReleaseViewController(releaseView)。可能與即將發佈的Release數組有關係嗎?我是iOS新手,我不知道問題可能是什麼。
你在哪裏叫segue?在'didSelectRowAtIndexPath'中? – CaptJak
在prepareForSegue中記錄[self.upcomingReleasesArray objectAtIndex:[[self.tableView indexPathForSelectedRow] row],看它是否爲零。 – rdelmar