技术开发 频道

iOS开发笔记 4、iOS中的Cocoa、设计等

  delegated events

  notification

  图书 iPhone and iPad in Action Chapter6 有详细的说明

  设计模式

  MVC

  The Model View Controller (MVC) pattern separates an application’s data structures (the model) from its user interface (the view),with a middle layer (the controller) providingthe “glue” between the two. The controller takes input from the user (via the view), determines what action needs to be performed, and passes this to the model for processing. The controller can also act the otherway: passing information from the model to the view to update the user interface.

  Delegate

  The Delegate pattern is useful as an alternative to subclassing, allowing an object to define a method but assign responsibility for implementing that method to a different object (referred to as the delegate objector, more commonly, the delegate).

  Delegates need not implement all (or even any) of the delegate methods for the source object. In that case, the source object’s default behavior for the method is often used.

  下例通过委托改变了控件的行为[键盘不显示]

  -(BOOL) textFieldShouldBeginEditing: (UITextField *)textField

  {

  return NO;

  }

  -(void)viewDidLoad {

  CGRectrect = CGRectMake(10,10, 100,44);

  UITextFiled *myTextField=[[UITextFieldalloc] initWithFrame:rect];

  myTextField.delegate = self;

  [self.viewaddSubView:myTextField];

  [myTextField release];

  }

  Target-Action

  下例展现了一个按钮的响应处理

  -(void) buttonTap: (id)sender

  {

  NSLog(@”Button tapped”);

  }

  -(void)viewDidLoad{

  ….

  [myButton addTerget:self action:@selector(buttonTap:) forControlEvents: UIControlEventTouchUpInside];

  …

  }

  Categories

0
相关文章