简体   繁体   English

iPhone SDK:如何将值数组从ViewController传递到其他ViewController?

[英]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.

相关问题 如何将对象从一个ViewController传递到另一个ViewController - How do I pass object from one ViewController to another ViewController iPhone SDK:如何将消息从TableCell内的TextField发送到viewController? - iPhone SDK: How to send a message from a TextField inside a TableCell to the viewController? iPhone SDK:如何从MyAppDelegate访问嵌套在TabBar中的ViewController? - iPhone SDK: How to access a ViewController nested in a TabBar from MyAppDelegate? 如何将另一个viewController从tabviewController内推到navigationController上? - How do I push another viewController onto the navigationController from within a tabviewController? iPhone SDK中的Viewcontroller问题 - Viewcontroller question in iPhone SDK iPhone SDK:从UIImageViewClass到ViewController的调用方法 - iPhone SDK: Call method from UIImageViewClass to ViewController iPhone应用程序中其他ViewController背后的ViewController - ViewController behind other ViewController in iPhone application 如何在Swift 3.0中使用按钮将值从ViewController传递到另一个ViewController - How to pass values from a ViewController to another ViewController using a button with Swift 3.0 iPhone编码:如何在另一个ViewController中设置变量? - iPhone Coding: How can I set a variable in a ViewController from another? 如何将文本字段数据从第三个ViewController传递到第一个ViewController - How to pass textfield data from third viewcontroller to first viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM