微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
      •  

         

    •  

       

  •  

     

  • 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
      • @R_915_4045@ion 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐