这是我的情况。我有一个viewController a类带有一个按钮,该按钮通过执行以下操作。
。- (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类访问和编辑的数组。我该如何实现?
在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];
}
}
在ViewControllera中创建NSMutableArray,然后将其传递给ViewControllerb。
这可以实现创建nsmutablearray,并在属性中进行属性分配
之一@property(nonatomic,assign) NSMutableArray *array;
我提到您要通过两者进行编辑。检查此链接。
一些代码在这里。在您的应用程序代表课中。
@interface YourDelegateClass:UIResponder
{
NSMutableArray *array;
}
@property (nonatomic, assign) NSMutableArray *array;
您可以使用此代码从应用程序类的任何地方访问该数组
YourDelegateClass * delegate =[[UIApplication shareApplication]delegate];
yourclassa.array = delegate.array;
or yourclassb.array = delegate.array;
注意:您必须 Alloc * delegate.array *在您的班级或委托。
创建nsmutable数组在View1 Set1 set属性
@property(nonatomic,assign) NSMutableArray *array;
在ViewB中创建相同的数组并设置属性和
@property (nonatomic, assign) NSMutableArray *arrayB;
@synthesize
现在,当时呼叫查看viewa数组的设置值
ViewControllerB *viewController = [[ViewControllerB alloc] initWithStyle:UITableViewStylePlain];
[viewController arrayB:array];
最简单的方法就是,正如其他人指出的那样,只是通过设置属性将数组传递给B。因此,您始终使用相同的数组。如果A和B同时更改数组,这可能很有用。
@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 = ...
}