4

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

#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] (
)

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

4

4 回答 4

3

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

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

    }
于 2013-03-19T16:28:48.250 回答
3

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

@synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
   //imageCollection1 = [[NSMutableArray alloc] init];
   //tapCollection1 = [[NSMutableArray alloc] init];
   NSLog(@"%@",tapCollection1);
   NSLog(@"%@",imageCollection1);
}
于 2013-03-19T16:31:05.767 回答
0

不要in of ,而是在init方法中做mutableArraysviewDidLoadSecondViewControllerinitSecondViewController

 -(id)initWithNibName //this method
  {
     //create object

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

   }

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

于 2013-03-19T16:36:34.623 回答
0

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

只需删除

imageCollection1 = [[NSMutableArray alloc] init];
tapCollection1 = [[NSMutableArray alloc] init];
于 2013-03-19T16:41:27.590 回答