If you are trying to create a big project, I recommend you to create many classes to handle every single bigger task. Why? If one class contains 400 lines of code it’s ok, but when it grows, scrolling and finding something between 2000 lines isn’t just nice and cause confusion. You may say that comments can help you. Well, not in that case. Comments help you to understand the code below them if you return to it after some time, but not to locate some minor features of project.
I advice you to create many classes – like UIViewController, and name them properly. I like to have the complete menu of the application in one single class operating on 10-20 views. I can create a single animation that after some assignments allows me to switch any two views with transition. But class responsible for entering high-scores, with all the data common only to this class, special set of animations and others just don’t suit to my menu class, and that’s why I decide to put it in the other source file.
Today, I’m going to show you how to create a project, where on the first view user has a login field (UITextField), and once he pressed a button (UIButton), the next class is allocated, and the login he has just entered is displayed on it’s view.
This is called passing data between different classes. Today, I will describe you how to pass the NSString value, but using the same or similar steps you can pass anything else.
Xcode
Let’s start be creating the project and adding there two classes: login and account, both UIViewControllers and two views – again: login and account. In app delegate add the standard lines:
#import "login.h" @class login;
login *viewController = [[login alloc] initWithNibName:@"login" bundle:[NSBundle mainBundle]]; [window addSubview:[viewController view]];
In login’s header add the UITextField visible for interface builder and the action (when user presses the button). Also add the protocol: UITextFieldDelegate. For example:
#import <UIKit/UIKit.h> @interface login : UIViewController <UITextFieldDelegate> { IBOutlet UITextField *nickname; } -(IBAction)submit; @end
Now, double click login.xib and design a view. Drop UIButton, UITextField, customize them in the way you want. Set login.xib class login, and assign connections: view => view, nickname => text field, submit action => button’s touch up inside. That’s almost all, but as we extend the UIViewController with protocol UITextFieldDelegate we would like him to give as a signal for some predefined actions called by UITextField (hiding the keyboard for example), that’s why, you need to click on UITextField, and in text field connections, connect delegate with File’s Owner.
You can close login.xib, this part is done, login.m will be implemented later, let’s now move to account.h. You need to create two instance object and one property:
@interface account : UIViewController { IBOutlet UILabel *loginLabel; NSString *nickname; } @property (nonatomic, retain) NSString *nickname;
The NSString nickname will be in near future loginLabel’s text. Setting the property allows me to access this object from outside. (nonatomic, retain) means, that it will be the same object, not the copy etc, (using different keywords here, you can set read only, copy and more, but on the beginning you will only use nonatomic, retain). Before the property will work, you need to synthesize this property in implementation.
#import "account.h" @implementation account @synthesize nickname;
And now design viewDidLoad method:
- (void)viewDidLoad { loginLabel.text = nickname; [super viewDidLoad]; }
Open account.xib with Interface Builder, set it’s class account, add a label and connect it with loginLabel and the view as usuall. Save and close Interface Builder.
Now let’s go back to login.m, import the account.h and implement the missing method submit.
#import "account.h" @class account;
-(void)submit { account *Acc = [account alloc]; Acc.nickname = nickname.text; [self.view addSubview:Acc.view]; }
First line is creating new (and only one) object of account class. 2nd line is setting the property using text from UITextField, and adds the account’s view as a subview to current view. When you run the project right now it will work but the keyboard cannot be hidden. Because you set the nickname (UITextField), as a delegate to this class (File’s Owner), you can implement this method to dismiss the keyboard after pressig Return Key:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { [theTextField resignFirstResponder]; return YES; }
Above method not only can hide the keyboard (resignFirstResponder and return YES), but as well call the submit method:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { [theTextField resignFirstResponder]; [self submit]; return YES; }
That’s all, you can complete dealloc methods on your own.
Please note:
Please note #1: you could set as property not only a nickname (NSString), but as well loginLabel (UITextField), but you can only modify UITextField once the view is loaded, because before the viewDidLoad method is called, it’s an empty – nil object. In future you may add account’s view and change it alpha to zero, so the view is loaded but is full transparent, but you have full access to any objects like UITextField. Later, simple change the alpha back to 1.
Please note #2: remember to synthesize all properties
Please note #3: you can pass this way almost everything, but the property can look a little bit different. Objects need a nonatomic and retain keyword (or other if needed), but passing int, float, double, boolean as long as they are not objects, they only need a nonatomic keyword:
@property (nonatomic) int magicNumber;
Please note #4: the definition of object should be exacly the same as definition of property, don’t forget IBOutlets, types, stars:
IBOutlet UILabel *myLabel; UILabel *programmaticalLabel; bool myLogicValue; @property (nonatomic, retain) IBOutlet UILabel *myLabel; @property (nonatomic, retain) UILabel *programmaticalLabel; @property (nonatomic) bool myLogicValue;
That’s all. Sample project for you.

Sir,
Very nice tutorial. After a long Search i found this & it works like a charm. Thanks in advance
Mahesh Paymal
Hi, I have had no problem with the simple tutorial here, but am having one spec of trouble when implementing in a much more complicated method: here is the code I currently have:
- (void)triggerAccelerometerBall {
NSLog(@”triggerAccelerometerBall”);
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.delegate = self;
accelerometer.updateInterval = kUpdateInterval;
accelerometerBallView.alpha = 1;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
if (accelerometerBallColorSegment.selectedSegmentIndex == 0) {accelerometerBallColorSenderString = [NSString stringWithString:@"Yellow"];}
if (accelerometerBallColorSegment.selectedSegmentIndex == 1) {accelerometerBallColorSenderString = [NSString stringWithString:@"Orange"];}
if (accelerometerBallColorSegment.selectedSegmentIndex == 2) {accelerometerBallColorSenderString = [NSString stringWithString:@"Red"];}
if (accelerometerBallColorSegment.selectedSegmentIndex == 3) {accelerometerBallColorSenderString = [NSString stringWithString:@"Green"];}
if (accelerometerBallColorSegment.selectedSegmentIndex == 4) {accelerometerBallColorSenderString = [NSString stringWithString:@"Aqua" ];}
if (accelerometerBallColorSegment.selectedSegmentIndex == 5) {accelerometerBallColorSenderString = [NSString stringWithString:@"Black" ];}
AccelerometerBallController *accelerometerController = [[AccelerometerBallController alloc] init];
//problem…
accelerometerController.accelerometerBallColorString = accelerometerBallColorSenderString;
//the problem with these 2 lines don’t call the seperate class the same way: notice, accelerometerBallView is called upon to load the other view with my current code. Changing this causes errors anyway I do it. How do I remedy this?
[(AccelerometerBallController *)accelerometerBallView setAcceleration:acceleration];
[(AccelerometerBallController *)accelerometerBallView draw];
}
the
For someone who is just getting into programming I think that it would be easier just to use #pragma mark to organize code instead of creating new classes that would get confusing to a beginner. Here’s a nice article about #pragma mark: http://tinyurl.com/6jpwab
That is what I use. And if you put a “-” dash in you get a line all the way across.
Thanks for the tutorial.
I implemented your code in my app, I can see the variable set (through NSLog) in the login page but it is null in the account page both in the label and in NSLog.
I must be missing something
Hi Tory, give me some code plz.