如何使用segues将字典数据从一个视图控制器发送到另一个视图控制器?

时间:2017-05-04 11:43:02

标签: ios iphone ipad ios4

@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将这些字典发送到另一个视图控制器

3 个答案:

答案 0 :(得分:1)

进行此更改:

-(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代码lol

已经有一段时间了

答案 1 :(得分:0)

试试这个

- (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"};
    }
} 

答案 2 :(得分:0)

  • 首先在当前的ViewController中导入另一个ViewController。

    #import "secViewController.h"
    

现在,在调用为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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@",self.data);
}