iPhone         Objective-C, UIKit                              bofeng@corp.netease.com                                    ...
Agenda• overview• objective-c• iPhone UIKit
overview
First iPhone App• Hello world!
Things we have• Hello world!
• xcode SDK•       & test with Simulator• test with iPhone/iPod touch ($99)•         appstore•
iPhone VS Android
objective-c
Objective C•••   runtime•   foudation framework: values and collection classes••   category•   protocol
Sample Code   hello world
objective c•   @    •   @interface, @implementation, @class    •   @property, @synthesize    •   @protocol    •   NSString...
Sample Code  class and message
runtime•   id•   class & className•   respondsToSelector•   performSelector•   isKindOfClass (super class included)•   isM...
@selector• SEL•       C++
actionCGRect rect = CGRectMake(20, 20, 100, 30);UIButton* myButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];myBu...
Sampleruntime & selector
Foundation Framework• Value and Collection Classes• User defaults• Archiving• Task, timers, thread• File system, I/0• etc ...
Value and Collection           Classes•   NSString & NSMutableString•   NSArray & NSMutableArray•   NSDictionary & NSMutab...
Sample Code  collection classes
•                   alloc   new•   alloc&dealloc    C++ new&delete•   but with ...
•           alloc   new         copy                                   1.                                          retain ...
Sample Code  “           ”      ... memory management
Getter & Setter  @property & @synthesize
•       new   alloc    copy                                 1.    release•                      retain release
but how to solve this ?- (Person*) getChild {    Person* child = [[Person alloc] init];    [child setName:@”xxxx”];    ......
Autorelease• NSAutoreleasePool* pool ...• [object autorelease]•                     autorelease  NSAutoreleasePool  release
That’s why create pool            first ...int main(int argc, char* argv[]) {    NSAutoreleasePool* pool = [[NSAutoreleaseP...
how to solve this ?- (Person*) getChild {    Person* child = [[Person alloc] init];    [child setName:@”xxxx”];    ....   ...
Autorelease Pool•    •                    NSMutableArray pool    •   autorelease             Array    •             pool  ...
Autorelease Pool StackNSAutoreleasePool* poolFirst ..for (int i=0; i < 10000; i++) {    NSAutoreleasePool* poolSecond ... ...
Autorelease Pool•    •   NSAutoreleasePool    •                 alloc            release    •               Autorelease po...
•   new   alloc    copy                  1.                     release autorelease•                    1     [NSString st...
Category
Sample Code   category
NSString   ClassCluster ...
Sample Codesomething about class cluster ...
Category•••
Protocol•   familiar with C++ virtual class•   familiar with Java’s interface••                          @optional        ...
protocol@protocol TwoMethod  - (void) oneMethod;  - (void) anotherMethod;@end
Class & Protocol@interface MyClass : NSObject <TwoMethod> {}@end@implementation MyClass-(void) oneMethod {    // oneMethod...
Sample Code   protocol
Objective C•••   runtime    id, @selector, respondsToSelector ...•   foudation framework: values and collection classes•• ...
Using Objective-C Now !• without mac• ubuntu:  • sudo apt-get install gnustep gnustep-devel  • bash /usr/share/GNUstep/Mak...
iPhone UIKit
iPhone Application•           &• MVC• Views (design & life cycle)• Navigation based & Tab Bar based application• very impo...
UIApplication•   Every application must have exactly one instance of    UIApplication (or a subclass of UIApplication). Wh...
UIApplicationMain• delegateClassName • Specify nil if you load the delegate object    from your application’s main nib file...
UIControl• UILabel• UIButton• UITableView• UINavigatorBar• ...
- design time
Demobutton click
Without IB ?create a button and bind event by code
Views•   View                            view    superview                   subviews•       iphone app             window...
View’s life cycle & hook        function•   initWithNibName:bundle•   viewDidLoad•   viewWillAppear•   viewWillDisappear• ...
Navigation Controller
UINavigationController•   manages the currently displayed screens using the    navigation stack•   at the bottom of this s...
DemoUINavigationController
TabBar Controller
UITabBarController• implements a specialized view controller that  manages a radio-style selection interface• When the use...
DemoUITabBarController
Combine•              UI    • TabBarController Based +      NavigationController
DemoCombine UITabBarController &   UINavigationController
TableView• display a list of data • Single column, multiple rows • Vertical scrolling• Powerful and ubiquitous in iPhone  ...
Display data in Table View•    •    Table views display a list of data, so use an array    •    [myTableView setList:myLis...
DemoUITableView <UITableViewDataSource>
Selection
DemoUITableView <UITableViewDelegate>
UITabBar + UINavigation + UITableView !NavigationController                                 TableViewController           ...
DemoUITabBar + UINavigation + UITableView
• Propery Lists, NSUserDefaults• SQLite• Core Data
SandBox• Why keep applications separate? • Security • Privacy • Cleanup after deleting an app
Sample: /Users/xxx/library/Application Support/iPhone Simulator/4.0/Applications
Property Lists•       Convenient way to store a small amount of data    •    Arrays, dictionaries, strings, numbers, dates...
DemoSave data with NSUserDefaults
SQLite•   Complete SQL database in an ordinary file•   Simple, compact, fast, reliable•   No server•   Great for embedded d...
SQLite Obj-C Wrapper•   http://code.google.com/p/flycode/source/browse/    trunk/fmdb•   A query maybe like this:    •   [d...
Using Web Service•   Two Common ways:•   XML    •   libxml2        •   Tree-based: easy to parse, entire tree in memory   ...
NSURLConnection•   NSMutableURLRequest•   - connectionWithRequest: delegate•   delegate method:    •   – connection:didRec...
Demouse json-framework and NSURLConnection with hi-api
iPhone Application•              &•   MVC•   Views (design & life cycle)•   Navigation based & Tab Bar based application  ...
•       Objective-C•       The iPhone Developer’s Cookbook•       Programming in Objective-C 2.0••           Stanford iPho...
Q &AThe End
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Beginning to iPhone development
Upcoming SlideShare
Loading in...5
×

Beginning to iPhone development

1,726

Published on

Slide I made for beginning to iPhone development. Most of them borrowed from ppt of the iPhone development course in Stanford University.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,726
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
65
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Beginning to iPhone development

  1. 1. iPhone Objective-C, UIKit bofeng@corp.netease.com @vonbo
  2. 2. Agenda• overview• objective-c• iPhone UIKit
  3. 3. overview
  4. 4. First iPhone App• Hello world!
  5. 5. Things we have• Hello world!
  6. 6. • xcode SDK• & test with Simulator• test with iPhone/iPod touch ($99)• appstore•
  7. 7. iPhone VS Android
  8. 8. objective-c
  9. 9. Objective C••• runtime• foudation framework: values and collection classes•• category• protocol
  10. 10. Sample Code hello world
  11. 11. objective c• @ • @interface, @implementation, @class • @property, @synthesize • @protocol • NSString* str = @”hello world”• [receiver message]• C C++
  12. 12. Sample Code class and message
  13. 13. runtime• id• class & className• respondsToSelector• performSelector• isKindOfClass (super class included)• isMemberOfClass• @selector
  14. 14. @selector• SEL• C++
  15. 15. actionCGRect rect = CGRectMake(20, 20, 100, 30);UIButton* myButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];myButton.frame = rect;[myButton setTitle:@"my button"forState:UIControlStateNormal];[myButton addTarget:self action:@selector(btnClick:)forControlEvents : UIControlEventTouchUpInside];[self.view addSubview:myButton];
  16. 16. Sampleruntime & selector
  17. 17. Foundation Framework• Value and Collection Classes• User defaults• Archiving• Task, timers, thread• File system, I/0• etc ...
  18. 18. Value and Collection Classes• NSString & NSMutableString• NSArray & NSMutableArray• NSDictionary & NSMutableDictionary• NSSet & NSMutableSet• NSNumber [a obj-c object wrapper for basic C type] • (NSNumber* num = [NSNumber numberWithInt:3])
  19. 19. Sample Code collection classes
  20. 20. • alloc new• alloc&dealloc C++ new&delete• but with ...
  21. 21. • alloc new copy 1. retain release• 0 Obj-C dealloc dealloc dealloc
  22. 22. Sample Code “ ” ... memory management
  23. 23. Getter & Setter @property & @synthesize
  24. 24. • new alloc copy 1. release• retain release
  25. 25. but how to solve this ?- (Person*) getChild { Person* child = [[Person alloc] init]; [child setName:@”xxxx”]; .... // will return ... [child release] or not ? return child;}
  26. 26. Autorelease• NSAutoreleasePool* pool ...• [object autorelease]• autorelease NSAutoreleasePool release
  27. 27. That’s why create pool first ...int main(int argc, char* argv[]) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; // put your own code here ... [pool release];}
  28. 28. how to solve this ?- (Person*) getChild { Person* child = [[Person alloc] init]; [child setName:@”xxxx”]; .... // will return ... [child release] or not ? return [child autorelease];}
  29. 29. Autorelease Pool• • NSMutableArray pool • autorelease Array • pool Array release• autorelease pool autorelease pool pool
  30. 30. Autorelease Pool StackNSAutoreleasePool* poolFirst ..for (int i=0; i < 10000; i++) { NSAutoreleasePool* poolSecond ... // have some autorelease object // [object autorelease] [poolSecond release];}// other code[poolFirst release];
  31. 31. Autorelease Pool• • NSAutoreleasePool • alloc release • Autorelease pool retain autorelease [pool retain] or [pool autorelease] • AutoreleasePool “ ” • pool pool iphone release autorelease
  32. 32. • new alloc copy 1. release autorelease• 1 [NSString stringWithString:@”objc”]• retain release
  33. 33. Category
  34. 34. Sample Code category
  35. 35. NSString ClassCluster ...
  36. 36. Sample Codesomething about class cluster ...
  37. 37. Category•••
  38. 38. Protocol• familiar with C++ virtual class• familiar with Java’s interface•• @optional @required warning required
  39. 39. protocol@protocol TwoMethod - (void) oneMethod; - (void) anotherMethod;@end
  40. 40. Class & Protocol@interface MyClass : NSObject <TwoMethod> {}@end@implementation MyClass-(void) oneMethod { // oneMethod’s implementation}- (void) anotherMethod { // anotherMethod’s implementation}@end
  41. 41. Sample Code protocol
  42. 42. Objective C••• runtime id, @selector, respondsToSelector ...• foudation framework: values and collection classes•• category class cluster “ ”• protocol
  43. 43. Using Objective-C Now !• without mac• ubuntu: • sudo apt-get install gnustep gnustep-devel • bash /usr/share/GNUstep/Makefiles/ GNUstep.sh • GNUmakefile• http://forum.ubuntu.org.cn/viewtopic.php? t=190168
  44. 44. iPhone UIKit
  45. 45. iPhone Application• &• MVC• Views (design & life cycle)• Navigation based & Tab Bar based application• very important TableView•• Web service
  46. 46. UIApplication• Every application must have exactly one instance of UIApplication (or a subclass of UIApplication). When an application is launched, the UIApplicationMain function is called; among its other tasks, this function create a singleton UIApplication object.• The application object is typically assigned a delegate, an object that the application informs of significant runtime events—for example, application launch, low-memory warnings, and application termination—giving it an opportunity to respond appropriately.
  47. 47. UIApplicationMain• delegateClassName • Specify nil if you load the delegate object from your application’s main nib file.• from Info.plist get main nib file• from main nib file get the application’s delegate
  48. 48. UIControl• UILabel• UIButton• UITableView• UINavigatorBar• ...
  49. 49. - design time
  50. 50. Demobutton click
  51. 51. Without IB ?create a button and bind event by code
  52. 52. Views• View view superview subviews• iphone app window Views window window view (top level)• view • - (void)addSubview:(UIView *)view; • - (void)removeFromSuperview;• Superviews retain their subviews• UIView CGRect CGPoint CGSize
  53. 53. View’s life cycle & hook function• initWithNibName:bundle• viewDidLoad• viewWillAppear• viewWillDisappear• ...maybe viewDidUnload• hook function • shouldAutorotateToInterfaceOrientation • didReceiveMemoryWarning
  54. 54. Navigation Controller
  55. 55. UINavigationController• manages the currently displayed screens using the navigation stack• at the bottom of this stack is the root view controller• at the top of the stack is the view controller currently being displayed• method: • pushViewController:animated: • popViewControllerAnimated:
  56. 56. DemoUINavigationController
  57. 57. TabBar Controller
  58. 58. UITabBarController• implements a specialized view controller that manages a radio-style selection interface• When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views• init with an array (has many view controllers)
  59. 59. DemoUITabBarController
  60. 60. Combine• UI • TabBarController Based + NavigationController
  61. 61. DemoCombine UITabBarController & UINavigationController
  62. 62. TableView• display a list of data • Single column, multiple rows • Vertical scrolling• Powerful and ubiquitous in iPhone applications
  63. 63. Display data in Table View• • Table views display a list of data, so use an array • [myTableView setList:myListOfStuff]; • • All data is loaded upfront • All data stays in memory• • Another object provides data to the table view • Not all at once • Just as it’s needed for display • Like a delegate, but purely data-oriented
  64. 64. DemoUITableView <UITableViewDataSource>
  65. 65. Selection
  66. 66. DemoUITableView <UITableViewDelegate>
  67. 67. UITabBar + UINavigation + UITableView !NavigationController TableViewController TabBarController
  68. 68. DemoUITabBar + UINavigation + UITableView
  69. 69. • Propery Lists, NSUserDefaults• SQLite• Core Data
  70. 70. SandBox• Why keep applications separate? • Security • Privacy • Cleanup after deleting an app
  71. 71. Sample: /Users/xxx/library/Application Support/iPhone Simulator/4.0/Applications
  72. 72. Property Lists• Convenient way to store a small amount of data • Arrays, dictionaries, strings, numbers, dates, raw data • Human-readable XML or binary format• NSUserDefaults class uses property lists under the hood• When Not to Use Property Lists • More than a few hundred KB of data • Custom object types • Multiple writers (e.g. not ACID)
  73. 73. DemoSave data with NSUserDefaults
  74. 74. SQLite• Complete SQL database in an ordinary file• Simple, compact, fast, reliable• No server• Great for embedded devices • Included on the iPhone platform• When Not to Use SQLite • Multi-gigabyte databases • High concurrency (multiple writers) • Client-server applications
  75. 75. SQLite Obj-C Wrapper• http://code.google.com/p/flycode/source/browse/ trunk/fmdb• A query maybe like this: • [dbconn executeQuery:@"select * from call"]
  76. 76. Using Web Service• Two Common ways:• XML • libxml2 • Tree-based: easy to parse, entire tree in memory • Event-driven: less memory, more complex to manage state • NSXMLParser • Event-driven API: simpler but less powerful than libxml2• JSON • Open source json-framework wrapper for Objective-C • http://code.google.com/p/json-framework/
  77. 77. NSURLConnection• NSMutableURLRequest• - connectionWithRequest: delegate• delegate method: • – connection:didReceiveResponse: • – connection:didReceiveData: • – connection:didFailWithError: • – connectionDidFinishLoading:
  78. 78. Demouse json-framework and NSURLConnection with hi-api
  79. 79. iPhone Application• &• MVC• Views (design & life cycle)• Navigation based & Tab Bar based application & TableView• (sandbox, property list, sqlite)• Web service
  80. 80. • Objective-C• The iPhone Developer’s Cookbook• Programming in Objective-C 2.0•• Stanford iPhone dev course • iTunes iTune Store cs193p mac windows
  81. 81. Q &AThe End
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×