using uitextfield string as label text in two view controllers

Discussion in 'iOS Programming' started by tfellas, Sep 18, 2014.

  1. tfellas macrumors member

    Joined:
    Sep 15, 2014
    #1
    Hi all,

    I have two view controllers

    1) with a log in box, username and password
    and 2) with a label

    how do i get the label to say Welcome, Username

    replace username with the actual username obviously :p

    Thanks
     
  2. AxoNeuron, Sep 18, 2014
    Last edited: Sep 18, 2014

    AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #2
    In the second view controller, create an NSString property, call it something like self.userName. In the first view controller, use the prepareForSegue method and instantiate an object of the secondary view controller ie.

    SecondaryViewController *viewController = segue.destinationViewController.

    Then in this prepareForSegue method set the viewController.username property equal to userNameTextField.text. Then in the viewDidLoad method of the secondary view controller, set self.userNameLabel.text = self.userName.

    So in the first view controller your prepareForSegue method should look something like this:

    Code:
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
         if([segue.destinationViewController isKindOfClass:[SecondaryViewController class]]) {
              SecondaryViewController *viewController = segue.destinationViewController;
              viewController.userName = self.userNameTextField.text;
         }
    }
    Then in the secondary view controller in the viewDidLoad method all you need to do is set the self.userNameLabel.text equal to the self.userName NSString property.

    To a new developer the prepareForSegue method probably looks a bit intimidating but after learning the language a bit more it will all make perfect sense.
     
  3. tfellas, Sep 19, 2014
    Last edited: Sep 19, 2014

    tfellas thread starter macrumors member

    Joined:
    Sep 15, 2014
    #3
    Hi thanks for the help

    edit #6!

    it works, how do I do Welcome, username?
     
  4. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #4
    You use a token in the NSString. For example:

    Code:
    self.welcomeLabel.text = [NSString stringWithFormat:@"Welcome, %@", userNameString];
     
  5. tfellas thread starter macrumors member

    Joined:
    Sep 15, 2014
  6. tfellas thread starter macrumors member

    Joined:
    Sep 15, 2014
    #6
    is there a way so if the user registers a username eg bob.

    it comes out as Welcome, Bob

    as in change the first letter to a capital letter?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    1. Open the class reference documentation for NSString.
    2. Search for the terms case (as in "upper case") or capital (as in "capitalize").

    An online example of NSString class reference can be found by googling:
    NSString class

    Xcode may also contain NSString documentation (look under Help menu). The docs may be a separate install, initiated from within Xcode, or they may be installed already. You'll have to check.


    As a design choice, capitalizing usernames may be a bad idea. For example, my username chown33 shouldn't be capitalized, and I'd be annoyed if it were.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    I wanted to add that there are dozens of such guides, for every aspect of Cocoa Touch and iOS.

    For example, visit the central iOS Developer Library location:
    https://developer.apple.com/library/ios/navigation/

    and enter programming guide into the search box. There are documents named "X Programming Guide" for values of X such as Text, String, Attributed String, Preferences and Settings, Table View, Networking, Threading, etc.


    Here's another essential reference:
    https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/
    It provides simple summaries of many fundamentals, and also has links to more detailed articles.
     
  9. AxoNeuron, Sep 20, 2014
    Last edited: Sep 20, 2014

    AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #10
    There is a way to do it, but as chown said it is not advisable. For questions like this, the developer documentation is best, and when clarification is needed, so is stackoverflow (though keep in mind some code from stackoverflow is old, meant for Cocoa on OS X, or outright wrong/inefficient, so be careful). As a general rule for myself, I never use code from a place like stack overflow unless I take it apart piece by piece and understand precisely how it works, then I write it myself (never just a straight copy and paste) and write dlwn every detail in my notes to, make sure I am learning from the experience.

    An easy way to pull up the documentation for a search while you are in Xcode is to hit command + shift + 0.

    In any case, to capitalize the first letter of the string, all you have to do is call the NSString method capitalizedString on it. For example:

    Code:
    NSString *userName = (wherever you get the username from...);
    NSString *capitalizedUsername = [userName capitalizedString];
    
    Now, the capitalizedUsername string will have a capitalized first letter.
     
  10. tfellas, Sep 21, 2014
    Last edited: Sep 21, 2014

    tfellas thread starter macrumors member

    Joined:
    Sep 15, 2014

Share This Page