Re: How call upper layer function
Re: How call upper layer function
- Subject: Re: How call upper layer function
- From: Conrad Shultz <email@hidden>
- Date: Tue, 19 Apr 2011 15:21:01 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
(Sorry, putting back on list after accidental private reply.)
On 4/19/11 1:50 PM, Rodrigo Zanatta Silva wrote:
>> Lol, sorry for making your life hard :P
>>
>> let's write better. (Will be a problem in your computer with this
>> font in e-mail? )
Works just fine, though my responses will be in plain text.
As evidenced by comments below I assume that this is NOT the actual code
you are trying. In general, you want to copy and paste your code
directly; failing to do so can inadvertently introduce bugs (or even fix
them) in what you send to the list.
>> //This is LevelUp.h #import "LeveDown.h"
Typo here.
>> @interface LevelUp : NSObject { } -(void) init;
You are declaring a method implemented by a superclass (NSObject), which
is unnecessary and confusing. Worse, you have changed the return type
from id to void. I don't know how the compiler is going to handle this,
but it's not going to behave as you expect.
>> -(void)wantCallThisFunction @end
>>
>> //This is LevelUp.m @implement LevelUp
I think you meant @implementation.
>> -(void) init { LevelDown *test = [[LevelDown alloc] init]; }
You are throwing away this object and introducing a memory leak in the
process. If you want to save a reference to the LevelDown instance, you
needed to declare an appropriate instance variable in your interface and
set its value in init. Don't forget to release it in dealloc, too.
>>
>> -(void)wantCallThisFunction { ///Do something } @end
>>
>> ////////////////////////////// // levelDown.h @interface LevelDown
>> : UIView { //Some class -(void) init; -(void)keyboardWillShow;
See comments above regarding re-declaring super's methods.
>> } @end
>>
>> //LevelDown.m @implement
Again, @implementation.
>> -(void) init {};
What is this supposed to do? I'm uncertain whether this is going to
cause things to crash and burn since you are overriding the hugely
important init method but changing the return type, but I'm quite
certain this is wrong regardless.
>> -(void)keyboardWillShow { //iOS automatic call this function //I
>> need to call wantCallThisFunction }
What does this _mean_? You need to have an instance of a class in order
to call its instance methods. Are you saying that you have elsewhere
instantiated a LevelUp instance that is disconnected from the LevelDown
instance?
If you really mean that there is a LevelUp instance logically coupled to
the LevelDown instance, you need to retain a reference to the LevelUp
instance in the LevelDown instance, e.g.
@interface LevelDown : NSObject {
LevelUp *relatedUpLevel;
}
- - (void)setRelatedUpLevel:(LevelUp *)upLevel;
@end
After having externally called setRelatedUpLevel (and having managed
memory appropriately in setRelatedUpLevel, an exercise left to the
reader or to @property/@synthesize), you could then call from within
keyboardWillShow:
[relatedUpLevel wantCallThisFunction];
>> Like I say, if I use this line in keyboardWillShow function :
>> [[NSNotificationCenter defaultCenter]
>> postNotificationName:@"thisWork" object:nil];
>>
>> And to catch this call, in function init of LevelUp class , I have
>> to use this line [[NSNotificationCenter defaultCenter]
>> addObserver:self selector:@selector(wantCallThisFunction)
>> name:@"thisWork" object:nil];
>>
>> So the question is: there are another way to do that?
>>
>> Now I am thinking that I have to remake the program and use a
>> third object that create instances of LevelUp and LevelDown in the
>> same object. Am I correct, or there are another way? I'm not
>> remembering if java have problem like that.
If you are not "broadcasting" to multiple possible instances (which is
really what you would use NSNotificationCenter for), the pattern that
you probably want to look at is delegation.
Read more about common Objective-C design patterns at
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html
Quite honestly, though, I would STRONGLY recommend starting with an
introductory book (I like Hillegass for OS X, LaMarche for iOS) and
working through it from the beginning. You are not going to have a fun
or productive time just jumping into the documentation.
- --
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iD8DBQFNrgrNaOlrz5+0JdURAolEAJ0eXxsSlMelLOAKN11uWcJpI91mIwCeJx8a
p+TMA+cw+08t2H7wwmD/20M=
=k7lE
-----END PGP SIGNATURE-----
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden