Welcome to iOS200
iOS Platform Development - Intermediate
Day 1
Hello World!
I am Alex Rodriguez
I work at Integrity as a Project and Engineering Lead
I've been doing mobile development for 5 years...
iOS
Android
HTML5,CSS3,JS
I Love Rock Climbing
NSLog(@"I am %@", Person.info);
What is your name?
What do you do?
What kind of programming experience do you have?
What is your favorite app?
Course Description
- This course will guide you in developing applications for the iOS platform and further knowledge of the Objective-C language and existing constructs that are used in typical application development.
Prerequisites...
-
iOS100 or equivalent
- Fundamental Understanding of...
- The iOS Platform and SDK
- Xcode IDE & iOS Simulator
- Object Oriented Programming (OOP)
- Model-View-Controller (MVC)
- Developing in Objective-C
- Creating and using classes in Objective-C
- Using Interface Builder to connect view objects to classes
Course Topics
- Protocols & Delegates
- Storyboards & Segues
- Navigation Controls
- Table Views
- Gestures
- Photo Library
- Activities
- Data Management
How does one create a new class in their program?
What is the difference between an NSArray and NSMutableArray?
What is the root class of all objects in the foundation framework?
What is the difference between an instance and class method?
Examples?
What are two different ways we can iterate though an array?
Practice
- Create a mutable array that contains the following:
- Output all of the values of the array...
- Manually
- Traditional For Loop
- Fast Enumeration
Protocols & Delegates
-
Protocol: An interface
- A promise to implement a list of methods
-
Delegate: An object that implements a protocol
Delegates
- For objects with complex lives (like a text field), Apple uses the delegation pattern.
- "I am your delegate, when something interesting happens in your life, send a message to me"
- "OK, I am done editing"
- "OK, I am being edited"
- (void) textFieldDidEndEditing:(UITextField *)textField;
- (void) textFieldDidBeginEditing:(UITextField *)textField;
Note:
Many of the UIKit objects use the delegate pattern.
Protocols & Delegates Cont.
- Delegates can also receive queries ...
- "I am about to end editing and hide the keyboard. OK?"
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
How to be a Delegate
- Know what you have to do
Say "I can do that"
- Declare delegations in class file
Have a plan
- Implement delegate protocol methods
Let em know
- Assign an object as the delegate
Example 1:
"UITextFieldDelegates"
Download the code:
Custom Protocols & Delegates
- Most protocols that we want used are already defined for us in the Foundation Library.
- Sometimes we want to define our own protocols...
// In the interface file (.h)
@protocol ProtocolName<ProtocolToExtend>
@optional
// Your protocols optional methods to implement
@required
// You protocols required methods to implement
@end
- You mark your class as implementing the protocol...
// Publicly
@interface ClassName : SuperClass <ProtocolName>
// OR Privately
@interface ClassName () <ProtocolName>
Example Protocol Declaration
// StreetLegal.h
#import <Foundation/Foundation.h>
@protocol StreetLegal <NSObject>
- (void)signalStop;
- (void)signalLeftTurn;
- (void)signalRightTurn;
@end
Example Protocol Implementation
// Bicycle.h
#import <Foundation/Foundation.h>
#import "StreetLegal.h"
@interface Bicycle : NSObject <StreetLegal>
- (void)startPedaling;
- (void)removeFrontWheel;
- (void)lockToStructure:(id)theStructure;
@end
Example Protocol Implementation
// Bicycle.m
#import "Bicycle.h"
@implementation Bicycle
- (void)signalStop {
NSLog(@"Bending left arm downwards");
}
- (void)signalLeftTurn {
NSLog(@"Extending left arm outwards");
}
- (void)signalRightTurn {
NSLog(@"Bending left arm upwards");
}
- (void)startPedaling {
NSLog(@"Here we go!");
}
- (void)removeFrontWheel {
NSLog(@"Front wheel is off."
"Should probably replace that before pedaling...");
}
- (void)lockToStructure:(id)theStructure {
NSLog(@"Locked to structure. Don't forget the combination!");
}
@end
Example Protocol Implementation
- When referring to an object that implements a protocol, do this:
@property (nonatomic, strong) id <ProtocolName> myVariable;
Exercise 1:
"DexterDelegates"
Download the code:
Challenge
Create a "Libby" class that implements the same methods as the "Alex" class but logs out different messages
- (void)dexterDidWhine:(Dexter *)dexter {
NSLog(@"OK sweetie, time for a walk!");
}
- (void)dexterDidPawBowl:(Dexter *)dexter {
NSLog(@"Aww what's wrong Dexter, would you like a biscuit?");
}
Instructions:
- Create and define an Objective-C Protocol called "DextersDelgate"
- (void)dexterDidWhine:(Dexter *)dexter;
- (void)dexterDidPawBowl:(Dexter *)dexter;
NOTE: You will need to declare:
@class effectively tells the compiler that such a class exists somewhere and, thus, pointers declared to point to instances of said class are perfectly valid.
Instructions:
2. Modify the Dexter class to have a property for a delegate object that can be used to send messages from the Dexter object to the assigned delegate
@property (nonatomic, strong) id <DexterDelegate> delegate;
3. Modify Dexter class method implementations to use:
Instructions:
3. Modify main.c to use an Alex object as the Dexter delegate.
Alex *alex = [[Alex alloc] init];
dexter.delegate = alex;
4. Add a second call to the Dexter object that uses an instance of the Libby class as the delegate.
Output should look like:
2014-03-09 21:09:36.323 DexterDelegates[48642:70b] OK Dexter, let's go outside!
2014-03-09 21:09:36.324 DexterDelegates[48642:70b] Here ya go Dexter, here's a biscuit!
2014-03-09 21:09:36.324 DexterDelegates[48642:70b] OK sweetie, time for a walk!
2014-03-09 21:09:36.325 DexterDelegates[48642:70b] Aww what's wrong Dexter, would you like a biscuit?
BONUS:
In the main.m, use a string variable called "whoIsHome" to assign Alex or Libby as being the Dexter delegate.
HINT: You will need to use:
if ([whoIsHome isEqualToString:@"Alex"]) {
}
To check if the string is equal to "Alex" or "Libby"
Bonus Bonus!
If "whoIsHome" = NULL, add a method called "barksAtStrangers" to the Dexter class and call it...
2014-03-09 21:18:12.665 DexterDelegates[48734:70b] WOOF WOOF!
TableViews
- A control that can be used to represent multiple items as a list
- It also provides scrolling capability when the number of items represented is larger than the space provided.
- It's probably the most used UIKit object in the entire iOS ecosystem.
- There are two types of table views: plain and grouped.
- A plain table view consists of a single list of items.
- A grouped table view can have multiple sections, with customizable section headers and footers.
Implements 2 Protocols
-
UITableViewDataSource: What is your data?
- Number of sections
- Number of rows per section
- Table view cell for a row
-
UITableViewDelegate: Events & Customizations
- Row got tapped
- Variable height for cells
- Custom header/footer support
tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create a cell (based on a prototype)
// Populate cell's subview w/ your data
return cell;
}
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
tableView:cellForRowAtIndexPath
Example 2:
"PrettyIcons"
Download the code: