【问题标题】:How to pass array form one view to another view in iphone如何将数组从一个视图传递到iphone中的另一个视图
【发布时间】:2013-03-19 16:25:47
【问题描述】:

我是电话编程的新手。我已经在数组中存储了一些数据,我想将该数组数据从一个视图传递到另一个视图。我尝试了什么但它不起作用。任何人都可以告诉我什么是错误在那。

#import"firstviewcontroller.h"

 @property(nonatomic,retain)NSMutableArray *tapCollection;
 @property(nonatomic,retain)NSMutableArray *imageCollection;

#import"firstviewcontroller.m"

   @synthesize imageCollection,tapCollection;
    -(void)viewdidload
    {
    self.tapCollection = [[NSMutableArray alloc] init];
    self.imageCollection = [[NSMutableArray alloc] init];
    }
    - (void)insertNewObject:(id)sender
    {

        secondviewcontroller*r= [[secondviewcontrolleralloc] initWithNibName:@"secondviewcontroller" bundle:nil];
        self.navigationController.navigationBar.tintColor=[UIColor blackColor];
         r.imageCollection1 =imageCollection;
             r.tapCollection1 =tapCollection;
        [self.navigationController pushViewController:r animated:YES];

    }

实际上我存储在数组中的数据是图像和按钮标记值。在控制台中,它显示的图像和按钮标记值存储在数组中

2013-03-19 21:54:03.374 Taukyy[290:1c103] (
    0,
    "<UIImage: 0x9cd59e0>",
    1,
    "<UIImage: 0x9cd6220>",
    2,
    "<UIImage: 0x9cd6b70>"
)

导入“secondviewcontroller.h”

@property(nonatomic,retain)NSMutableArray *tapCollection1;
@property(nonatomic,retain)NSMutableArray *imageCollection1;

导入“secondviewcontroller.m”

@synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
    imageCollection1 = [[NSMutableArray alloc] init];
   tapCollection1 = [[NSMutableArray alloc] init];
   NSLog(@"%@",tapCollection1);
   NSLog(@"%@",imageCollection1);
}

但是这里的值没有显示。它的显示如下

2013-03-19 21:29:16.379 Taukyy[594:1c103] (
)
2013-03-19 21:29:16.380 Taukyy[594:1c103] (
)
2013-03-19 21:29:16.381 Taukyy[594:1c103] (
)

请任何人告诉我这段代码有什么错误 谢谢 阿斯兰

【问题讨论】:

  • 这是数据。您使用地址而不是名称到达它。拥有不同地址的同名对象没有好处。

标签: iphone ios


【解决方案1】:

secondviewcontroller.m 中删除viewDidLoad's 数组分配

tapCollection1 & imageCollection1retain 属性。所以它应该retain 分配的对象。

您的 secondviewcontroller.h 应该如下所示,

@property(nonatomic,retain)NSMutableArray *tapCollection1;
@property(nonatomic,retain)NSMutableArray *imageCollection1;

- (void)viewDidLoad
{
  //remove the allocation codes
}

您可以在firstviewcontroller.m之类的地方登录,

 - (void)insertNewObject:(id)sender
    {

         secondviewcontroller*r= [[secondviewcontrolleralloc] initWithNibName:@"secondviewcontroller" bundle:nil];
         self.navigationController.navigationBar.tintColor=[UIColor blackColor];
         r.imageCollection1 =imageCollection;
         r.tapCollection1 =tapCollection;
         NSLog(@"%@",r.imageCollection1);
         NSLog(@"%@",r.tapCollection1);
        [self.navigationController pushViewController:r animated:YES];

    }

【讨论】:

  • +1 很好的解释...但是您必须对其进行格式化,以便其他人可以轻松理解
【解决方案2】:

不要再次分配/初始化您的数组。您在准备转场时设置它们。

@synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
   //imageCollection1 = [[NSMutableArray alloc] init];
   //tapCollection1 = [[NSMutableArray alloc] init];
   NSLog(@"%@",tapCollection1);
   NSLog(@"%@",imageCollection1);
}

【讨论】:

    【解决方案3】:

    为什么要分配以前分配的数组。

    删除

    imageCollection1 = [[NSMutableArray alloc] init];
    tapCollection1 = [[NSMutableArray alloc] init];
    

    【讨论】:

      【解决方案4】:

      不要在viewDidLoadSecondViewControllerinit mutableArrays , 而是在SecondViewControllerinit 方法中执行此操作

       -(id)initWithNibName //this method
        {
           //create object
      
            self.imageCollection1 = [NSMutableArray alloc] init];
            self.tapCollection1 = [NSMutableArray alloc] init];
      
         }
      

      您现在将获得正确的值..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 1970-01-01
        相关资源
        最近更新 更多