MohamMad Salah
MohamMad Salah

Reputation: 981

Set properties from another class in Objective-C

I am having a problem trying to set properties which will be set by another class, here are two properties which will be used to populate a table:

@interface TableViewController : UITableViewController
    @property (nonatomic, retain) NSArray *friendsNames;
    @property (nonatomic, retain) NSArray *friendsIDs;
@end

In this class (inherit from NSObject) I am setting them using this method:

@implementation MyFacebooDelegate

-(void)getFriends:(NSArray *)names {
    NSMutableArray *friendsNames = [[NSMutableArray alloc] init];
    NSMutableArray *friendsIDs = [[NSMutableArray alloc] init];
    for (int i=0; i<[names count]; i++) {
        NSDictionary *friend = [names objectAtIndex:i];
        NSString *name = [friend objectForKey:@"name"];
        [friendsIDs addObject:[friend objectForKey:@"id"]];
        [friendsNames addObject:name];
    }

    if(tableController == nil)
        tableController = [[TableViewController alloc] init];

    [tableController setFriendsIDs:friendsIDs];
    [tableController setFriendsNames:friendsNames];
    NSLog(@"%@", [tableController friendsNames]); 

    [friendsNames release];
    [friendsIDs release];
}

The names all print on the console (NSLog(@"%@", [tableController friendsNames])) but not in the TableViewController class.

If I try to print the content of two arrays in the viewDidLoad of the TableViewController class then it will be null, also the arrays are set before the table appears on the screen.

Upvotes: 1

Views: 2602

Answers (2)

ksachdeva
ksachdeva

Reputation: 101

Assuming that you would use value of friendNames & friendIds from with in UITableViewController (that would implement UITableViewDataSource .. I do not see it in your snippet so mentioning it)

Can you re-write your properties as (assuming you are using ARC):

@property (nonatomic, strong) NSArray *friendsNames;
@property (nonatomic, strong) NSArray *friendsIDs;

and use this syntax:

tableController.friendNames = friendNames;
tableController.friendIDs = frinedIDs

and make sure that you do show the tableController that you are creating here and not some other (it is not clear from your snippet how you are showing/pushing this tableController so mentioning it)

Upvotes: 1

Ivor Prebeg
Ivor Prebeg

Reputation: 978

You are doing it wrong way... you should set MyFaceboo delegate as UITableViewController's tableView's datasource and implement UITableViewDataSourceProtocol

Upvotes: 2

Related Questions