【问题标题】:how to send the dictionary data from one view controller to another view controller using segues?如何使用 segues 将字典数据从一个视图控制器发送到另一个视图控制器?
【发布时间】:2017-10-02 13:51:52
【问题描述】:
@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 将这些字典发送到另一个视图控制器的问题

【问题讨论】:

标签: ios iphone ipad ios4


【解决方案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 代码了,哈哈

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 您需要在 SecondViewController 中为名为 'dict' 的 NSDictionary 声明属性。
    【解决方案3】:
    • 首先在当前ViewController中导入另一个ViewController。

      #import "secViewController.h"
      

    现在在调用准备degue方法时将数据设置为另一个视图数据。

    ViewController.m 文件

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        secViewController *dest = [segue destinationViewController];
        dest.data = @"test data";
    }
    

    这里将 NSString *data 声明为目标 V​​iewController 中的全局变量。

    secViewController.h

    @property NSString *data;
    

    viewDidLoad() 方法中接收打印的哪个数据。

    secViewController.m

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      相关资源
      最近更新 更多