2

MasterViewController.h:

#import <UIKit/UIKit.h>

@interface MasterViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *listOfBugs;

MasterViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _listOfBugs = [[NSMutableArray alloc]init];
}

...
...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
    [_listOfBugs addObject:bug];
}

上記のコードでは、プロパティを持つオブジェクトを保持する配列を作成しました。別のクラスから配列内のオブジェクトにアクセスし、同じ配列にオブジェクトを追加したいと思います。私は次のように同じクラスから新しい配列を作成できることを知っています:

ListOfBugsViewController.m:

#import "ListOfBugsViewController.h"
#import "MasterViewController.h"
MasterViewController *myClass = [[MasterViewController alloc]init];


    NSMutableArray *array = [myClass.listOfBugs....

しかし、明らかにそれは私が探しているものではありません。すでに配列内にあるオブジェクトにアクセスし、新しい配列を作成したくない。

編集:少し簡単にするために、いくつかのクラスと、オブジェクトを追加する配列を1つだけ持っています。すべてのクラスは、それにオブジェクトを追加し、以前に追加されたオブジェクトを読み取ることができる必要があります。割り当ておよび初期化されていないクラスから同じ配列にアクセスするにはどうすればよいですか?

ありがとう

4

4 に答える 4

1

かなり簡単に他のクラスから配列にオブジェクトを追加します。

方法を見てください...

Base.m

...
_baseArray=[[NSMutableArray alloc]init];
[_baseArray addObject:@"Anoop"];
[_baseArray addObject:@"kumar"];
...

OtherClass.m

Base *baseObj=[Base new];
[baseObj.baseArray addObject:@"vaidya"];

編集:

我々の議論に従って

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self createObjects];

}

- (void) createObjects
{
    _listOfBugs=[[NSMutableArray alloc]init];

    [_listOfBugs addObject:@"Anoop"];

    NSLog(@"init done, %@",_listOfBugs);

}


- (id)init
{
    self = [super init];    
    if (self) {
        [self createObjects];
    }
    return self;
}
于 2012-12-06T08:29:56.573 に答える
0

しかし、明らかにそれは私が探しているものではありません。すでに配列内にあるオブジェクトにアクセスし、新しい配列を作成したくない。

新しい配列を作成するのではなく、既存の配列への新しいポインタを作成します。この新しいポインタを介してその配列を編集できます。

NSMutableArray *array = myClass.listOfBugs;
[array addObject:newBug];

ただし、新しいポインタを作成する必要はありません。プロパティを介して配列に直接アクセスできます。

[myClass.listOfBugs addObject:newBug];
于 2012-12-06T08:14:11.140 に答える
0

あなたの質問は紛らわしい言葉で表現されています。listOfBugs私はあなたが望むのはあなたのからの以前に作成されたインスタンスからプロパティにアクセスすることだと思いMasterViewControllerますListOfBugsViewController

あなたはあなたListOfBugsViewControllerによってインスタンス化されていますMasterViewControllerか?その場合は、に新しいプロパティを追加ListOfBugsViewControllerしてMasterViewController(このプロパティを弱くする)へのMasterViewControllerポインタを含め、インスタンス化した直後にそのポインタを自分自身に設定することができますListOfBugsViewController

于 2012-12-06T08:25:24.483 に答える
0

Objective Cの良いところは、オブジェクトが実際にはオブジェクトインスタンスへのポインタであるということです。さらに良いのは、オブジェクトを渡すときに、参照によって渡されることです(間違っている場合は訂正してください)。

他のクラスからメソッドを呼び出して、次のようにソースの可変配列を渡すことができます。

// ------------------------------------------
// Your "Other Class" .h header file
// ------------------------------------------

...

@interface OtherClass
{
    ...
}

...

+(void)addNewBugToArray:(NSMutableArray *)paramSourceArray;

@end

// ------------------------------------------
// Your "Other Class" .m implementation file
// ------------------------------------------

...

// ------------------------------------------
// Note: paramSourceArray is passed 
// by reference, meaning any change to it
// will affect the source array directly
// ------------------------------------------
+(void)addNewBugToArray:(NSMutableArray *)paramSourceArray
{
    [paramSourceArray addObject:@"newBug"];
}

...

@end




// ------------------------------------------
// Your Main View Controller .m file
// ------------------------------------------

#import "OtherClass.h"

...

-(void)viewDidLoad
{
   ...

   // initialize your source array
   sourceArray = [[NSMutableArray alloc] init];

   [btnAddBug addTarget:self selector:@selector(addNewBug) forControlEvent:UIControlEventTouchUpInside];

   ...

}

-(void)addNewBug
{
    // ---------------------------------------------------------
    // calling class method from "OtherClass" to add new bug
    // to our source array directly using pass by reference
    // ---------------------------------------------------------
    [OtherClass addNewBugToArray:sourceArray];
}
于 2012-12-06T08:38:30.750 に答える