Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tfellas

macrumors member
Original poster
Sep 15, 2014
31
0
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
 
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
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.
 
Last edited:
Hi thanks for the help

edit #6!

it works, how do I do Welcome, username?
 
Last edited:
You use a token in the NSString. For example:

Code:
self.welcomeLabel.text = [NSString stringWithFormat:@"Welcome, %@", userNameString];
 
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?
 
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?

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.
 

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.
 
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?
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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.