gpt4 book ai didi

ios - 如何以编程方式将 JSON 数组从 UITableViewCell 传递到新的 UIViewController?

转载 作者:行者123 更新时间:2023-11-29 10:45:54 25 4
gpt4 key购买 nike

到目前为止,这是我想通的:

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 拉取)。*

我看到了一个关于如何将数据从 View Controller 传递到 View Controller 的答案,但它非常抽象,而且我对 Objective-C 还很陌生。我还发现了很多关于 Storyboards 和 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.

关于ios - 如何以编程方式将 JSON 数组从 UITableViewCell 传递到新的 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22403647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com