iphone - 将 NSMutableArray 传递给另一个 View ,给出空值

标签 iphone ios uiviewcontroller nsmutablearray viewcontroller

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

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);


 }

最佳答案

用下面的方法替换

-(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

关于iphone - 将 NSMutableArray 传递给另一个 View ,给出空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17855069/

相关文章:

iphone - 如何在 UIWebView 中缓存内容以便以后更快地加载?

ios - swift 每种屏幕尺寸的不同 StoryBoard

iphone - 用比 Core Plot 更简单的方法在 iPhone 上绘制图形

iphone - 需要在基于标签栏导航的应用程序中隐藏底部栏

ios - 从外部静态函数在 ViewController 上显示警报

swift - 如何删除 UIViewController 过渡动画?

android - 为 Android 和 Iphone 设计丰富的网站

iphone - 如何使用 ARC 处理 iOS 中的自动释放对象

javascript - touchmove开始时取消touchend事件

ios - xcode 8 swift : unwind segue transfer data is not working