如何使用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

将这些字典发送给另一个视图控制器

进行此更改:

-(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代码大声笑

已经有一段时间了

尝试此

- (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"};
    }
} 
  • 首先在当前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 在目标视图controller中全球。

secviewcontroller.h

@property NSString *data;

哪个数据在viewDidLoad()方法中收到该打印。

secviewcontroller.m

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

相关内容

  • 没有找到相关文章

最新更新