irving ios jumpstart meetup - objective-c session 2
Post on 21-Oct-2014
304 views
DESCRIPTION
Session 2 of Objective-C for the Irving iOS Jumpstart meetup. This evening we learned about creating UITableView's and programmatically setting the contents of their UITableViewCell's.TRANSCRIPT
![Page 1: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/1.jpg)
Objective-C ProgrammingIrving iOS Jumpstart
![Page 2: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/2.jpg)
Objective-C Programming
March 11, 2014
![Page 3: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/3.jpg)
Overview of Today
• Quick 5 Realms Overview
• Objective-C Messaging
• Simple Programmatic UITableView Example
• Realistic UITableView Example
![Page 4: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/4.jpg)
5 Realmsfor Learning iOS Development
Download full 5 Realms presentation at: http://www.slideshare.net/irving-ios-jumpstart/5-realms
![Page 5: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/5.jpg)
Why 5 Realms?
To the beginning iOS developer the formalities of classes, frameworks, design patterns, and learning an all new integrated development
environment can be overwhelming.
![Page 6: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/6.jpg)
Why 5 Realms?
Knowing which realm you're having trouble in really helps in finding the solution and getting unstuck in
your learning efforts.
![Page 7: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/7.jpg)
5 Realms
Design Patterns Objective-C
iOS SDK OOP
Xcode IDE
![Page 8: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/8.jpg)
Objective-C Messaging
Objective-C
![Page 9: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/9.jpg)
Defining Classes in Objective-C
• Created via .h file and .m file
• All classes inherit from NSObject or another class
Objective-C
![Page 10: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/10.jpg)
@interface @implementation
• @interface in an objective-c .h file
• @implementation in an objective-c .m file
• serves to separate the interface of a class from its implementation.
Objective-C
![Page 11: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/11.jpg)
@property @synthesize
• Objective-C properties
• @property directive is used to create properties on the class.
• Properties are accessed (read and set) via automagically created setters and getters.
• In Xcode 5, optionally use @synthesize to give instance variable a custom name
Objective-C
![Page 12: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/12.jpg)
Objective-C Methods• Class method
+ (return_type)methodName
• Instance method
- (return_type)methodName
• Distinguished by + or -
Objective-C
![Page 13: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/13.jpg)
Objective-C Methods- (void)addHotels { NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil]; ourHotels = [NSArray arrayWithArray:hotels]; }
- (return_type)methodName
Objective-C
![Page 14: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/14.jpg)
Method With Arguments
• this method has one argument
- (void)listOurHotelsUsingTitle:(NSString *)title { NSLog(@"%@: %@", title, ourHotels); }
Objective-C
![Page 15: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/15.jpg)
Method With Arguments
• including sample method call below
- (void)listOurHotelsUsingTitle:(NSString *)title
{
NSLog(@"%@: %@", title, ourHotels);
}
![self listOurHotelsUsingTitle:@"Favorite Hotels"];
Objective-C
![Page 16: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/16.jpg)
Foundation Framework
• NSString - the Foundation Framework class used to create string objects!
NSString *city = @"Irving";
iOS SDK
![Page 17: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/17.jpg)
Foundation Framework
NSString *myStory = [NSString stringWithFormat:@"The brown fox lives in %@", city];
//The brown fox lives in Irving
iOS SDK
![Page 18: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/18.jpg)
Foundation Framework
• NSString comparison
if ([city isEqualToString:@"Irving"]) {
NSLog(@"The two strings are the same.");
} else {
NSLog(@"The two strings are different.");
} //The two strings are the same.
iOS SDK
![Page 19: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/19.jpg)
Foundation Framework
• NSString comparison
if ([city isEqualToString:myStory]) {
NSLog(@"The two strings are the same.");
} else {
NSLog(@"The two strings are different.");
} //The two strings are different.
iOS SDK
![Page 20: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/20.jpg)
Foundation Framework
• NSArray - the Foundation Framework class used to create array objects!
NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil];
• An array is a set of ordered data items
iOS SDK
![Page 21: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/21.jpg)
Foundation Framework
NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil];
• array index begins at zero, so an array with 3 elements are indexed 0, 1, and 2
NSLog(@"The hotel at index 1 in this array is %@", [hotels objectAtIndex:1]); // The W
iOS SDK
![Page 22: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/22.jpg)
Foundation Framework
• NSArray shortcut (objective-c literals)!
NSArray *hotels = @[@"Aloft", @"The W", @"Hilton"];
• Cleaner and familiar syntax
iOS SDK
![Page 23: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/23.jpg)
Foundation Framework
• NSDictionary - the Foundation Framework class used to store key and value pairs!
NSDictionary *hotels = [NSDictionary dictionaryWithObjectsAndKeys:@"5 star", @"Aloft", @"4 star", @"W", @"5 star", @"Hilton", nil];
iOS SDK
![Page 24: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/24.jpg)
Foundation Framework
- (void)addHotelsWithRatings { NSDictionary *hotels = [NSDictionary dictionaryWithObjectsAndKeys:@"5 star", @"Aloft", @"4 star", @"W", @"5 star", @"Hilton", nil];
!
NSLog(@"%@", hotels); }
iOS SDK
![Page 25: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/25.jpg)
Foundation Framework
• NSDictionary shortcut (objective-c literals)!
NSDictionary *hotels = @{@"Aloft":@"5 star", @"W":@"4 star", @"Hilton":@"5 star"};
• Cleaner syntax
iOS SDK
![Page 26: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/26.jpg)
Foundation Framework
• Mutable objects are changeable after creation.
• Immutable objects cannot change once they are created.
• Therefore additional methods exist on mutable objects
iOS SDK
![Page 27: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/27.jpg)
Mutable ObjectsNSString (immutable)
NSMutableString (mutable)
NSArray
NSMutableArray
NSDictionary
NSMutableDictionary
iOS SDK
![Page 28: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/28.jpg)
Mutable Objects• NSMutableString Example
NSMutableString *iCanChange = [NSMutableString stringWithString:@"Hello..."];
NSLog(@"%@", [iCanChange stringByAppendingString:@"world!"]);
// Hello...world!
iOS SDK
![Page 29: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/29.jpg)
Mutable Objects
Download samples from github
!
https://github.com/jamesderry
iOS SDK
![Page 30: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/30.jpg)
Programmatic UITableView
Crosses several realms of IOS understanding.
!
!
!
Design Patterns Objective-C
iOS SDK OOP
Xcode IDE
![Page 31: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/31.jpg)
Programmatic UITableView
• Xcode
• Storyboard - we'll build the view controller storyboard containing a tableview
• IBOutlet - we'll use to grab a handle to the tableview for manipulation in objective-c code
Xcode IDE
![Page 32: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/32.jpg)
Programmatic UITableView
• Design Patterns
• Model - View - Controller (MVC) pattern
• Delegate pattern
• Datasource pattern
Design Patterns
![Page 33: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/33.jpg)
Programmatic UITableView
• Objective-C features
• Protocols
• Classes (UITableView and UITableViewCell)
• Dot syntax for cell properties
Objective-C
![Page 34: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/34.jpg)
Programmatic UITableView
• iOS SDK Features
• UIViewController (and view controller life cycle)
• UIView
• UITableView and UITableViewCell
iOS SDK
![Page 35: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/35.jpg)
Programmatic UITableView Exercise
Create simple table view app
![Page 36: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/36.jpg)
Programmatic UITableView Exercise
Common Pitfalls
• forgetting to match the prototype cell identifier name to the name used in your code
• forgetting to set the tableview datasource and delegate
![Page 37: Irving iOS Jumpstart Meetup - Objective-C Session 2](https://reader038.vdocuments.us/reader038/viewer/2022102823/544594dbb1af9fdb068b45c0/html5/thumbnails/37.jpg)
Thank You!
• Credits
• Wikipedia
• Programming in Objective-C by Stephen Kochan
(any errors or omissions are probably mine)