【问题标题】:Pass data from View1 to View2 when a button is clicked in view1当在 view1 中单击按钮时,将数据从 View1 传递到 View2
【发布时间】:2017-02-07 08:49:40
【问题描述】:

大家好,我是 iOS 编程新手。当在视图中点击按钮时,我需要将数据从一个视图控制器传递到第二个视图控制器。正在从字典格式的 Web 服务中获取数据,我的问题是当数据为数组格式时如何解析数据。以下是我在获取字典作为响应时使用的代码。

我应该如何解析和传递一个数组? 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];
}

【问题讨论】:

  • 如果是数组使用objectAtIndex方法。显示响应会帮助你。
  • 我们可以在 buttonclick 中使用 objectAtIndex 方法吗? @Pavankumar
  • 你的响应是数组吗?如果是,有两种方法从数组中获取字符串。 NSArray *array = @[@"a",@"b",@"c"]; NSLog(@"%@",array[0]); NSLog(@"%@",[数组 objectAtIndex:0]);

标签: ios objective-c iphone xcode nsmutabledictionary


【解决方案1】:

第二个视图控制器模态文件。

#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

我的故事板 -

【讨论】:

    【解决方案2】:
    - (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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多