Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Calling methods

Calling methods
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 2, 2004, 02:21 PM
 
Ok I have run into a small problem. I have searched the web and can find no answer, probably because I am looking to hard. Here is my problem:

How do you call a method of one interface from another interface?

EX: Interface A needs to call method X in Interface B.

I hope someone here has the information I need. Thanks!
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Jan 2, 2004, 03:49 PM
 
Um, what language? (I haven't run across any where interfaces can call methods, so I'm not going to guess, unless you meant "object" instead of "interface")
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 2, 2004, 03:53 PM
 
LOL Sorry about that. It is in Objective-C. I think I am confusing both myself and everyone else. I am at work right now so I cant get code. I will try an example:

You have 2 windows(.m files) in objective-c. When one function in window A completes, then you need to call a function that is in the .m file for window B that will set a TextField in window B.

I think there is a simple way of doing this, but I cannot figure it out... Am I making sense now?
     
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jan 2, 2004, 07:33 PM
 
OK, say the object handling Window A is of class WinAObject. There you have an IBOutlet NSTextField *textField; and you declare and implement a method to alter it, such as
Code:
- (void) setTextField: (NSString *)newVal;
In the object handling Window B, you #import "WinAObject.h" and have a WinAObject *otherWindow that at some point you set to refer to the instance for Window A.

Then you just call
Code:
[otherWindow setTextField: aString];
That probably was a very unclear explanation. But you can probably get a better sense of it by creating a simple sample app.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 2, 2004, 08:20 PM
 
Ahh ok that makes perfect sense I think. Does this sound right?

A.m

#import "B.m"
...
[windowB setText:text];
...

B.m
...
-(void) setText:text //or whatever
{

[textField setStringValue:text];
}
...

I figure I was missing setting up an object for the window. I will try that tonight when i get off work and see what happens. If it works, then you will have solved my problems!! Will that cause another window to appear though?
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jan 2, 2004, 08:39 PM
 
Originally posted by djohnson:
I figure I was missing setting up an object for the window. I will try that tonight when i get off work and see what happens. If it works, then you will have solved my problems!! Will that cause another window to appear though?
Only if you create another window. What you need is a pointer to the address of your existing controller object, which you could set up in IB if they're both in the same nib file.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 2, 2004, 09:07 PM
 
Ok that last part lost me... How do you setup a pointer to the address of my existing controller object? Yes they are in the same nib file.
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jan 4, 2004, 02:43 PM
 
Originally posted by djohnson:
Ok that last part lost me... How do you setup a pointer to the address of my existing controller object? Yes they are in the same nib file.
Create an outlet.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 5, 2004, 09:25 AM
 
Ok I see how creating an outlet will connect them, but what do you connect the outlet to?
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jan 5, 2004, 12:34 PM
 
Originally posted by djohnson:
Ok I see how creating an outlet will connect them, but what do you connect the outlet to?
The other object in your nib. Like this:

     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 5, 2004, 02:20 PM
 
Oh... I learn something new everyday Thanks! I will try that in the morning and see what happens. It does make sense though, connecting one object to another means they can communicate which is what objective-c is all about. Thanks!
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jan 5, 2004, 05:51 PM
 
Originally posted by djohnson:
Ahh ok that makes perfect sense I think. Does this sound right?

A.m

#import "B.m"
...
[windowB setText:text];
...

B.m
...
-(void) setText:text //or whatever
{

[textField setStringValue:text];
}
...

I figure I was missing setting up an object for the window. I will try that tonight when i get off work and see what happens. If it works, then you will have solved my problems!! Will that cause another window to appear though?
Don't import the ".m" file, import the ".h" file.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 7, 2004, 03:32 PM
 
Yes I know... just a mistype was all. Anyways, I have tried this and now am getting that the "caller" class does not know the other class. I do have the ".h" included. Here is the code I have to call the function:

In the .h
IBOutlet prefsWindow *prefConn;
...

In the .m
[prefsConn update];

When I declare the IBOutlet and build, it tells me that prefsWindow is not known. Any ideas?
     
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jan 7, 2004, 03:43 PM
 
have you put
Code:
@class prefsWindow
at the top of the .h file?
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Jan 7, 2004, 03:58 PM
 
Umm... If you are meaning the first .h... of course. I changed the IBOutlet to type id and it is working! Of course I had to change it in the .nib file. Thanks for the help!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:36 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2