0

Hi all I am new to iOS programming. When a button is tapped in view I need to pass data from one view controller to second view controller. Data is being fetched from a web service in Dictionary format, my problem is how to parse data when the data is in Array format. Following is the code what i am using to when I get dictionary as response.

What should I do to parse and pass a Array ? 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];
}
Chanchal Warde
  • 983
  • 5
  • 17
Abhinav
  • 9
  • 5
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Jordan Montel Feb 07 '17 at 08:53
  • If it is array use objectAtIndex method.And show response will help you according to that. – Pavankumar Feb 07 '17 at 09:25
  • Possible duplicate of [Passing variables between view controllers](http://stackoverflow.com/questions/11577484/passing-variables-between-view-controllers) – User511 Feb 07 '17 at 09:28
  • can we use objectAtIndex method in buttonclick ? @Pavankumar – Abhinav Feb 07 '17 at 09:42
  • Is your response is array? If yes, two methods to get string from array. NSArray *array = @[@"a",@"b",@"c"]; NSLog(@"%@",array[0]); NSLog(@"%@",[array objectAtIndex:0]); – Pavankumar Feb 07 '17 at 09:57
  • Add your sample data in question to let us know what you are actually trying to do. – Chanchal Warde Feb 07 '17 at 10:13

2 Answers2

0

Second view controller modal file.

#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

Declare your array in header.

#import <UIKit/UIKit.h>

@interface View2Controller : UIViewController

@property NSArray *array;

@end

And here is main view controller.

#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

My Storyboard - enter image description here

Igor Kuznetsov
  • 108
  • 1
  • 10
0
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}
// When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"MySegue" sender:sender];
}

// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {

        // Get destination view
        SecondView *vc = [segue destinationViewController];

        // Get button tag number (or do whatever you need to do here, based on your object
        NSInteger tagIndex = [(UIButton *)sender tag];

        // Pass the information to your destination view
        [vc setSelectedButton:tagIndex];
    }
}

I hope this will you:https://agilewarrior.wordpress.com/2012/01/25/how-segue-in-ios-and-pass-data-from-one-viewcontroller-to-another/

Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19