Cocoa question....

Discussion in 'Mac Help/Tips' started by altivec 2003, Jul 10, 2003.

  1. altivec 2003 macrumors regular

    Joined:
    Feb 8, 2003
    Location:
    Texas
    #1
    Well, honestly I don't know what happened to a similar thread I posted yesterday. I searched and searched for it but I couldn't find it anywhere. This is a better section for me to post it under anyway, I guess. My apologies to Arn if my old post is stil present. It certanly may be deleted. Ok, on to the question:

    I have just started programming in objective c after a couple of years in procedural c in mac os 9. I have been following Learning Cocoa by O'Reilly but decided that I wanted to try writing my own type of program. I have learned a lot in this exploit but have one main problem which is probably very easily solved:

    I am writing a text-based rpg having a gui which consists of a text view to display all of the games text, a text field where the player types and a button that runs everything. I have gotten everything to work except some minor issues with the text view. My first problem was that I couldn't figure out how to "insert" text into the text view but I solved it with this code which I figured out:

    - (void)addtexttodisplay:(NSString *)textToAdd
    {
    originaltext = [displayText string];
    totaltext = [NSString stringWithFormat: @"%@%@",originaltext,textToAdd];
    [displayText setString:totaltext];
    }

    displayText is the name of the text view. My only problem with this code is that once enough text is displayed to fill the text view, the new text is hidden and the player is required to scroll down to view it. I find this really annoying and am sure that there must be a better way of doing things. If you have any experience in cocoa, please help me :). Thanks.

    Over all I think that cocoa is a pretty neat language even though it is completely different that the languages that I am used to. I do think that it will cut down a lot on the tediousness which will be awesome. I just wish it were a little more like procedural c.....

    Well anyway, thanks in advance for answering my question and sorry if my old one turns up :rolleyes:
     
  2. altivec 2003 thread starter macrumors regular

    Joined:
    Feb 8, 2003
    Location:
    Texas
    #2
    yikes in that code the :( is a : (. I forgot to turn of smilies....
     
  3. scem0 macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #3
    to get rid of the smilie you can use the
    Code:
     tags.
    
    (code)
    blah blah blah
    blah blah blah
    (/code)
    
    ( == [ && ) ==]
    
    But, sorry, can't help you in any other way. :( .
    
    scem0
     
  4. Fukui macrumors 68000

    Fukui

    Joined:
    Jul 19, 2002
    #4
    Re: Cocoa question....

    Pretty sure you need to tell the NSScrollView (that contains the NSTextView) to scrolll downwards as lines are added to you TextView. I think you'll need to subclass the ScrollView and implement scrollLineDown:(id) by subclassing, and set the setVerticalPageScroll:(float) method...I think. You can also use an NSTimer to cause a delay so the user can have time to read the text while/before its scrolling.

    You'll probably need to get the bounds of the NSTextView and find the height to know where to scroll to...

    The best place to post quesstions is in the Apple/Omnigroup mailing lists, also check cocoa.mamasam.com, they answer alot of common questions.

    Also you should check out AppKido, it makes searching through apples docs much faster and better...

    Good Luck

    P.S You can use procedural C to as much as you want within methods, and linked libs etc, objective-C is/was just C with a bunch of pre-processor directives to make it OO, for example, [self message] is just calling objc_msg(); Think of it as smalltalk implemented in C
     
  5. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    You should probably do something like the following:

    [textView setSelectedRange:selectionRange];

    [inputController scrollToEnd];


    You should also do yourself a favour and change addtexttodisplay to addTextToDisplay.

    The reason should be obvious. :)
     
  6. altivec 2003 thread starter macrumors regular

    Joined:
    Feb 8, 2003
    Location:
    Texas
    #6
    Found my old thread and reported it to the monitors to delete it :(. I guess I was looking in the wrong forum, but usually I am subscribed to them automatically but this one didn't subscribe so I was a little worried. Anyway, Thanks a lot for all the advice (especially Fukui who was kind enough to post his reply in BOTH threads :eek:). This might end up helping a lot. I as also wondering if there was another way of adding text to the display or if my way is really the best. That is hard to imagine, and I am gettting a little worried that after a couple of minutes the program might slow down a little cause it might take awhile to send the increasingly large totaltext string to the textView. Hmpf. Ok I will check the appleKido thing and once again, thanks.
     
  7. Fukui macrumors 68000

    Fukui

    Joined:
    Jul 19, 2002
    #7
    You might try telling the NSTextView to insertText:(id)aString where aString is the text to add to the end of the view, this is the method that is used for adding text from the keyboard etc, but you need to make sure that the insertion point is at the end of the text. Check out the Cocoa Docs, specifically NSText and NSTextView:
    Here at ADC

    Good Luck
     

Share This Page