gpt4 book ai didi

ios - 将 JSON 字符串从一个 View 传递到另一个 View

转载 作者:行者123 更新时间:2023-11-29 03:21:13 31 4
gpt4 key购买 nike

我有一个 TableView ,我在其中解析一些 JSON 数据并将其显示在自定义单元格中。在一个 View 中,我正在解析来自 Facebook 的特定用户的帖子。

我在 TableView 中显示预览,但我想将数据传递到另一个 View 。例如,我在表格 View 中阅读了一篇文章的预览,但我想阅读整篇文章。

当我单击该帖子(单元格)时,我应该被定向到一个新的 View Controller ,其中显示完整的帖子(在标签或 TextView 中)。

我该怎么做?

这是我将数据解析到 TableView 的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


strURLToLoad = [[NSMutableString alloc] init];

[btnFaceBook setTitle:@"link.com/json.php?name=Name" forState:UIControlStateDisabled];
[btnTwitter setTitle:@"link1.com/json.php?name=Name" forState:UIControlStateDisabled];
[btnTwitter2 setTitle:@"link2.com/json.php?name=Name" forState:UIControlStateDisabled];

[btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
[btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

[btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
[btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

[btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
[btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];



NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
PostsObject *cell = [nib objectAtIndex:0];
fontForCellText = cell.title.font;
cellTextWidth = cell.title.frame.size.width;
cellHeightExceptText = cell.frame.size.height - cell.title.frame.size.height;

[self.navigationController setNavigationBarHidden:YES];

self.tableView.separatorColor = [UIColor clearColor];

// Setting Up Activity Indicator View
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicatorView.hidesWhenStopped = YES;
self.activityIndicatorView.center = self.view.center;
[self.view addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
self.tableView.separatorColor = [UIColor clearColor];

// Initializing Data Source
movies = [[NSMutableArray alloc] init];

[self btnFromTabBarClicked:btnFaceBook];
}

- (void)loadJSONFromCurrentURL
{
[self.activityIndicatorView startAnimating];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strURLToLoad]];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[movies setArray:JSON];
[self.activityIndicatorView stopAnimating];
[self.tableView reloadData];

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];

[operation start];
}

- (IBAction)btnFromTabBarClicked:(UIButton *)sender
{
//Unselect all 3 buttons
btnFaceBook.selected = btnTwitter.selected = btnTwitter2.selected = NO;

//Select the button that was clicked
sender.selected = YES;

//Set the string of an NSMutableString property called strURLToLoad with the URL
//The URL is pre stored in the text of the UIButton in the Disabled text.
[strURLToLoad setString:[sender titleForState:UIControlStateDisabled]];

//Load the URL
[self loadJSONFromCurrentURL];
}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (movies && movies.count) {
return movies.count;
} else {
return 0;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"PostsObject";

PostsObject *cell = (PostsObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
NSString *strText = [movie objectForKey:[self getTextKey]];

CGRect rect = cell.title.frame;
rect.size.height = [self getHeightForText:strText];
cell.title.frame = rect;
cell.title.text = strText;
cell.arrow.center = CGPointMake(cell.arrow.frame.origin.x, rect.origin.y + rect.size.height/2);
cell.published.text = [movie objectForKey:@"published"];
cell.twitterName.text = [movie objectForKey:[self getTwitterName]];
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
NSString *strText = [movie objectForKey:[self getTextKey]];

CGFloat cellHeight = cellHeightExceptText + [self getHeightForText:strText];

return cellHeight;
}

- (NSString *)getTextKey
{
return btnTwitter.selected?@"tweet":@"message";
}

- (NSString *)getTwitterName
{
return btnTwitter2.selected?@"user":@"user";
}

- (CGFloat)getHeightForText:(NSString *)strText
{
CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"labelSize.height = %f",labelSize.height);
return labelSize.height;
}

最佳答案

有多种方法可以实现这一目标。

您可以实现 tableview:didSelectRowAtIndexPath: 并在其中分配下一个 VC 并将您的数据传递给它的属性,然后呈现该 VC。

或者您可以实现从表格单元格到新 VC 的 segue,然后在 prepareForSegue: 中将数据传递给它的属性。

无论哪种方式,所呈现的 VC 都可以通过 viewDidLoad 将数据加载到该 View 中。

就传递 JSON 而言,它并不是特别理想。您可能会考虑创建自己的基于 Objective-C 的模型对象,JSON 数据将被解析成这些模型对象,然后在您的应用中使用这些模型对象。

关于ios - 将 JSON 字符串从一个 View 传递到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21065297/

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