how it affects one array to another - iphone

-(IBAction)Done :(id) sender {
NSMutableArray *addinfo = [[NSMutableArray alloc]init];
AddinfoCarteV *addinfoCarteV =[[AddinfoCarteV alloc]initWithNibName:#"AddinfoCarteV" bundle:nil];
[addinfo addObject:textfieldToAdd.text];
NSLog(#"%#",addinfo);
addinfoCarteV.informationMan = addinfo ;
textfieldToAdd.hidden= NO ;
}

Please try to ask questions clearly..I think you need to allocate and initialize your addinfoCarteV.informationMan array.or if you want to copy your addinfo array to informationMan array try this code
addinfoCarteV.informationMan = [[NSMutableArray alloc] init];
[addinfoCarteV.informationMan addObjectsFromArray:addinfo];

Related

How to pass array form one view to another view in iphone

I am new to i phone programming.I have store some data in array that i want to pass that array data form one view to another view.What i am tried but its not working.Can any body tell me what is mistake in that.
#import"firstviewcontroller.h"
#property(nonatomic,retain)NSMutableArray *tapCollection;
#property(nonatomic,retain)NSMutableArray *imageCollection;
#import"firstviewcontroller.m"
#synthesize imageCollection,tapCollection;
-(void)viewdidload
{
self.tapCollection = [[NSMutableArray alloc] init];
self.imageCollection = [[NSMutableArray alloc] init];
}
- (void)insertNewObject:(id)sender
{
secondviewcontroller*r= [[secondviewcontrolleralloc] initWithNibName:#"secondviewcontroller" bundle:nil];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
r.imageCollection1 =imageCollection;
r.tapCollection1 =tapCollection;
[self.navigationController pushViewController:r animated:YES];
}
Acctually what data i store in array is images and button tag values.Here in console its displaying Images and button tag value i have store in array
2013-03-19 21:54:03.374 Taukyy[290:1c103] (
0,
"<UIImage: 0x9cd59e0>",
1,
"<UIImage: 0x9cd6220>",
2,
"<UIImage: 0x9cd6b70>"
)
import"secondviewcontroller.h"
#property(nonatomic,retain)NSMutableArray *tapCollection1;
#property(nonatomic,retain)NSMutableArray *imageCollection1;
import"secondviewcontroller.m"
#synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
imageCollection1 = [[NSMutableArray alloc] init];
tapCollection1 = [[NSMutableArray alloc] init];
NSLog(#"%#",tapCollection1);
NSLog(#"%#",imageCollection1);
}
But here values are not displaying.Its showing as below
2013-03-19 21:29:16.379 Taukyy[594:1c103] (
)
2013-03-19 21:29:16.380 Taukyy[594:1c103] (
)
2013-03-19 21:29:16.381 Taukyy[594:1c103] (
)
Please can any body tell me what is mistake in this code
Thanks
Aslam
Remove the viewDidLoad's array allocations from secondviewcontroller.m
You tapCollection1 & imageCollection1 are retain properties.So It should retain the assigned objects.
Your secondviewcontroller.h should look like,
#property(nonatomic,retain)NSMutableArray *tapCollection1;
#property(nonatomic,retain)NSMutableArray *imageCollection1;
and
- (void)viewDidLoad
{
//remove the allocation codes
}
You can log it in firstviewcontroller.m like,
- (void)insertNewObject:(id)sender
{
secondviewcontroller*r= [[secondviewcontrolleralloc] initWithNibName:#"secondviewcontroller" bundle:nil];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
r.imageCollection1 =imageCollection;
r.tapCollection1 =tapCollection;
NSLog(#"%#",r.imageCollection1);
NSLog(#"%#",r.tapCollection1);
[self.navigationController pushViewController:r animated:YES];
}
Don't allocate/initialize your arrays again. You set them when you're preparing your segue.
#synthesize tapCollection1,imageCollection1;
- (void)viewDidLoad
{
//imageCollection1 = [[NSMutableArray alloc] init];
//tapCollection1 = [[NSMutableArray alloc] init];
NSLog(#"%#",tapCollection1);
NSLog(#"%#",imageCollection1);
}
Why are you allocating previously allocated array.
just remove
imageCollection1 = [[NSMutableArray alloc] init];
tapCollection1 = [[NSMutableArray alloc] init];
Do not init the mutableArrays in viewDidLoad of SecondViewController ,
instead do it in the init method of SecondViewController
-(id)initWithNibName //this method
{
//create object
self.imageCollection1 = [NSMutableArray alloc] init];
self.tapCollection1 = [NSMutableArray alloc] init];
}
You will get the right values now..

Initialize 2 dim array NSMutableArray

For C I would init an array like this:
NSInteger x[3][10]; That works.
Below I have a one dim array that works. Would like to move all of this to a 2 dim array, How do I init it? So in other words take the code below and make it work with 2 dimensions.
NSMutableArray *SRData;
SRData = [[NSMutableArray alloc] init];
NSMutableDictionary *SRRow;
SRRow = [[NSMutableDictionary alloc] init];
[SRRow setObject:#"Read" forKey:#"Descr"];
[SRRow setObject:#"Read2.png" forKey:#"Img"];
[SRRow setObject:#"Read the codes" forKey:#"Det"];
[SRData addObject:SRRow] ;
[SRRow release];
In Objective-C, you just have to have an array of arrays to get the second dimension. To my knowledge, there is no shorthand, so you're stuck doing something like the following:
NSMutableArray *firstDimension = [[NSMutableArray alloc] init];
for (int i = 0; i < rows; i++)
{
NSMutableArray *secondDimension = [[NSMutableArray alloc] init];
[firstDimension addObject:secondDimension];
}
So all you would do is add your other objects (in your case, the NSMutableDictionarys) to the secondDimension array. Usage would be like:
[[firstDimension objectAtIndex:0] objectAtIndex:0];
Edit
Full code example:
NSMutableArray *SRData = [[NSMutableArray alloc] init]; //first dimension
NSMutableArray *SRRow = [[NSMutableArray alloc] init]; //second dimension
[SRData addObject:SRRow]; //add row to data
[SRRow release];
NSMutableDictionary *SRField = [[NSMutableDictionary alloc] init]; //an element of the second dimension
[SRField setObject:#"Read" forKey:#"Descr"];
//Set the rest of your objects
[SRRow addObject:SRField]; //Add field to second dimension
[SRField release];
Now, to get at that "field" you would use code such as the following:
[[SRData objectAtIndex:0] objectAtIndex:0]; //Get the first element in the first array (the second dimension)

Getting out a undefined number of ViewController from an Array

In my app, I load UIViewController into an array from a .plist, and then, I need to get those VC's out. The problem is, since the number of VC's is not always the same, then I don't know how many I'm getting out each time. So I'm looking for a better engeneered solution - better iteration, rather than hard coding.
For example:
NSMutableArray *views = [[NSMutableArray alloc] init];
for (int i = [currentList count]; i > 0; i--) {
UIViewController *view = [[UIViewController alloc] init];
view.title = [NSString stringWithFormat:#"dgdg - %i", i];
[views addObject:view];
}
So there is my array of VC's, and now:
myIvar = [[CustomSubClass alloc] initWithViewControllers:**help** nil];
I tried:
myIvar = [[CustomSubClass alloc] initWithViewControllers:[views copy], nil];
and:
myIvar = [[CustomSubClass alloc] initWithViewControllers:[NSIndexSet..., nil];
I tried:
myIvar = [[CustomSubClass alloc] initWithViewControllers:[views objectAtIndex:0]... nil];
but none of it worked. Thanks in advance.
The syntax you want is like this:
[[CustomSubClass alloc] initWithViewControllers:[views objectAtIndex:0], [views objectAtIndex:1], [views objectAtIndex:2], nil];
Basically just repeat have all your arguments separated by commas, then always have the final argument be nil.
views is an array and you can pass this directly as a parameter like so:
 myIvar = [[CustomSubClass alloc] initWithViewControllers:views];

Removing Object in NSMutable Array using NSMutable Dictionary

i want to remove object in Mutable array using mutable dictionary, How?
My code :
Employees *Emp7 = [[Employees alloc] init];
Emp7.Number = 7;
Emp7.Name = #"Safa'";
[EmployeesArray addObject:Emp1];
[Dictionary setValue:Emp1 forKey:Emp1.Name];
in the Deleting button (IBAction)
Employees *objEmp2 = [Dic1 objectForKey:objEmp1.Name ];
[Dic1 removeObjectForKey:#"Safa'"];
Please be a little more clear with your question, is this what you are looking for ?
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3", nil];
[array removeObject:[dict objectForKey:someKey]];
where 'values' and 'keys' are both arrays and 'somekey' is an object in the 'keys' array.
Please let me know if this helps.
TESTED CODE : 100 % WORKS
NSMutableArray *EmployeesArray = [[NSMutableArray alloc]init];
NSMutableArray *EmployeesArrayToDelete = [[NSMutableArray alloc]init];
Employees *Emp7 = [[Employees alloc] init];
Emp7.number = #"7"; Emp7.Name = #"Safa'";
[EmployeesArray addObject:Emp7];
Employees *Emp3 = [[Employees alloc] init];
Emp3.number = #"7"; Emp3.Name = #"AppleVijay#facebook.com";
[EmployeesArray addObject:Emp3];
Employees *EmployeToDelete = [[Employees alloc] init];
EmployeToDelete.number = #"7";
EmployeToDelete.Name = #"Safa'";
NSLog(#"before delete : %#",EmployeesArray);
for (Employees *eachEmployee in EmployeesArray) {
if ([eachEmployee.Name isEqual:EmployeToDelete.Name] && [eachEmployee.number isEqual: EmployeToDelete.number]) {
[EmployeesArrayToDelete addObject:eachEmployee];
}
}
if ([EmployeesArrayToDelete count]>0) {
[EmployeesArray removeObjectsInArray:EmployeesArrayToDelete];
}
NSLog(#" After delete Employeesarray : %#",EmployeesArray);

problem while getting arrays from one class to another class

i am have 4 arrays in myclass.m
i need to get those arrays into myclassviewcontroller.m
for that i write code in myclassviewcontroller.m like this.
- (void)resultarrays :(NSMutableArray *)Agentids loanofficerid:(NSMutableArray *)Loanofficerid agentname:(NSMutableArray *)agentname agentemail:(NSMutableArray *)agentemail agentphone:(NSMutableArray *)Agentphone {
agentids = [[NSMutableArray alloc] initWithObjects:Agentids,nil];
loanofficerid = [[NSMutableArray alloc] initWithObjects:Loanofficerid,nil];
agentnames = [[NSMutableArray alloc] initWithObjects:agentname,nil];
agentemails = [[NSMutableArray alloc] initWithObjects:agentemail,nil];
agentphone = [[NSMutableArray alloc] initWithObjects:Agentphone,nil];
NSLog(#"123 %#",agentids);
NSLog(#"123 %#",loanofficerid);
NSLog(#"123 %#",agentnames);
NSLog(#"123 %#",agentphone);
}
in myclass.m i write this
myclassviewcontroller *LOVobj = [[myclassviewcontroller alloc]init];
[LOVobj resultarrays:resultData_agent loanofficerid:array1 agentname:array2 agentemail:array3 agentphone:array4];
then it displays all the objects that i print in console.
After this, In the button click i print these arrays then it prints null.
even i assign setter and getter methods to it.
i did n't what's the problem can any one please help me.
Thank u in advance.
First of all, change the code to this:
- (void)resultarrays :(NSArray *)Agentids loanofficerid:(NSArray *)Loanofficerid agentname:(NSArray *)agentname agentemail:(NSArray *)agentemail agentphone:(NSArray *)Agentphone {
agentids = [[NSMutableArray alloc] initWithArray: Agentids];
loanofficerid = [[NSMutableArray alloc] initWithArray: Loanofficerid];
agentnames = [[NSMutableArray alloc] initWithArray: agentname];
agentemails = [[NSMutableArray alloc] initWithArray: agentemail];
agentphone = [[NSMutableArray alloc] initWithArray: Agentphone];
NSLog(#"123 %#",agentids);
NSLog(#"123 %#",loanofficerid);
NSLog(#"123 %#",agentnames);
NSLog(#"123 %#",agentphone);
}
Don't pass mutable array if you don't want it to change.
First of all, you're creating arrays containing references to arrays, not arrays of the objects in the parameter arrays. And since you're storing the references of the parameter arrays, if the contents of the parameter arrays changes, so will all the references.
You probably instead want something like this for each array:
agentids = [NSMutableArray arrayWithArray: Agentids];
(and [agentids retain] since arrayWithArray returns an auto-released object).