Code:
[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc]
initWithTitle:@"Randomize"
style:UIBarButtonItemStylePlain
target:self
action:@selector(randomizePositions)] autorelease]];
In this case, yes, I believe you should autorelease. You called alloc/init on the object, so you need to release it somewhere. The easiest way to do that is to call autorelease, so that it gets released at the end of the current event.
Don't worry about who else retained it, and why - that's their business.
Here's another, also correct, way to do what you're doing:
Code:
UIBarButtonItem *newItem = [[UIBarButtonItem alloc]
initWithTitle:@"Randomize"
style:UIBarButtonItemStylePlain
target:self
action:@selector(randomizePositions)];
[self.navigationItem setRightBarButtonItem: newItem];
[newItem release];
This doc was very helpful to me:
Very simple rules for memory management in Cocoa