[How-To] Set Value of UITextField/NSTextField, Fire a wait timer, Set another Value.

Discussion in 'iOS Development' started by cocotutch, Jul 20, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Well,

    I have explained what I want to do, here is the code I have, I have it on an NSTextField to practice before I insert it into my iPhone-app.

    [OBJC]#import "Controller.h"

    @implementation Controller

    @synthesize timer, wait;

    - (void) awakeFromNib {
    [window setFrame:[[NSScreen mainScreen] frame] display:YES];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector

    Please Register or Log in to view images

    selector(textSetMethod) userInfo:nil repeats:NO];
    [self payOutText];
    }

    - (void) payOutText {
    wait = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector

    Please Register or Log in to view images

    selector(textSetMethod

    Please Register or Log in to view images

    userInfo:nil repeats:NO] retain];
    }



    - (void) textSetMethod {
    [wait fire];
    NSString *code = @"";
    if ([[text stringValue] isEqualToString:code]) {
    NSString *title = [NSString stringWithString

    Please Register or Log in to view images

    "Ha"];
    [text setStringValue:title];
    }
    [wait invalidate];
    [wait fire];
    NSString *code2 = @"Ha";
    if ([[text stringValue] isEqualToString:code2]) {
    NSString *title2 = [NSString stringWithString

    Please Register or Log in to view images

    "Ha, Ha"];
    [text setStringValue:title2];
    }
    [wait invalidate];
    [wait fire];
    NSString *code3 = @"Ha, Ha";
    if ([[text stringValue] isEqualToString:code3]) {
    NSString *title3 = [NSString stringWithString

    Please Register or Log in to view images

    "Ha, Ha, Ha!"];
    [text setStringValue:title3];
    }
    [wait invalidate];
    [wait fire];
    NSString *code4 = @"Ha, Ha, Ha!";
    if ([[text stringValue] isEqualToString:code4]) {
    NSString *title4 = [NSString stringWithString

    Please Register or Log in to view images

    "I'v locked the lot."];
    [text setStringValue:title4];
    }
    [wait invalidate];
    NSString *code5 = @"I'v locked the lot.";
    if ([[text stringValue] isEqualToString:code5]) {
    NSString *title5 = [NSString stringWithString

    Please Register or Log in to view images

    "It's no use trying,"];
    [text setStringValue:title5];
    }
    [wait invalidate];
    [wait fire];
    NSString *code6 = @"It's no use trying,";
    if ([[text stringValue] isEqualToString:code6]) {
    NSString *title6 = [NSString stringWithString

    Please Register or Log in to view images

    "Just, suck it up."];
    [text setStringValue:title6];
    }
    [wait invalidate];
    [wait fire];
    NSString *code7 = @"Just, suck it up.";
    if ([[text stringValue] isEqualToString:code7]) {
    NSString *title7 = [NSString stringWithString

    Please Register or Log in to view images

    "This computer has been"];
    [text setStringValue:title7];
    }
    [wait invalidate];
    [wait fire];
    NSString *code8 = @"This computer has been";
    if ([[text stringValue] isEqualToString:code8]) {
    NSString *title8 = [NSString stringWithString

    Please Register or Log in to view images

    "Shackled."];
    [text setStringValue:title8];
    }
    [wait invalidate];

    [timer invalidate];
    [timer release];
    timer = nil;
    }

    @end[/OBJC]

    Could I please have assistance?

    I want to set a String Value of an NS/UITextField, make it wait with a 1 sec Timer rather than NSThread Sleeping, then set another value to the same NS/UITextField.

    I am at my wits end, i have been working on it for 3 days.

    cocotutch
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    The title of this thread is very misleading. I don't know anything about AppKit, so I'll just tell you the UIKit methods.

    Implement the UITextFieldDelegate protocol <UITextFieldDelegate> and override the didFinishEditing method on your text field's delegate.

    In that method, create a non repeating timer that fires off after one second. In the selector assigned to that timer, have somethig like textField2.text = textField1.text.

    That's it. Sorry about the lack of code, I typed this on my phone.
  3. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Non repeating timer:
    [OBJC][NSTimer scheduledTimerWithTimeInterval:1 target:self selector

    Please Register or Log in to view images

    selector(someSelector) userInfo:nil repeats:NO];
    [/OBJC]
  4. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Thanks for that Chicken, but as you can see in my code sample on Post #1, I have set it to non-repeat mode. I just want it to be fired in between setting the NSTextField stringValue and then have the app set the next value (i.e. "Ha, Ha" after "Ha".) then fire it again, then change the value, It just stays at "Ha" it doesn't continue on after calling the timer.
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Then do what Skylar suggested with text field delegate methods. If you want I can get the code for that to :/
  6. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Yeah if you wouldn't mind, Skylar seems to think us amateurs can do anything.

    Please Register or Log in to view images

  7. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    It's really easy.

    I'll edit the post in a minute going to get code.

    [OBJC]- (void)textFieldDidEndEditing

    Please Register or Log in to view images

    UITextField *)textField[/OBJC]

    Here is the delegate method Skylar is talking about just override that and do what you want with your timers.
  8. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    First thing you can try is to set the timer to repeat, and when the method is called and the text matches the last string, invalidate the timer.

    Or, you can set it to don't repeat, but don't invalidate it. If the method was called, it was because the timer finished. Since the timer won't repeat, there's no need to invalidate it, just to fire it again.

    I'd take the first approach.

    Anyway, checking the text is not what I would do. Instead, i'd use an int to control what text would be next.
  9. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Im not Editing the Text Field manually. Im setting the StringValue through code, thats why its not working. I sort of want it to go:

    "Ha"
    "Ha, Ha"
    "Ha, Ha, Ha!"

    with a small delay in between setting the values so it seems to draw them to taunt the user. there is no editing by the user, it just shows a Padlock when its done doing what it does.
  10. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    I would use what Pelaez wrote

    Please Register or Log in to view images

Share This Page