代码改变世界

iphone开发笔记(二)

2010-08-06 20:04  Tracy E  阅读(1060)  评论(0编辑  收藏  举报

NavigationController 推出push 推出pop

[self.navigationControllerpushViewController:_detailControlleranimated:YES];
[self.navigationControllerpopViewControllerAnimated:YES];

 

Debug:

NSLog(@"%s %d", __FUNCTION__, __LINE__);

 

纯代码时,点击textField外的地方回收键盘:

先定义一个UIControl类型的对象,在上面可以添加触发事件,令SEL实践为回收键盘的方法,最后将UIControl的实例加到当前View上。

UIControl *m_control = [[UIControlalloc] initWithFrame:CGRectMake(0, 0, 320,
480)];
[m_control addTarget:selfaction:@selector(keyboardReturn)
forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:m_control];
 
- (void) keyboardReturn
{
[aTextFieldresignFirstResponder];
}

 

调整输入界面

当键盘调出时将输入框覆盖时,可以用下方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self.viewsetFrame:CGRectMake(0, -100, 320, 480) ];
    returnYES;
}

 

当准备输入时,将视图的位置上调100,这样键盘就不能覆盖到输入框。

 

当依赖注入方法不好使时,可以在AppDelegate内申明一个全局的控制器实例_anotherViewController,在另一个需要使用_anotherViewController的地方定义以下委托方法,使用共享的UIApplication实例来获取该委托的引用

SomeAppDelegate *appDelegate = (SomeAppDelegate *)[[UIApplicationsharedApplication] delegate];
_anotherViewController = appDelegate._anotherViewController;

 

纯代码在UIViewController控制器内建Table View

@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *timeZoneNames;
}
@property (nonatomic,retain) NSArray *timeZoneNames;
@end
 
- (void) loadView
{
  UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] style: UITableViewStylePlain];
  tableView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingWidth);
  tableView.delegate = self;
  tableView.dataSource = self;
  [tableView reloadData];
 
  self.view = tableView;
  [tableView release];
}
 

 

 

plist文件中的数据赋给数组

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:thePath];
 

 

UITouch

手指的触摸范围:64X64

 

#pragma mark -
#pragma mark Touch Events
 
- (void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event {
originFrame = bookCover.frame;
NSLog(@"%s%d", __FUNCTION__,__LINE__);
 
if ([touches count] == 2)
{
  NSArray *twoTouches = [touches allObjects];
  UITouch *firstTouch = [twoTouches objectAtIndex:0];
  UITouch *secondTouch = [twoTouches objectAtIndex:1];
  CGPoint firstPoint = [firstTouch locationInView:bookCover];
  CGPoint secondPoint = [secondTouch locationInView:bookCover];

  CGFloat deltaX = secondPoint.x - firstPoint.x;
  CGFloat deltaY = secondPoint.y - firstPoint.y;
  initialDistance = sqrt(deltaX * deltaX + deltaY * deltaY );
  frameX = bookCover.frame.origin.x;
  frameY = bookCover.frame.origin.y;
  frameW = bookCover.frame.size.width;
  frameH = bookCover.frame.size.height;
  NSLog(@"%s%d", __FUNCTION__,__LINE__);
}
}
 
- (void)touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event {

if([touches count] == 2)
{
  NSLog(@"%s%d", __FUNCTION__,__LINE__);
 
  NSArray *twoTouches = [touches allObjects];
  UITouch *firstTouch = [twoTouches objectAtIndex:0];
  UITouch *secondTouch = [twoTouches objectAtIndex:1];

  CGPoint firstPoint = [firstTouch locationInView:bookCover];
  CGPoint secondPoint = [secondTouch locationInView:bookCover];

  CGFloat deltaX = secondPoint.x - firstPoint.x;
  CGFloat deltaY = secondPoint.y - firstPoint.y;
  CGFloat currentDistance = sqrt(deltaX * deltaX + deltaY * deltaY );

  if (initialDistance == 0) {
    initialDistance = currentDistance;
  }
  else if (currentDistance != initialDistance)
  {
    CGFloat changedDistance = currentDistance - initialDistance;
    NSLog(@"changedDistance = %f",changedDistance);
    [bookCoversetFrame:CGRectMake(frameX - changedDistance / 2,
    frameY - (changedDistance * frameH) / (2 * frameW),
    frameW + changedDistance,
    frameH + (changedDistance * frameH) / frameW)];
  }
}
}
 
- (void)touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event {
UITouch *touch = [touches anyObject];

//双击图片变大/还原
if ([touch tapCount] == 2)
{
  NSLog(@"%s%d", __FUNCTION__,__LINE__);
 
  if (!flag) {
    [bookCoversetFrame:CGRectMake(bookCover.frame.origin.x - bookCover.frame.size.width / 2,
    bookCover.frame.origin.y - bookCover.frame.size.height / 2,
    2 * bookCover.frame.size.width,
    2 * bookCover.frame.size.height)];
    flag = YES;
}
  else {
    [bookCoversetFrame:CGRectMake(bookCover.frame.origin.x + bookCover.frame.size.width / 4,
       bookCover.frame.origin.y + bookCover.frame.size.height / 4,       bookCover.frame.size.width / 2,
      bookCover.frame.size.height / 2)];     flag = NO;   } } }

 

 

Get the Location of Touches

- (CGPoint)locationInView:(UIView *)view
- (CGPoint)previousLocationInView:(UIView *)view

 

viewwindow

 

Getting Touch Attributes

tapCount(read only)

timestamp(read only)

phase(read only)

 

Getting a Touch Object's Gesture Recognizers

gestureRecognizers

 

Touch Phase

UITouchPhaseBegan

UITouchPhaseMoved

UITouchPhaseStationary

UITouchPhaseEnded

UITouchPhaseCancelled

 

Plist里读内容

NSString *plistPath = [[NSBundlemainBundle] pathForResource:@"book"ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionaryalloc] initWithContentsOfFile:plistPath];
NSString *book = [dictionary objectForKey:bookTitle];
[textViewsetText:book];

 -(void) initialize {
NSUserDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES" forKey:@"DeleteBackup"];
[defaults registerDefaults:appDefaults];
}

 

 

To get a value of a default, use the valueForKey: method:

[[theDefaultsController values] valueForKey:@"userName"];

 

To set a value for a default, use setValue:forKey:

[[theDefaultsController values] setValue:newUserName forKey:@"userName"];

 

[[NSUserDefaults standardUserDefaults] setValue:aVale forKey:aKey];
[[NSUserDefaults standardUserDefaults] valueForKey:aKey];
 

 

获取Documents目录

NSArray *paths = NSSearchPathForDictionariesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [documentsDirectory
stringByAppendingPathComponent:@"theFile.txt"];

 

 

获取tmp目录

NSString *tempPath = NSTemporaryDirectory();
NSString *tempFile = [tempPath stringByAppendingPathComponent:@"tempFile.txt"];
 
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"someKey"];
[[NSUserDefaults standardUserDefaults] objectForKey:aKey];