开发者

Objective C - Setting a variable in viewDidLoad and using it elsewhere

开发者 https://www.devze.com 2023-03-14 01:59 出处:网络
another objective c question for you. Probably a simple one too... In my viewDidLoad method I am setting a variable. I need to access this within - (void)tableView:(UITableView *)tableView willDispla

another objective c question for you. Probably a simple one too...

In my viewDidLoad method I am setting a variable. I need to access this within - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath however I am receivig the following error and I don't really know a way to sort it!

-[CFString intValue]: message sent to deallocated instance 0x592dcb0

- (void)viewDidLoad
{   
    //code to get json is here
    totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];
    NSLog(@"totalincat %i",[totalInCategory intValue]);

    [super viewDidLoad];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)celllforRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"totalincat %i",[totalInCategory intValue]);

    if((indexPath.row == [myArray count]-1)&&(inde开发者_JAVA百科xPath.row < [totalInCategory intValue]))
    {
        //code to get more rows
        [self.tableView reloadData];
    }
}

I have NSString *totalInCategory; set up and synthesized.

totalInCategory is fine within viewDidLoad (as far as I'm aware that is), but nowhere else... any ideas would be really handy!


Retain the data...

totalInCategory = [[parsedJson objectForKey:@"TotalInCategory"] retain];

objectForKey returns an autoreleased object which you need to retain if you wish to use it. Don't forget to add a [totalInCategory release]; in the dealloc.


put:

[totalInCategory retain] just below totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];

That will retain the variable, which is currently autoreleased by the objectForKey method.


Without knowing the type of parsedJson I can't be sure, but it seems likely that you're missing a retain.

 totalInCategory = [[parsedJson objectForKey:@"TotalInCategory"] retain];

And in the dealloc method for the class add

 [totalInCategory release];

You should probably also take a glance at the iOS Memory Management Programming Guide if you haven't already.


Use totalInCategory with self. if you have specified retain in the property declaration .

self.totalInCategory = [parsedJson objectForKey:@"TotalInCategory"];
0

精彩评论

暂无评论...
验证码 换一张
取 消