到目前为止,这是我所发现的:
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@end
FirstViewController.m
#import "FirstViewController.h"
#import "CellDetailView.h"
@interface FirstViewController()
@property (strong, nonatomic) NSMutableArray *myMutableArray;
@end
- (void)viewDidLoad{
//...
}
//There's a JSON method here that's working just fine populating the UITableViewCells as intended.
// ...JSON URL STUFF...
// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
// NSError *errorJson = nil;
// NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&errorJson];
self.coolDict = [dataDictionary objectForKey:@"coolStuff"];
self.myMutableArray = [self.coolDict objectForKey:@"stuff_1"];
[self.tableView reloadData];
//UITableViewCell method here, works great!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
CellDetailView *nextViewController = [[CellDetailView alloc] init]; //This is a UIViewController Class I made.
NSDictionary *someInfo = self.myMutableArray[indexPath.row]; // This is logging the array for each cell, that's cool.
NSLog(@"%@", someInfo);
[[self navigationController] pushViewController:nextViewController animated:YES];
}
CellDetailView.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface CampaignItemDetails : UIViewController
@end
CellDetailView.m
#import "CellDetailView.h"
#import "FirstViewController.h"
@interface CellDetailView ()
@end
@implementation CellDetailView
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *myDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
myDetailLabel.text = someInfo[@"title"];
[self.view addSubview:myDetailLabel]; //THIS DOESNT WORK :(
}
@end
如何将myMutableArray放入CellDetailView中,以便可以使用数组中的更多JSON数据填充ViewController?
*我知道这个主意是推送到CellDetailView(而不是从FirstViewController提取)。*
我已经看到了有关如何将数据从视图控制器传递到视图控制器的答案,但这非常抽象,我对 objective-c 还是个新手。我还发现了很多带有Storyboard和Nibs的东西,但我没有使用这些东西之一。
最佳答案
我很想像这样重写initMethod:
NSDictionary *someInfo = self.myMutableArray[indexPath.row];
CellDetailView *nextViewController = [[CellDetailView alloc] initWithInfo:someInfo];
[[self navigationController] pushViewController:nextViewController animated:YES];
确保在cellDetailView中添加了初始化方法:
- (id) initWithInfo:(NSDictionary *) info {
self = [super init];
if (self) {
//Now you can save the info to a private NSDictionary property:
}
return self;
}
确保添加:
- (id) initWithInfo:(NSDictionary *) info;
在您的cellDetailView .h中。