[英]iPhone SDK: How do I pass an array of values from a ViewController onto other ViewController?
I tried creating a method like below in SecondViewController
: 我尝试在
SecondViewController
创建如下所示的方法:
-(void)setValue:(NSMutableArray*)array
{
//NSMutableArray *newArray = [[NSMutableArray alloc] init];
newArray = [array retain];
}
and passing the value from FirstViewController
using an object of SecondViewController
named second: 并使用名为Second的
SecondViewController
对象传递FirstViewController
的值:
[second setValue:existingArray];
existingArray
is an NSMutableArray. existingArray
是一个NSMutableArray。
What could be wrong? 有什么事吗
In the code you have set the array in the " second
" object of a SecondViewController
. 在代码中,已将数组设置在
SecondViewController
的“ second
”对象中。
So you need to use that object for displaying the SecondeViewController
, other wise how can it won't show the array. 因此,您需要使用该对象来显示
SecondeViewController
,否则它将无法显示数组。
Check the following code. 请检查以下代码。
// In the FirstViewController
- (void)buttonClicked:(id)sender{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle: nil];
[second setValue:existingArray];
[self.navigationController pushViewController:second animated:YES];
[second release];
}
Here in the code I am assigning the data to the array in the "second" object and I am using that object to display the controller. 在代码中,我将数据分配给“第二个”对象中的数组,并使用该对象显示控制器。
Regards, 问候,
Satya 萨蒂亚
You could use a property instead of the setter you have there(which looks bad). 您可以使用属性而不是那里的设置器(看起来很糟)。 Question is do you really need to do a copy on the array or a retain would be enough?
问题是您真的需要在阵列上进行复制还是保留就足够了?
Create a NSMutableArray *myArray as a local variable in the SecondViewController. 在SecondViewController中,将NSMutableArray * myArray创建为局部变量。 Add a property @property(nonatomic, retain) NSMutableArray *myArray;
添加属性@property(非原子,保留)NSMutableArray * myArray; in the interface.
在界面中。
Synthesize it and to set it just call [mySecondViewController setMyArray:newArray]; 合成并设置它只需调用[mySecondViewController setMyArray:newArray];
If you have instantiated correctly the SecondViewController and if the array that you want to send is not nil then it should work. 如果您已正确实例化SecondViewController,并且要发送的数组不是nil,则它应该可以工作。
if you do it like this: 如果您这样做:
-(void)setValue:(NSMutableArray*)array
{
NSMutableArray *newArray = [[NSMutableArray alloc] init];
newArray = [array mutableCopy];
}
newArray would be a variable declared inside the SetValue method, after the program exits the setValue method, the newArray variable will not be accessible anymore. newArray将是在SetValue方法内声明的变量,程序退出setValue方法后,将不再可访问newArray变量。 Also you leak memory because newArray is never released.
另外,由于从不释放newArray,因此还会泄漏内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.