大家好我是iOS编程新手。当在视图中点击按钮时,我需要将数据从一个视图控制器传递到第二个视图控制器。数据是以字典格式从Web服务获取的,我的问题是如何在数据格式化时解析数据。以下是我将字典作为响应时使用的代码。
我该怎么做才能解析并传递数组? TIA
- (IBAction)btnListClicked:(id)sender
{
ListVC *list = [[ListVC alloc]initWithNibName:@"ListVC" bundle:nil];
list.clientID= [detailsdict objectForKey:@"clientid"];
list.custID= [detailsdict objectForKey:@"custid"];
//detailsdict is NSMutableDictionary
[self.navigationController pushViewController:list animated:YES];
}
答案 0 :(得分:0)
第二个视图控制器模态文件。
#import "View2Controller.h"
@interface View2Controller ()
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
@implementation View2Controller
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
for (id object in self.array) {
// do something with object
NSLog(@"%@", object);
}
self.textLabel.text = self.array.lastObject;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
在标题中声明您的数组。
#import <UIKit/UIKit.h>
@interface View2Controller : UIViewController
@property NSArray *array;
@end
这是主视图控制器。
#import "ViewController.h"
#import "View2Controller.h"
@interface ViewController ()
@property NSArray *array;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.array = [NSArray arrayWithObjects:@"test",@"string2", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnListClicked:(UIButton *)sender {
[self performSegueWithIdentifier:@"passData" sender:sender];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"passData"]) {
View2Controller *vc = [segue destinationViewController];
vc.array = self.array;
}
}
@end
答案 1 :(得分:0)