PDA

View Full Version : Update window




SRossi
May 30, 2009, 09:46 AM
I'm now creating a word count for my iWrite application.

What I would like to know is how to update a window after the users types a key. Because I can get two labels to tell how many word there is but they never update.

I have them in the awakeFromNib method, is there any other methods can be used that update when the nib is opened again and again or will I need to use a sheet?

Thanks in advance

Stephen



SRossi
May 30, 2009, 10:07 AM
Heres my code that is in the awkaeFromNib function:

NSInteger stringLength = [mString length];

[textField setIntValue:stringLength];

unsigned wc = 0;
NSCharacterSet *lettersAndNumbers = [NSCharacterSet alphanumericCharacterSet];
int index = 0;
while (index < ([mString length]))
{
int newIndex = [mString nextWordFromIndex:index forward:YES];
NSString *word = [[mString string] substringWithRange:NSMakeRange(index, newIndex-index)];

if ([word rangeOfCharacterFromSet:lettersAndNumbers].location != NSNotFound)
{
wc++;
}
index = newIndex;
}

[textFieldWord setIntValue:wc];


Stephen

robbieduncan
May 30, 2009, 10:09 AM
awakeFromNib is only called once when the window loads from it's nib/xib file. Pressing a key with the event routing to a window will not re-load it from it's nib/xib. That'd be insane. You need to monitor for key based events in the text view and update the other views as required.

SRossi
May 30, 2009, 10:15 AM
Would it work if i was to set up two global int variables that kept hold of the character and word count and passed them into the statistics.xib ow would that just keep them the same as well?

Stephen

GorillaPaws
May 30, 2009, 11:00 AM
It is my understanding that you should almost never use global variables, and that if your design seems to need them than you're probably doing something wrong and need to re-think your design.

SRossi
May 30, 2009, 11:09 AM
I wouldnt like to use global variables but I do not know enough cocoa and objective-c to get along without them. Still cant understand how to get this to work :(

Stephen

GorillaPaws
May 30, 2009, 11:34 AM
I wouldnt like to use global variables but I do not know enough cocoa and objective-c to get along without them. Still cant understand how to get this to work :(

Stephen

Let me preface all this with the warning that I don't really know what I'm doing yet, so please filter my ideas with the appropriate degree of skepticism. My instinct is to set up a custom NSObject to keep an eye on your NSTextField that would store those values as iVars. It would be the delegate object of your textField so would receive notifications such as textDidChange:. In that method you could put in the logic to calculate the number of words, store them in your iVars and then update your labels appropriately.

My concern with this approach is that your method is going to be constantly getting called as someone is typing and may cause performance issues, or it may not (I simply don't have enough experience with this stuff to know). A solution that might be possible would be creating a delay mechanism using NSTimer or something like that that would only execute the "meat" of your method after a short delay, but all of this is just me speculating. I know this kind of thing must have been figured out many times, you might want to check out the mailing lists and such to see if you can't find a more sophisticated solution from someone smarter than myself.

The other solution I thought of would be creating a separate thread to handle those updates so that it won't slow down the typing performance. I haven't learned about threading yet (except enough to realize that I'm going to avoid dealing with that mess for as long as I can get away with) so I can't offer much help in that department except to point you to the Apple Docs.

SRossi
May 30, 2009, 11:40 AM
Yeah i understand what you are saying but is there no method such as awakeFromNib or that that when the doucment window is closed it will update when it is opened again. I can log how many characters and that there is onto the console but I just dont understand how to only make the labels assign values when the statistics window is opened.

Thanks though

Stephen

GorillaPaws
May 30, 2009, 11:56 AM
Ah, I see now. What about using WindowDidLoad method from the NSWindowController, getting your values from the accessors in your iVars, and then using them here to update your labels?

SRossi
May 30, 2009, 12:01 PM
I have been stupid up to know saying window I actually have them within a panel sorry. Although where would I call a hidesOnDeactivate method because I know if I call that method then every time the panel is opened it will be as though it is its first time opening if you get what I mean.

Thanks

Stephen

GorillaPaws
May 30, 2009, 12:28 PM
I have been stupid up to know saying window I actually have them within a panel sorry. Although where would I call a hidesOnDeactivate method because I know if I call that method then every time the panel is opened it will be as though it is its first time opening if you get what I mean.

Thanks

Stephen

I don't have any experience with NSPanels, but they can have NSWindowControllers as well. I would give your panel it's own NSWindowController so you can implement these kinds of hooks there.

SRossi
May 30, 2009, 12:52 PM
I have some experience with them may just have to leave this out the application. I understand how to do it just dont know hoe to do it.

Stephen