|
Next: Help with small multi-window app
|
| Author |
Message |
External

Since: Nov 07, 2008 Posts: 3
|
(Msg. 1) Posted: Fri Nov 07, 2008 9:56 am
Post subject: Programmatic Update of Text Fields Archived from groups: comp>sys>mac>programmer>help (more info?)
|
|
|
I recently created a small "toy" app in an attempt to learn some Obj-C
and Cocoa programming. It's a multithreaded setup, where multiple
"readers" check out "books" from a "library" -- i.e. threads must
access limited resources from an array. I've got that working
perfectly in CLI form. I can slam a 20-book Library with 500 book-
hungry readers at a time with no problems -- kind of fun. They all end
up reading all the books.
Now I'm taking a shot at providing a GUI for this ridiculous toy. I
have easily been able to set up the interface and use an NSButton to
launch the reader threads. So far, so good. But now I am running into
a problem that I cannot find an answer to, after much searching and
reading.
The difficulty is that I am attempting to use notification from the
library array to update information in the GUI. The idea is that when
the app controller hears a notification from the library, it should
update its labels (sorry, NSTextFields -- I'm coming from Java).
However I used IB to put in these text fields and I cannot figure out
how to access them programmatically.
The current code looks something like this:
<code>
-(void) handleLibraryNotification:(NSNotification*) note{
booksAvailable = [[library bookReport] count];
booksCheckedOut = [arrayOfBooks count] - booksAvailable;
[booksCheckedOutTextField setIntValue:booksAvailable];
[booksAvailableTextField setIntValue:booksCheckedOut];
}
</code>
This doesn't work at all. I realize that the solution here is probably
simple, but I have not been able to discover it. Any help from the
cognoscenti would be welcome.
(btw, I understand from my reading that lots of people use the key-
value system to solve something like this, but for this experiment I
want to do it with notification. Later, if I get the text fields
updating, I will actually make use of all the data packed into the
notification. ) |
|
| Back to top |
|
 |  |
External

Since: Jul 04, 2005 Posts: 246
|
(Msg. 2) Posted: Fri Nov 07, 2008 11:52 am
Post subject: Re: Programmatic Update of Text Fields [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
mwt <michaeltaft DeleteThis @gmail.com> wrote:
> The difficulty is that I am attempting to use notification from the
> library array to update information in the GUI. The idea is that when
> the app controller hears a notification from the library, it should
> update its labels (sorry, NSTextFields -- I'm coming from Java).
> However I used IB to put in these text fields and I cannot figure out
> how to access them programmatically.
>
> The current code looks something like this:
>
> <code>
> -(void) handleLibraryNotification:(NSNotification*) note{
> booksAvailable = [[library bookReport] count];
> booksCheckedOut = [arrayOfBooks count] - booksAvailable;
>
> [booksCheckedOutTextField setIntValue:booksAvailable];
> [booksAvailableTextField setIntValue:booksCheckedOut];
>
> }
> </code>
(1) booksCheckedOutTextField and booksAvailableTextField need to be the
names of outlets from this object to the text fields in question (i.e.
instance variables whose values are pointers to those very text fields).
Are they?
(2) I hope you are not doing this on a background thread, as modifying
the interface on a background thread is a no-no.
m.
--
matt neuburg, phd = matt DeleteThis @tidbits.com, http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com |
|
| Back to top |
|
 |  |
External

Since: Nov 07, 2008 Posts: 3
|
(Msg. 3) Posted: Fri Nov 07, 2008 12:53 pm
Post subject: Re: Programmatic Update of Text Fields [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Re: Background thread -
Maybe I've got this thing set up incorrectly. Because I built it first
for the command line, I just took a guess about how to do it.
Basically I left the main.m file completely alone. It's exactly how
Xcode generated it:
8< ------------
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}
------------>8
Then I set up a button on the main window and gave that button an
action that launches the class that was formerly the main class in the
command line code. This class has been changed just enough to make it
run that way. That is where the autorelease pool is. That is where
this notification handling code is, and it receives notifications
fine.
But perhaps this is the wrong way to set things up?
Gregory: (I recognize your signature) - I took your advice from the
ObjC group and reworked the Library to only have a single array. The
dueling-array scenario wasn't necessary. Thanks. |
|
| Back to top |
|
 |  |
External

Since: Nov 07, 2008 Posts: 3
|
(Msg. 4) Posted: Fri Nov 07, 2008 1:08 pm
Post subject: Re: Programmatic Update of Text Fields [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
I also just noticed that it does indeed update the text fields *once*
when all the threads are finished.
That would seem to point to the background thread problem both of you
mentioned. |
|
| Back to top |
|
 |  |
External

Since: Jun 06, 2005 Posts: 660
|
(Msg. 5) Posted: Fri Nov 07, 2008 2:47 pm
Post subject: Re: Programmatic Update of Text Fields [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
In article
<fadab7da-a7e6-406f-b263-0e5ae611fbfa RemoveThis @z6g2000pre.googlegroups.com>,
mwt <michaeltaft RemoveThis @gmail.com> wrote:
> I recently created a small "toy" app in an attempt to learn some Obj-C
> and Cocoa programming. It's a multithreaded setup, where multiple
> "readers" check out "books" from a "library" -- i.e. threads must
> access limited resources from an array. I've got that working
> perfectly in CLI form. I can slam a 20-book Library with 500 book-
> hungry readers at a time with no problems -- kind of fun. They all end
> up reading all the books.
>
> Now I'm taking a shot at providing a GUI for this ridiculous toy. I
> have easily been able to set up the interface and use an NSButton to
> launch the reader threads. So far, so good. But now I am running into
> a problem that I cannot find an answer to, after much searching and
> reading.
>
> The difficulty is that I am attempting to use notification from the
> library array to update information in the GUI. The idea is that when
> the app controller hears a notification from the library, it should
> update its labels (sorry, NSTextFields -- I'm coming from Java).
> However I used IB to put in these text fields and I cannot figure out
> how to access them programmatically.
>
> The current code looks something like this:
>
> <code>
> -(void) handleLibraryNotification:(NSNotification*) note{
> booksAvailable = [[library bookReport] count];
> booksCheckedOut = [arrayOfBooks count] - booksAvailable;
>
> [booksCheckedOutTextField setIntValue:booksAvailable];
> [booksAvailableTextField setIntValue:booksCheckedOut];
>
> }
> </code>
>
> This doesn't work at all. I realize that the solution here is probably
> simple, but I have not been able to discover it. Any help from the
> cognoscenti would be welcome.
>
> (btw, I understand from my reading that lots of people use the key-
> value system to solve something like this, but for this experiment I
> want to do it with notification. Later, if I get the text fields
> updating, I will actually make use of all the data packed into the
> notification. )
The first thing to check - the equivalent of asking Mom if her machine
is plugged in when she calls to ask why her computer's not working today
- is to ensure that you've the outlets booksCheckedOutTextField and
booksAvailableTextField are defined in IB *and* in your header file and
that they're connected to the things you think they are in IB.
The second thing that springs to mind is to make sure that you've
correctly registered your observer and are posting the same
notifications for which you're listening. If you hard-coded strings,
make sure there are no typos. Or, better, stop hard-coding strings.
The more insidious problem - and I think the one most likely biting you
- is that for the most part you shouldn't attempt to manipulate the UI
from a thread other than the main thread. Couple that with the fact that
normal notification centers are delivered on the same thread on which
they were sent. If you want your notifications to be delivered on the
main thread (such that the handler method can safely manipulate the UI)
you have to use a distributed notification center. Or you can use one of
the performSelectorOnMainThread: methods to ensure that the actual
processing takes place in the right place no matter where the
notification gets handled.
--
"Harry?" Ron's voice was a mere whisper. "Do you smell something ... burning?"
- Harry Potter and the Odor of the Phoenix |
|
| Back to top |
|
 |  |
| Related Topics: | Newbie: IB bindings versus programmatic bindings - Hello all. I'm new to cocoa programming, and my first project relies heavily on bindings. I've set them up in IB, which seemed like a good idea after reading the "getting started" manuals 'n stuff. However, now that I'm getting deeper into ...
How can I get a text from PEF Viewer - I'm an absolute beginner in Mac world, anyway I'm surprised to don't find any way to transfer what I see in disassembly window of PEF Viewer in a text file. The "Save as" item is disabled, the "Edit" menu is disabled. Apparently I c...
http response text - i'm trying to get just the response content, not the headers... from an HTTP GET ... but CFReadStreamRead() seems to just give me the whole lot... headers and all.....which probably makes sense, but how to I extract just the content? TIA
Creating text image in code - What's the easiest (i.e least amount of code) way to generate an NSImage with black text on a transparent background? It needs to be done in code, as the text will be constantly changing. (The last time I did any 2D graphics programming was in QuickDraw...
Drawing text at an angle within a view? - This is probably a silly question, but when drawing within a common view, drawing various shapes and text, can you draw text at an angle? I don't see an attribute for angle or rotation but it seems to me that in order to have text drawn at an angle,.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|