0

我已经尝试了几种我在这里找到的方法,但没有一个奏效。将这个 NSMutalbeArray 传递到另一个视图控制器的简单方法是什么?

    NSMutableArray *pph = [[NSMutableArray alloc] init];
    [pph addObject:[NSString stringWithFormat:@"%d     %d     %d",diario.Cowid,diario.Lact,diario.Del]];

在下面的同一个文件中

- (IBAction)masInfoPPH;
{
    tipo = @"PPH";
    adiario = pph;
    NSLog(@"\n Array adiario: %@",pph);
    DetDiarioViewController *DetDiarios = [[DetDiarioViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:DetDiarios animated:YES];
}

出于某种原因,pph(NSMutalbeArray)在这里为空,但在那里它确实给了我它应该拥有的对象。adiario 是一个全局数组,或者至少它应该是。帮助!

4

2 回答 2

1

确实没有全局数组。在您的类的接口中为您的类中的 pph 创建一个属性。

@property(nonatomic, strong) NSMutableArray *pph;  



self.pph = [[NSMutableArray alloc] init];
[self.pph addObject:[NSString stringWithFormat:@"%d     %d     %d",diario.Cowid,diario.Lact,diario.Del]]

但是您仍然需要将其放入下一个视图控制器。在它的界面中创建一个类似的属性,然后在推送之前设置它

DetDiarioViewController *detDiarios = [[DetDiarioViewController alloc] initWithNibName:nil bundle:nil];
detDiarios.pph = self.pph;
[self.navigationController pushViewController:detDiarios animated:YES];

顺便说一句 - 在objective-c中,约定是使用小写字母作为实例的第一个字母

于 2013-07-11T23:52:13.327 回答
0

The scope of your pph array is unclear from your description.. But ANYTHING declared inside a single method is LOCAL to that method, unless it is returned BY that method.

You have several options... Declare the array as an instance variable, ie..

@interface YourClass: NSObject {  NSMutableArray *pph; }

or

@implementation YourClass {  NSMutableArray *pph; }

or as a static variable (in your .m file) (which would enable you to access the value from Class (+) methods..

static NSMutableArray *pph = nil;

or most preferably... as a property

@interface YourClass @property (strong) NSMutableArray *pph;

which you can then call upon from any instance method via the automatically synthesized Ivar _pph, or via the property's accessors.. self.pph.

Hope this helps!

于 2013-07-11T23:53:02.500 回答