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>"
)

import"secondviewcontroller.h"

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

import"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

のではなく、 のメソッドinitmutableArraysそれを行いますviewDidLoadSecondViewControllerinitSecondViewController

 -(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 に答える