gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:29:35 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com