variable out of scope between view controllers
I am working on a navigation based iphone application. I have some information I want to pass on when I push a new view controller on the stack, say myObject. So:
aViewController *vc = [[aViewController alloc] initwith....];
vc.object = myObject //myObject is defined in the current view controller
[self.navigationController pushViewController:vc]; //vc.object has correct value here
[vc release]
The object variable in aViewController is set to retain the new object. What happens when I get into the code of the new view controller, I see that this object is set to "out of scope" and I can't get to it,开发者_如何学运维 although I have checked that vc.object is valid when the view controller is being pushed.
What does the out of scope error mean, and what is the best way to fix this? Thanks!
I had a similar problem.
In my case the NSMutableArray was declared as a private variable within the class.
When I wrapped the variable with a public property of the same name AND added "self." before the variable name in didSelectRowAtIndexPath then my error went away.
Ok, really embarassed to announce that I got the problem all wrong. It seems like the object has been out of scope in the first place. the myObject is actually [myObjectList objectForIndex:index] where myObjectList is NSMutableArray *myObjectList. This is not a synthesized property. Object is a class derived from NSObject ( @interface Object : NSObject)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for(Object* p in myObjectList)
{
NSLog(@"%@", p.name); //this returns a garbled message
}
MyObjectDetailViewController * vc = [[MyObjectDetailViewController alloc] initWithStyle:UITableViewStylePlain];
vc.object = [myObjectList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:vc animated:YES]; //when i hover over "vc" in debugger, it shows that vc.object is defined(i see my name variables) but when i hover over myObjectList in the previous line, the objects say "out of scope"
[vc autorelease];
}
To cover all ground, here is where the array is instantiated. This function is a callback function from sqlite's sqlite3_exec.
int callback (void* context, int count, char** values, char** columns)
{
NSMutableArray *list = (NSMutableArray*)context;
Object *obj = [[Object alloc] initWithName:[NSString stringWithUTF8String:values[kName]]];
[list addObject:obj];
[obj release];
NSLog(@"After release");
for(Object *p in list)
{
NSLog(@"%@", p.name); //this prints out okay
}
return SQLITE_OK;
}
I hope this makes the problem a little clearer. The fact that myObjectList is out of scope but I seem to be able to retrieve its objects is really weird...
精彩评论