label.text = textField.text;

Discussion in 'iOS Development' started by ipodgreatmaster, Sep 12, 2009.

  1. ipodgreatmaster

    ipodgreatmaster Member

    Joined:
    Feb 20, 2008
    Messages:
    186
    Likes Received:
    0
    Device:
    4G iPod touch
    Hello. This can be a real simple question, but i need to know.

    Im making a game. In the first screen there is a UITextField where the user has to put his name. Then some screens later, there is a Label where the name has to appears. I want to make this without NSUserDeaults.

    Normally i think i have to use

    label.text = textField.text;

    This works perfect if the label is in the same view as the textField, but not if the label is in another view. Can you please help me?

    Thanks
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I don't know your exact layout, but you'll need to do something like this:

    myView.mySubview.textField

    in order to access any property of the textField view, like 'text'.

    *Given that: "textField" is a subview of "mySubview" which is a subview of "myView".

    Do you see how this works?

    Read up on the dot-syntax, which is "new" in Objective-C 2.0
  3. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Why you don't want to use NSUserDefaults?

    And of course it won't work, objects can't just "know" what other objects are subviews of other objects...
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    Perhaps he wants a label to change after every keystroke, in which case reading and writing to a file seems somewhat inefficient. NSUserDefaults would be make more sense being called when the user is finished entering text.

    Though, that is purely speculative, I don't know what he wants to achieve.
  5. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    1. If you want the name to actually be stored persistently, use NSUserDefaults.
    2. If both views "know" each other and can send each other messages, just set the text field's property of the other view (as explained above)
    3. You can store the name in a static variable and access it with C functions, like this:

    NameManager.h:
    Code:
    #import <UIKit/UIKit.h>
    
    NSString *GetPlayerName(void);
    void SetPlayerName(NSString* newName);
    NameManager.m:
    Code:
    static NSString *name = nil;
    
    NSString *GetPlayerName(void)
    {
         return name;
    }
    
    void SetPlayerName(NSString* newName)
    {
         NSString *oldName = name;
         name = [newName copy];
         [oldName release];
    }
    This code isn't really atomic/thread-safe, but you probly won't have to worry about that.

    Hope I helped!
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    YourAppDelegate.h
    [OBJC]@property (nonatomic, retain) NSString *playerName;[/OBJC]

    Anywhere in your app.h
    [OBJC]@class YourAppDelegate[/OBJC]

    Anywhere in yourt app.m
    [OBJC]/*To set*/
    ((YourAppDeelgate *)[UIApplication delegate]).playerName = @"Some String";

    /*To get*/
    NSString *someString = ((YourAppDelgate *)[UIApplication delegate]).playerName;[/OBJC]
  7. ipodgreatmaster

    ipodgreatmaster Member

    Joined:
    Feb 20, 2008
    Messages:
    186
    Likes Received:
    0
    Device:
    4G iPod touch
    Okay i have put everything but by some reason it shows a warning that cause an error in the app. It says

    UIApplication may not respond to +delegate

    Can you please help me?

    Thanks
  8. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    Replace "delegate" from Skylar's post with "sharedDelegate". (This should work if I remember correctly.)
  9. ipodgreatmaster

    ipodgreatmaster Member

    Joined:
    Feb 20, 2008
    Messages:
    186
    Likes Received:
    0
    Device:
    4G iPod touch
    Ok. I will try it in an hour
  10. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Well, I'm not going to give you everything. You might just have to look at the docs every once in a while.

    [UIAplication delegate] is pseudocode shorthand.

Share This Page