2017-02-07 9 views
0

こんにちはすべてiOSプログラミングを初めて習得しました。ビュー内でボタンをタップすると、あるビューコントローラから2番目のビューコントローラにデータを渡す必要があります。データが辞書形式のWebサービスからフェッチされています。私の問題は、データが配列形式のときにデータを解析する方法です。以下は、私が応答として辞書を取得するときに使用しているコードです。view1でボタンをクリックすると、View1からView2にデータが渡されます。

アレイを解析して渡すにはどうすればよいですか? 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]; 
} 
+0

[ビューコントローラ間でデータを受け渡す]の可能複製(http://stackoverflow.com/questions/ 5210535/passing-data-between-view-controllers) –

+0

配列を使用する場合は、objectAtIndexメソッドを使用します。 – Pavankumar

+0

[ビューコントローラ間の変数の受け渡し]の可能な複製(0120-919-03-xx) – User511

答えて

0

第2ビューコントローラモーダルファイル。

#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 

ヘッダーに配列を宣言します。

#import <UIKit/UIKit.h> 

@interface View2Controller : UIViewController 

@property NSArray *array; 

@end 

ここにメインビューコントローラがあります。

#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 

マイストーリーボード - enter image description here

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]; 
    } 
} 

私はこれがあなたを願っています:https://agilewarrior.wordpress.com/2012/01/25/how-segue-in-ios-and-pass-data-from-one-viewcontroller-to-another/

関連する問題