0

これが私の状況です。次のようにして、ViewController クラス A に TableViewController クラス B に移動するボタンを付けました。

- (void) goToClassB
{
    ViewControllerB *viewController =
    [[ViewControllerB alloc] initWithStyle:UITableViewStylePlain];
    // Present view controller modally.
    if ([self
        respondsToSelector:@selector(presentViewController:animated:completion:)]) {
        [self presentViewController:viewController animated:YES completion:nil];
    } else {
        [self presentModalViewController:viewController animated:YES];
    }
}

クラス A とクラス B の両方からアクセスおよび編集できる配列を使用できるようにしたいのですが、どうすればこれを達成できますか?

4

6 に答える 6

1

次のように、クラス B に配列変数を作成します。

@interface classB:NSObject
{
  NSMutableArray *arrayFromA;
}
@property (nonatomic, assign)  NSMutableArray *arrayFromA;

変数を合成します。

このメソッドでは、次のような配列を渡します。

- (void) goToClassB
{
   ViewControllerB *viewController = [[ViewControllerB alloc] initWithStyle:UITableViewStylePlain];
   [viewController setArrayFromA:yourArray];
   // Present view controller modally.
   if ([self
     respondsToSelector:@selector(presentViewController:animated:completion:)])
    {
     [self presentViewController:viewController animated:YES completion:nil];
    }
    else
    {
      [self presentModalViewController:viewController animated:YES];
    }
}
于 2012-11-06T08:14:52.427 に答える
0

The simplest way would, just as others point out, be simply passing the array to B by setting a property, but one other option would be to let B have a weak back reference to A. Thus you're always using the same array. This could be useful if A and B are changing the array at the same time.

@interface ViewControllerA : UIViewController
@property (nonatomic, strong)  NSArray *array;
@end 

@interface ViewControllerB : UIViewController
@property (nonatomic, weak)  ViewControllerA *viewControllerA;
@end 

/* When you're creating the ViewControllerB, do this: */
...
viewController.viewControllerA = self;
...

/* Use the array (From ViewControllerB) */

- (void)doSomethingWithTheArray
{
    self.viewControllerA.array = ...
}
于 2012-11-06T08:22:41.260 に答える
0

I mention that you want edit by both so.You can use application delegation for sharing app level variable. Check this link.

Some code is here. In your app delegate class.

@interface YourDelegateClass:UIResponder
{
  NSMutableArray *array;
}
@property (nonatomic, assign)  NSMutableArray *array;

You can access that array from everywhere of your app class with this code

YourDelegateClass * delegate =[[UIApplication shareApplication]delegate];
yourclassa.array = delegate.array;
or yourclassb.array = delegate.array;

Note: your must alloc *delegate.array* on your prefer at class or your delegate.

于 2012-11-06T08:23:24.343 に答える
0

view1 set プロパティに NSMutable 配列を作成する

@property(nonatomic,assign) NSMutableArray *array;

viewB に同じ配列を作成し、プロパティを設定して

@property (nonatomic, assign)  NSMutableArray *arrayB;

@合成する

ここで、viewB を呼び出して、viewA の配列の値を次のように viewB に設定します。

ViewControllerB *viewController = [[ViewControllerB alloc] initWithStyle:UITableViewStylePlain];
[viewController arrayB:array];
于 2012-11-06T08:27:26.470 に答える
0

これを達成することができます

@property(nonatomic,assign) NSMutableArray *array;
于 2012-11-06T08:19:53.577 に答える
0

ViewControllerA で NSMutableArray を作成し、割り当て後に ViewControllerB に渡します。

于 2012-11-06T08:12:03.583 に答える