
Giovambattista Fazioli
CTO
Saidmade Srl
Hi,
I have a delegate class and it create a UIView.
Can UIView release an evento to delegate class?
For example, my delegate create runtime N instances of UIView. UIView do something - for example animation (like your sample) - and when finish I have to send a message t o delegate for release instances.
This is possible and correct?
And How?
Thx

Jonathan Lehr
President
AboutObjects
Assuming that the delegate assigns a view instance to one of its properties or instance variables, it should override the dealloc method inherited from NSObject to release the view. So for example, if the delegate interface looked like this:
Code:
@interface MyDelegate : NSObject <UIApplicationDelegate>
{
UIView *myView;
}
@end
and the implementation file (MyDelegate.m) contained a method something like this:
Code:
- (void)applicationDidFinishLaunching
{
CGRect rect = [[UIScreen mainScreen] bounds];
// Here we're allocating a view, but not releasing it...
myView = [[UIView alloc] initWithFrame:rect];
// Other code to do stuff with the view
[myView doCoolAnimation];
...
}
then the delegate implementation file should also contain a dealloc method similar to the following:
Code:
- (void)dealloc
{
[myView release];
[super dealloc];
}

Giovambattista Fazioli
CTO
Saidmade Srl
Ok,
so my case is more complex, I think:
I have a delegate -> UIViewController -> UIView with a button. When button is clicked I like to send a message to delegate.
It's possible?
Thx

Jonathan Lehr
President
AboutObjects
Ah, I see. By the way, the word
release has a very precise meaning in Objective-C relating to memory management.
To send a message to the delegate (or an instance of any class for that matter), first implement the target method, i.e., the method you want to have invoked when the button is clicked. For example, you could add this method to your delegate:
Code:
- (void)showMessage
{
NSLog(@"The button got clicked");
}
Then, after you create the button, you can send it this message to configure its target (the object the button sends its message to) and action (the message it sends):
Code:
// Assuming myButton has already been created...
[myButton addTarget:self
action:@selector(showMessage)
forControlEvents:UIControlEventTouchUpInside];
Now when you click the button it will send
showMessage to its target.

Giovambattista Fazioli
CTO
Saidmade Srl
Yes, I known this.
But if the last your code ([myButton addtarget...) is into UIView...
How I can referrer to delegate object?

Jonathan Lehr
President
AboutObjects
I'm not sure what you mean. Are you saying the button configuration code would be in a subclass of UIView?
A general guideline for forum posts is to try to give as complete a picture of the problem as possible. Otherwise the person responding may end up wasting time answering the wrong question, as has apparently happened twice already in this thread.
- Jonathan