iPhone 程式設計入門(7) Cocoa Touch 摘要

(1) Cocoa Toch Frameworks

 

Foundation

  • Object wrappers
    •     Strings, collections
    •     Various system services (file I/O and network)
  •  

     

  • Subset of Foundation on Mac

UIKit

  • Event handling
  • Graphics and windowing
  • Text and web management

(2) Foundation

  • Strings
    NSString *myString = @"This is a string";

    NSString *myString = [NSString stringWithFormat:@"%d techTalks", 24];

    NSString *pathString = @"Library/Caches/Images/dataCache.txt";
    NSArrary *pathComments = [pathString pathComments];
    NSString *fileName = [pathString lastPathComponent];

    NSString *fileContents = [NSString stringWithContentOfFile:pathString];
  • Arrays
    • Mutable
      NSMutableArray *arr = [NSMutableArray array];
      [arr addObject:@"randomString"];
    • Immutable
      NSArray *arr = [NSArray arrayWithObjects:@"foo", @"bar", @"moof", nil];
      NSString *complexString = [arr componentsJoinedByString:@"--"];
  •  

     

  • Dictionary
    • Mutable
      NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:5];
      [md setObject:@"randomValue" forKey:@"randomKey"];
    • Immutable
      NSDictionary *dict = [NSDictionary dictionaryWithObjectAndKeys:
                                          @"valueOne", @"keyOne", @"valueTwo", @"keyTwo", nil];
  •  

     

  • File I/O
    • NSFileManager class
      NSFileManager *fm = [NSFileManager defaultManager];
      if ([fm isWritableFileAtPath: path]) {
          [fm createFileAtPath: [path stringByAppendingPathComponent:@"foo.bar"]
                                                                                         contents:@"foo"
                                                                                          attributes:nil ];
      }
    • tmp directory -- Cleared, not backed up
    • Library/Caches -- Saved, not backed up
    • Everything else -- Backed up
  •  

     

  • NSLog
    • NSLog(@"Simple debug statement");
    • NSLog(@"There are %d North American Tech Talk", numberOfNATechTalks);
    • NSLog(@"Got an array: %@", myArray);
    • NSLog(@"Got an dictionary: %@", myDictionary);
  •  

     

(3) Foundation Bundles -- a special kind of folder

  • Executable
  • User Interface (nib) files
  • Metadata
  • Resources
  • Access via NSBundle class
  • Easy accesses to static resources
    • Images
    • Sounds
    • Default data
  •  

     

  • Read-only
    • Codesigning requires static bundle
    • Write new data to sandbox
  •  

     

  • Accessing resources
    // Get an image from the main app bundle
    NSBundle *appBundle = [NSBundle mainBoundle];

    NSString *imagePath = [appBundle pathForResource:@"logo" ofType:@"png"];
    UIImage *logoImg = [[UIImage alloc] initWithContentsOfFile: imagePath];

    // Load a default SQLite DB if no user data exists
    NSString *defaultDB = [appBoundle pathForResource:@"db" ofType:@"sql"];
    if (sqlite3_open([defaultDB UTF8String], &database) == SQLITE_OK) {
           // fetch data from default db
    }

(4) Foundation -- Networking

  • Standard sockets programming 
    • CFNetwork, CFSocket
    • POSIX
  •  

     

  • NSURLConnection
    • Asynchronous request loading 
  •  

     

  • NSXML
    • Basic parser
  •  

     

  • Bonjour Networking
    • Zero-configuration discovery
  •  

     

  • Bonjour discovery

(5) UIKit -- The UIKit Library,

 

  • Buttons and Controls
  • Image Views
  • Web Views (put Safari inside your application)

(6) Application Design Patterns

  • Model-View-Controller
  • Foundation is for Model
  • UIKit is for View
  • iPhone SDK has ViewController class for Controller
  • MVC is the umbrella design pattern 

(7) Application Design Patterns - Target-Action

 

 

 

(8) Application Design Patterns - Notification

 

  • Notification Center
  • Observer / Controller
  • Notifications -- Registering an observer
    MPMoviePlayerController *mpc;
    NSURL *url = [NSURL urlWithString:@"http://foo.com/movie.mov"];
    mpc = [[MPMoviePlayerController alloc] initWithContentURL:url];

    // Register for device notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                                 selector:@selector(movieLoaded:)
                                       name: MPMoviePlayerContentPreloadDidFinishNotification
                                       object:mpc];

    // Get preload notification and react accordingly
    - (void) movLoaded: (NSNotification *) notif {
            [(MPMoviePlayerController *)[notif object] play];
    }

(9) Application Design Patterns - Delegation

  • The Application Life Cycle
    • Application launch
      • UIApplication
        • Main nib
        • Main window
      •  

         

      • Application delegate
        • UI config
      •  

         

    •  

       

  •  

     

  • The Application Delegate 
    • Your first (and last) chance to act
      • Custom class written by you
      • Conforms to UIApplicationDelegate protocol
      • Hooked up in interface Builder
    •  

       

    • Two key methods
      • - (void) applicationDidFinishLaunching:(UIApplication*) app
        • Load initial UI, prepare saved state
      •  

         

      • - (void) applicationWillTerminate:(UIApplication*) app
        • Save state and data   
      •  

         

    •  

       

  •  

     

  • Delegate Design Pattern 
    • Customization  without subclassing
      • Fine-tune default behavior
      • Respond to user actions
      • Separate controller from view
    •  

       

  •  

     

(10) iPhone Design Patterns

  • Model-View-Controller
    • Target-Action
      • what reacts, and how
      • dynamic object and selector
    •  

       

    • Notification
      • information while app is running
      • Asynchronous delivery
    •  

       

    • Delegation
      • Direct control over view behavior
      • Synchronous delivery and response
    •  

       

  •  

     

(11) iPhone Development

  • Get to know Cocoa Touch
    • Understand the Objective-C runtime
    • Explore Fundation and UIKit
    • Embrace design pattern
    • Look at example nib file
    • Use NSLog
  •  

     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值