提问人:subbu 提问时间:5/4/2017 更新时间:5/4/2017 访问量:1078
如何使用Segues将字典数据从一个视图控制器发送到另一个视图控制器?
how to send the dictionary data from one view controller to another view controller using segues?
问:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *svc=[segue destinationViewController];
if ([segue.identifier isEqualToString:@"show"]) {
svc.scrool=@"dict";
}
}
- (IBAction)buttonAction:(id)sender {
dict= @{@"firstname":@"firstname.textfield.text", @"lastname":@"lastname.textfield.text", @"fullname":@"fullname.textfield.text",@"gender":@"gender.textfield.text",@"country":@"country.textfield.text",@"state":@"state.textfield.text",@"phnumber":@"phnumber.textfield.text",@"email":@"email.textfield.text",@"zipcode":@"zipcode.textfield.text",@"landnumber":@"landnumber.textfield.text"};
}
你能请任何人给我答案,因为这些需要使用 segues 将这些字典发送到另一个视图控制器
答:
0赞
KKRocks
5/4/2017
#1
试试这个
- (IBAction) buttonAction:(id)sender
{
[self performSegueWithIdentifier:@"show" sender:sender];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *svc=[segue destinationViewController];
if ([segue.identifier isEqualToString:@"show"]) {
svc.scrool=@"dict";
svc.dict = @{@"firstname":@"firstname.textfield.text", @"lastname":@"lastname.textfield.text", @"fullname":@"fullname.textfield.text",@"gender":@"gender.textfield.text",@"country":@"country.textfield.text",@"state":@"state.textfield.text",@"phnumber":@"phnumber.textfield.text",@"email":@"email.textfield.text",@"zipcode":@"zipcode.textfield.text",@"landnumber":@"landnumber.textfield.text"};
}
}
评论
1赞
KKRocks
5/4/2017
您需要在 SecondViewController 中声明名为 'dict' 的 NSDictionary 属性。
1赞
Jacob Boyd
5/4/2017
#2
进行以下更改:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *svc=[segue destinationViewController];
if ([segue.identifier isEqualToString:@"show"]) {
svc.scrool = dict;
}
}
在代码中,您传递的是值为“dict”的字符串,而不是 dict 变量。
编辑: 我还想在此代码中添加:
dict= @{@"firstname":@"firstname.textfield.text", @"lastname":@"lastname.textfield.text", @"fullname":@"fullname.textfield.text",@"gender":@"gender.textfield.text",@"country":@"country.textfield.text",@"state":@"state.textfield.text",@"phnumber":@"phnumber.textfield.text",@"email":@"email.textfield.text",@"zipcode":@"zipcode.textfield.text",@"landnumber":@"landnumber.textfield.text"};
您实际上并没有从文本字段中提取值。如果已正确设置所有这些属性,则需要使用:
dict= @{@"firstname": firstname.textfield.text, @"lastname": lastname.textfield.text, @"fullname": fullname.textfield.text, @"gender": gender.textfield.text, @"country": country.textfield.text, @"state": state.textfield.text, @"phnumber": phnumber.textfield.text, @"email": email.textfield.text, @"zipcode": zipcode.textfield.text, @"landnumber": landnumber.textfield.text};
此外,请确保 SecondViewController 的 .h 文件中具有该属性:scrool
@property (strong, nonatomic) NSDictionary *scrool;
哇,我已经有一段时间没有写 Obj-c 代码了,哈哈
0赞
Sakir Sherasiya
5/4/2017
#3
首先在当前 ViewController 中导入另一个 ViewController。
#import "secViewController.h"
现在,在调用 prepare for degue 方法时设置另一个视图数据。
ViewController.m 文件
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
secViewController *dest = [segue destinationViewController];
dest.data = @"test data";
}
这里在目标 ViewController 中将 NSString *data 声明为全局。
secViewController.h
@property NSString *data;
哪一个数据在方法中接收打印。viewDidLoad()
secViewController.m(秒视图控制器.m)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"%@",self.data);
}
评论