Reputation: 6450
Here is my situation. I have ViewController class A with a button that goes to TableViewController class B by doing the following.
- (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];
}
}
I want to be able to have an array that can be accessed and edited by both class A and class B. How can I achieve this?
Upvotes: 0
Views: 110
Reputation: 1917
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 = ...
}
Upvotes: 0
Reputation: 2097
Create NSMutable array in view1 set property
@property(nonatomic,assign) NSMutableArray *array;
Create same array in viewB and set property and
@property (nonatomic, assign) NSMutableArray *arrayB;
@Synthesize that
Now at the time call viewB set value of array of viewA to viewB like this
ViewControllerB *viewController = [[ViewControllerB alloc] initWithStyle:UITableViewStylePlain];
[viewController arrayB:array];
Upvotes: 0
Reputation: 46
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.
Upvotes: 0
Reputation: 6427
This can be achieve Creating NSMutableArray And And Make it in Property assign in one of the class
@property(nonatomic,assign) NSMutableArray *array;
Upvotes: 0
Reputation: 107121
Create a Array variable in Class B like:
@interface classB:NSObject
{
NSMutableArray *arrayFromA;
}
@property (nonatomic, assign) NSMutableArray *arrayFromA;
Synthesize the variable.
And in this method pass the array like:
- (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];
}
}
Upvotes: 1
Reputation: 31339
Create a NSMutableArray in ViewControllerA and pass it to ViewControllerB after your allocation.
Upvotes: 0