This tutorial demonstrates how to create a basic login screen for iOS, similar to QQ's login interface.
Application Entry Point
The main.m file serves as the entry point for every iOS application. It initializes the UIApplication instance and sets up the app delegate.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
// UIApplication is a singleton representing the application itself
// The third parameter specifies the application class (nil defaults to UIApplication)
// The fourth parameter specifies the delegate class
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
AppDelegate Implementation
The AppDelegate handles application lifecycle events and manages the main window.
AppDelegate.h
#import <UIKit/UIKit.h>
@class LoginViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) LoginViewController *loginViewController;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "LoginViewController.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[_loginViewController release];
[super dealloc];
}
#pragma mark - Application Lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"Application did finish launching");
// Initialize the main window with screen bounds
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Initialize the login view controller from nib
self.loginViewController = [[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] autorelease];
self.window.rootViewController = self.loginViewController;
// Make window visible - only keyWindow can receive user interactions
[self.window makeKeyAndVisible];
return YES;
}
// Called when app loses focus (cannot interact with user)
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"Application will resign active");
}
// Called when app enters background (e.g., home button pressed)
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Application did enter background");
}
// Called when app returns to foreground
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"Application will enter foreground");
}
// Called when app gains focus and becomes interactive
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"Application did become active");
}
// Called when app is about to terminate
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(@"Application will terminate");
}
@end
Login View Controller
The ViewController manages the login UI and handles user input.
LoginViewController.h
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController
// IBOutlet properties connected to text fields in Interface Builder
@property(nonatomic, assign) IBOutlet UITextField *usernameField;
@property(nonatomic, assign) IBOutlet UITextField *passwordField;
// IBAction method for login button - "void" aliased for IB recognition
- (IBAction)loginButtonTapped;
@end
LoginViewController.m
#import "LoginViewController.h"
@implementation LoginViewController
- (void)loginButtonTapped
{
NSString *username = _usernameField.text;
NSString *password = _passwordField.text;
NSLog(@"Username=%@, Password=%@", username, password);
// Dismiss keyboard by ending editing on the view
// This works when first responder exists within current view hierarchy
[self.view endEditing:YES];
// Alternative: resign first responder on specific fields
// [_usernameField resignFirstResponder];
// [_passwordField resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
Key Concepts
Memory Management: In manual reference counting (MRC), properties with strong need explicit release in dealloc. UI elements connected via IBOutlet typically use assign and don't require manual memory management.
First Responder: The view that currently accepts keyboard input. Calling resignFirstResponder on a text field dismisses the keyboard. Using [self.view endEditing:YES] dismisses the keyboard regardless of wich field has focus.
Window Hierarchy: Only the key window can receive user interacctions. The makeKeyAndVisible method makes the window visible and sets it as the key window simultaneously.