将NSMutableArray传递给另一个给出空值的视图

时间:2013-07-25 10:12:04

标签: iphone ios uiviewcontroller nsmutablearray viewcontroller

我是iOS开发的新手,我想将NSMutableArray从一个viewcontroller传递给另一个,但总是给我空值

FirstViewController.h

@interface FirstViewController : UIViewController

@property (nonatomic, retain) NSMutableArray *colorArray;
-(IBAction)btn:(id)sender;

FirstViewController.m

@implementation FirstViewController


-(IBAction)btn:(id)sender
{


  SecondViewController* secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    secondViewController.animalArray = self.colorArray;

    NSLog(@"%@",secondViewController.animalArray); // here its not null

    [self.navigationController pushViewController:secondViewController animated:YES];

}

SecondViewController.h

@interface SecondViewController : UIViewController

@property (nonatomic, retain) NSMutableArray *animalArray;

SecondViewController.m

我只使用了NSLog(@“animalArray:%@”,self.animalArray);在viewDidLoad中检查值但是给我null

有什么我想念的吗?

修改:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"indidLoad%@",self.animalArray);


}

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];

    NSLog(@"inwillAppear%@",self.animalArray);


 }

6 个答案:

答案 0 :(得分:4)

替换为以下方法

-(IBAction)btn:(id)sender{
      SecondViewController* secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
         secondViewController.animalArray=[[NSMutableArray alloc]initWithArray:self.colorArray];
        [self.navigationController pushViewController:secondViewController animated:YES];
    }

:) +1

答案 1 :(得分:0)

如果你从(void)viewWillAppear:(BOOL)动画中调用你的NSLog,你应该看到一些东西。

在您的代码示例中,在initWithNibName之后直接调用viewDidLoad方法:

[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

在你有机会设置你的财产之前。

答案 2 :(得分:0)

    **FirstViewController.h**

@interface FirstViewController : UIViewController
{
    NSMutableArray *SongArray;
}
@property(nonatomic,retain)NSMutableArray *SongArray;

**FirstViewController.m**

    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];    
    secondView.SongArray = self.SongArray;

    [self.navigationController secondView animated:YES];


    **SecondViewController.h**

    @interface SecondViewController : UIViewController
    {
        NSMutableArray *SongArray;
    }
    @property(nonatomic,retain)NSMutableArray *SongArray;

这样做你的价值不会被保留。另请查看Pass NSMutableArray to one view controller to another

答案 3 :(得分:0)

检查viewWillAppear中的值,而不是viewDidLoad

答案 4 :(得分:0)

应该有效

-(IBAction)btn:(id)sender
{


    SecondViewController* secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    NSLog(@"%@",secondViewController.animalArray); // here its not null

    [self.navigationController pushViewController:secondViewController animated:YES];
    secondViewController.animalArray = self.colorArray;

}

有时如果在推送视图之前传递值,它将在第二个视图中给出null值

答案 5 :(得分:-2)

更好的方法是在Appdelegate文件中创建NSMutableArray,如下所示......

@property (nonatomic, retain) NSMutableArray *animalArray;//define this in Appdelegate.h file

在Appdelegate.m文件中合成它,如

@synthesize animalArray;

在prefix.pch文件中创建Appdelegate对象,如下所示。

#define AppDel ((AppDelegate *)[UIApplication sharedApplication].delegate)

并将您的Appdelegate文件导入prefix.pch,如下所示..

#import "AppDelegate.h"

现在你要使用那个数组...就像下面写的那样..

 AppDel.animalArray=//you can do whatever you want with array....

通过执行此操作无需将数组传递给其他视图控制器,您可以在整个项目中使用该全局数组,您可以在任何类中的该数组中插入对象,并且可以在任何类中访问。

让我知道它的工作与否!!!

快乐编码!!!!