0

I have a button that saves cell's data upon ButtonClick

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [self.checkbox setFrame:checkboxRect];  
    [self.checkbox setImage:[UIImage imageNamed:@"[email protected]"]forState:UIControlStateNormal];
    [self.checkbox setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateSelected];
    [self.checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.accessoryView = self.checkbox;
    array = [NSMutableArray array];
}
return self;
}
-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
if(sender.selected){
    [array addObject:cell]; 
}
}

and i use that array in my other class, but it keeps giving me empty arrays;

this is my other class, it gives me a log of Zero.

-(void)textMessage{
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Not Implmeneted Yet" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

[alert show];
[alert release];*/
AddressBookCell *names = [[AddressBookCell alloc]init];
NSLog(@"%d",[[names array]count]);
   
}

![1] http://min.us/m2GMsoRap

I need a way to store data upon button click and transfer it to my other classes.

EDIT## full code

#import "AddressBookCell.h"

@implementation AddressBookCell
@synthesize checkbox;
@synthesize array;
@synthesize addressController;



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [self.checkbox setFrame:checkboxRect];  
    [self.checkbox setImage:[UIImage imageNamed:@"[email protected]"]forState:UIControlStateNormal];
    [self.checkbox setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateSelected];
    [self.checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.accessoryView = self.checkbox;

    array = [[NSMutableArray alloc]init];
    
}
return self;

}

-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
NSLog(@"%d'",cell.tag);

if(sender.selected){
    [array addObject:cell];  
}else{
    if([array containsObject:cell]){
        [array removeObject:cell];
    }
    NSLog(@"%d", [array count]);
}

}

and now my other class

-(void)setUpContacts{

NSDictionary *alphabet = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:0],@"A",[NSNumber numberWithInt:1],@"B",[NSNumber numberWithInt:2],@"C",[NSNumber numberWithInt:3],@"D",[NSNumber numberWithInt:4],@"E",[NSNumber numberWithInt:5],@"F",[NSNumber numberWithInt:6],@"G",[NSNumber numberWithInt:7],@"H",[NSNumber numberWithInt:8],@"I",[NSNumber numberWithInt:9],@"J",[NSNumber numberWithInt:10],@"K",[NSNumber numberWithInt:11],@"L",[NSNumber numberWithInt:12],@"M",[NSNumber numberWithInt:13],@"N",[NSNumber numberWithInt:14],@"O",[NSNumber numberWithInt:15],@"P",[NSNumber numberWithInt:16],@"Q",[NSNumber numberWithInt:17],@"R",[NSNumber numberWithInt:18],@"S",[NSNumber numberWithInt:19],@"T",[NSNumber numberWithInt:20],@"U",[NSNumber numberWithInt:21],@"V",[NSNumber numberWithInt:22],@"W",[NSNumber numberWithInt:23],@"X",[NSNumber numberWithInt:24],@"Y",[NSNumber numberWithInt:25],@"Z", nil];


NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for(int i = 0; i<=27; i++){
    [tempArray addObject:[NSNull null]];
}
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
    firstLetter = [info objectForKey:@"lastName"];
    int index = 27;
    if([firstLetter length] > 0){
        firstLetter =[NSString stringWithFormat:@"%C",[firstLetter characterAtIndex:0]];
        
        firstLetter= [firstLetter capitalizedString];

        if([alphabet objectForKey:firstLetter]){
            
            NSNumber *t = [alphabet valueForKey:firstLetter];
            index = [t intValue] + 1;
        }
    }
    if([tempArray objectAtIndex:index] == [NSNull null]){
        [tempArray insertObject:[NSMutableArray array] atIndex:index];
    }
    [[tempArray objectAtIndex:index] addObject:info];
}
[alphabet release];    
NSArray *alphabet2 = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
NSMutableArray *tempArray2 = [[NSMutableArray alloc]init];
NSMutableArray *titleTemp = [[NSMutableArray alloc]init];
int c = 0;
for(int i = 0; i<=27; i++){
    if([tempArray objectAtIndex:i] != [NSNull null]){
        if(i == 0){
            [titleTemp insertObject:@"Suggested" atIndex:c];
        }else if(i == 27){
            [titleTemp insertObject:@"Others" atIndex:c];
        }else{
            int loc = i -1;
            
            [titleTemp insertObject:[alphabet2 objectAtIndex:loc] atIndex:c];
        }
        [tempArray2 insertObject:[tempArray objectAtIndex:i] atIndex:c];
        c++;
    }
}
[alphabet2 release];
[tempArray release];
letterArray = tempArray2;
titlePointer = titleTemp;

}

-(void)textMessage{
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Not Implmeneted Yet" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

[alert show];
[alert release];*/
AddressBookCell *names = [[AddressBookCell alloc]init];
savedArray = [NSArray arrayWithArray:[names array]];
NSLog(@"%@",savedArray);
   

}

- (void)viewDidLoad{

[super viewDidLoad];
[self setUpContacts];
  
indexPaths = [[NSMutableDictionary alloc]init];   

//suggestedPeople = [[NSArray alloc]initWithObjects:@"User1",@"User2",@"User3",@"User4", nil];

//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds        style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = @"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Text"style:UIBarButtonItemStylePlain target:self action:@selector(textMessage)];

//testing section

}

1
  • Try using a singleton, instead. See my answer here: stackoverflow.com/a/10093449/542400 but make it an NSArray instead of a dictionary. Commented Apr 28, 2012 at 17:33

3 Answers 3

4

Of course it's empty, you alloc/init'd a new instance of AdressBookCell in your other class, so it doesn't have anything in its array. You need a property in your other class that points to the instance of your first class where you fill your array, and then you can use [pointerToFirstClass array] to get at that array.

Sign up to request clarification or add additional context in comments.

6 Comments

i am fairly new to iso developing, what do you mean by " You need a property in your other class that points to the instance of your first class where you fill your array"
i have created property, @property (nonatomic, retain) AddressBookViewController *addressController;
You should add a property in your other class, lets call it pointerToFirstClass, and then in IB you need to add an object (one of the cubes) if you don't already have one, and change its class to that of your second class. If you right click on that cube you will see the property, pointerToFirstClass -- you need to connect that to the cube that represents your first class.
i dont use interface builder, this is all just coding
Why are you not using IB, it makes things a lot simpler. Did you create your button in code also? I don't know if I can tell you what to do without seeing your whole code then.
|
0

It looks like you are using manual memory management. If not, you need to specify.

In the case that you are, when you create the array with the line [NSMutableArray array], you are creating an autoreleased object that will be released before your other class tries to access it.

You'll need to retain it at the point you create it, and release it in your dealloc. If you change [NSMutableArray array] to [[NSMutableArray array] retain] and add an [array release] line to your dealloc the array won't disappear on you until you release the owning object.

2 Comments

yeah, i noticed that, so i changed it to [NSMutableArray alloc]init];
You'll need rdelmar's answer as well.
0

you haven't included enough info to tell what is going on, but I can guess...

a log of zero could mean an empty array, or more likely a nil array... try this:

AddressBookCell *names = [[AddressBookCell alloc]init];
NSLog(@"%@",[names array]);

1 Comment

yeah, i have noticed the array is nil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.