星期四, 2月 02, 2012

ios iphone/ipad Dev 學習筆記 (A-07) Dismiss Keyboard


環境:Xcode 4.2 + iOS SDK 5


在 (A-05) 的練習裡,每當游標進到 UITextField ,iOS 都會自動的把螢幕鍵盤打開,但關閉鍵盤的工作卻是 Programmer 的責任,在這個練習裡就要來把關閉螢幕鍵盤的方法給找出來。

HomeViewController.h 改成這樣:

  #import <UIKit/UIKit.h>
  @interface HomeViewController : UIViewController<UITextViewDelegate>

  @property (nonatomic, strong) UILabel * label;
  @property (nonatomic, strong) UIButton * button;
  // 該用 UITextField 來做練習,比較單純
  @property (nonatomic, strong) UITextField * textbox;

  @end




HomeViewController.m 改成這樣

  #import "HomeViewController.h"
  #import "SecondViewController.h"
  #import "BPGblShareData.h"

  @implementation HomeViewController

  @synthesize label;
  @synthesize button;
  @synthesize textbox;

  SecondViewController * secondViewController;

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }

  - (void)didReceiveMemoryWarning
  {
      // Releases the view if it doesn't have a superview.
      [super didReceiveMemoryWarning];
    
      // Release any cached data, images, etc that aren't in use.
  }

  - (void)viewDidLoad
  {
      CGRect frame = CGRectMake(20, 10, 280, 50);
      label = [[UILabel alloc] initWithFrame:frame];
      label.textAlignment = UITextAlignmentCenter;
      label.font = [UIFont fontWithName:@"Verdana" size:20];
      label.text = @"Hello World !!!";
    
      // add first button
      frame = CGRectMake(20, 300, 280, 50);
      button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      button.frame = frame;
      button.backgroundColor = [UIColor clearColor];
      [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      [button setTitle:@"這是按鈕" forState:UIControlStateNormal];

      // action handler for button
      [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];


      // add textField
      frame = CGRectMake(20, 70, 280, 50);
      textbox = [[UITextField alloc] initWithFrame:frame];
      self.textbox.delegate = self; // 注意這一個項目,要用到 TextFieldDidBeginEditing 需要這個    
    self.textbox.returnKeyType = UIReturnKeyDone; // 指定鍵盤類型,『Return』鍵顯示成『Done
      [textbox setBackgroundColor:[UIColor lightGrayColor]];
      [textbox setFont:[UIFont fontWithName:@"arial" size:30.0]];

    
      //add subviews 
      [self.view addSubview:label];    
      [self.view addSubview:button];
      [self.view addSubview:textbox];
    
      [super viewDidLoad];    

  }

  - (void)viewDidUnload
  {
      [super viewDidUnload];
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
  }

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }


  -(void) btnClicked:(id) sender{   
      BPGblShareData * gblSharedData = [BPGblShareData sharedManager];
      gblSharedData.textFieldText = self.textbox.text;

      secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    
      [UIView beginAnimations:@"flipping view" context:nil];
      [UIView setAnimationDuration:1];
      [UIView setAnimationCurve:UIViewAnimationOptionTransitionNone];
      [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view.superview cache:NO];
    
      [self.view.superview addSubview:secondViewController.view];

      //要把自己移除,否則會留下『殘影』
      [self.view removeFromSuperview];
      [UIView commitAnimations];        
  }

  // 任何一個 TextField 開始被編輯
  -(void) textFieldDidBeginEditing:(UITextField *) textFieldView
  {    
      currentTextField = textFieldView;
          //currentTextField.backgroundColor = [UIColor redColor];
  }

  // TextField 結束編輯。當 user 按了鍵盤上的 Return
  -(BOOL) textFieldShouldReturn:(UITextField * ) textFieldView{
      [textFieldView resignFirstResponder];
      return YES;
  }
  @end

沒有留言: