Update window

Discussion in 'Mac Programming' started by SRossi, May 30, 2009.

  1. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #1
    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
     
  2. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #2
    Heres my code that is in the awkaeFromNib function:

    Code:
    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
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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.
     
  4. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #4
    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
     
  5. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #5
    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.
     
  6. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #6
    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
     
  7. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #7
    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.
     
  8. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #8
    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
     
  9. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #9
    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?
     
  10. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #10
    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
     
  11. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #11
    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.
     
  12. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #12
    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
     

Share This Page