I have an object that is used between all my viewControllers so I have stuck it inside the application delegate. (I assume this is the correct place?).
If an action inside a viewController fires that needs to send something to said object I am performing the following:
- (IBAction)sliderMoved:(id) sender{
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[[delegate myObject] setSpeed:(int)slider];
// [delegate release];
}
I am a bit concerned I am not releasing the delegate object anywhere, is this ok? If I remove the commented line [delegate release] it just crashes the application.
You don’t own the
delegateobject that you create in this snippet. You haven’t created it withalloc,new, or acopy. Nor have you retained it. So, it is not your responsibility to release it.As for putting an object in the Application delegate just to be able to access it from other parts of your code – that is poor OOP design IMO.
Edited to add
I suppose I had better give an example
Suppose you have a class
MyClassthat you want to create an object of that you can pass around.You create it in the Application delegate, which it seems you are already doing:
Then you create another view controller – which you would normally do, except that this view controller has a property:
And then you set this property when you create the view controller:
And, if you pass this object to other view controllers as you require.