2009-12-18 2 views
0

J'ai un petit problème à démissionner du clavier. J'ai une table avec deux sections. Dans la deuxième section, je charge la classe stationenStartCellNumber qui a un UITextField.resignFirstResponder

Lorsque j'appuie sur n'importe quelle touche en dehors du UITextField, je souhaite que le clavier se désactive en tant que premier répondeur et ferme ainsi le clavier. Cela fonctionne parfaitement à l'intérieur de ma cellule, mais pas sur mon UITableView.

Ceci est mon code: stationenViewController.h:

#import <UIKit/UIKit.h> 
#import "stationenStartCell.h" 
#import "stationenAppDelegate.h" 

@interface stationenViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    IBOutlet UITableView *theTable; 

    NSString *stationenPlistPath; 
} 

@property (nonatomic, retain) UITableView *theTable; 
@property (nonatomic, retain) IBOutlet NSString *stationenPlistPath; 

- (void)copyStationPlist; 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; 

@end 

stationenViewController.m:

#import "stationenViewController.h" 
#import "chooseStationView.h" 
#import "chooseArrDepView.h" 
#import "stationenStartCellNumber.h" 

@implementation stationenViewController 

@synthesize theTable; 
@synthesize stationenPlistPath; 

- (void)viewWillAppear:(BOOL)animated { 
    [theTable reloadData]; 

    [[[self navigationController] navigationBar] setAlpha:1]; 

    [super viewWillAppear:animated]; 
} 

- (void)viewDidLoad 
{ 
    NSArray *bikePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *bikeDocumentsDirectory = [bikePath objectAtIndex:0]; 
    stationenPlistPath = [bikeDocumentsDirectory stringByAppendingPathComponent:@"stationen.plist"]; 

    if(![[NSFileManager defaultManager] fileExistsAtPath:stationenPlistPath]) 
    { 
     [self copyStationPlist]; 
    } 

    [super viewDidLoad]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSUInteger returnInt = 2; 

    if (section == 1) 
    { 
     returnInt = 1; 
    } 

    return returnInt; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    NSString *returnString = [[NSString alloc] init]; 

    if(section == 0) 
    { 
     returnString = @"Search station details"; 
    } 
    else if (section == 1) 
    { 
     returnString = @"Search train number"; 
    } 

    return returnString; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     static NSString *CellIdentifier = @"stationenStartCellID"; 
     stationenStartCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

     if (cell == nil){ 
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"stationenStartCell" owner:nil options:nil]; 

      for(id currentObject in topLevelObjects) 
      { 
       if([currentObject isKindOfClass:[stationenStartCell class]]) 
       { 
        cell = (stationenStartCell *)currentObject; 
        break; 
       } 
      } 
     } 

     [[cell theBackground] setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"stationenStartCell_Background.png"]]]; 
     [[cell theImage] setImage:[UIImage imageNamed:@"icon_checkMark.png"]]; 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.5]; 

     if(indexPath.row % 4 == 0) 
     { 
      [[cell cellName] setText:NSLocalizedString(@"Choose station", @"Main page")]; 
      [[cell chosenValue] setText:[prefs valueForKey:@"chosenStation"]]; 

      NSLog(@"Log info: %@", [prefs valueForKey:@"chosenStation"]); 

      if([[prefs valueForKey:@"chosenStation"] isEqualToString:@""] || [prefs valueForKey:@"chosenStation"] == nil) 
      { 
       [[cell theImage] setAlpha:0.2]; 
      } 
      else { 
       [[cell theImage] setAlpha:1]; 
      } 
     } 

     if(indexPath.row % 4 == 1) 
     { 
      NSString *searchType = [prefs valueForKey:@"searchType"]; 
      NSString *theValue = @""; 
      if([searchType isEqualToString:@"0"]) 
      { 
       theValue = NSLocalizedString(@"Arrivals", @"Main page"); 
      } 
      else if([searchType isEqualToString:@"1"]) 
      { 
       theValue = NSLocalizedString(@"Departures", @"Main page"); 
      } 

      if([theValue isEqualToString:@""]) 
      { 
       [[cell theImage] setAlpha:0.2]; 
      } 
      else { 
       [[cell theImage] setAlpha:1]; 
      } 

      [[cell cellName] setText:NSLocalizedString(@"Choose departure/arrival", @"Main page")]; 
      [[cell chosenValue] setText:theValue]; 
     } 

     [UIView commitAnimations]; 

     return cell; 
    } 
    else if (indexPath.section == 1) 
    { 
     static NSString *CellIdentifier = @"stationenStartCellNumber"; 
     stationenStartCellNumber *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil){ 
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"stationenStartCellNumber" owner:nil options:nil]; 

      for(id currentObject in topLevelObjects) 
      { 
       if([currentObject isKindOfClass:[stationenStartCellNumber class]]) 
       { 
        cell = (stationenStartCellNumber *)currentObject; 
        break; 
       } 
      } 
     } 

     [[cell theLabel] setText:@"Tåg nr:"]; 

     return cell; 
    } 

    return nil; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; 

    if (indexPath.section == 0) 
    { 
     if(indexPath.row == 0) 
     { 
      chooseStationView *chooseStationViewController = [[chooseStationView alloc] initWithNibName:@"chooseStationView" bundle:nil]; 
      [[self navigationController] pushViewController:chooseStationViewController animated:YES]; 
      [chooseStationViewController release], chooseStationViewController = nil; 
     } 
     else { 
      chooseArrDepView *chooseArrDepViewController = [[chooseArrDepView alloc] initWithNibName:@"chooseArrDepView" bundle:nil]; 
      [[self navigationController] pushViewController:chooseArrDepViewController animated:YES]; 
      [chooseArrDepViewController release], chooseArrDepViewController = nil; 
     } 
    } 
    else { 
     [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
    } 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [theTable becomeFirstResponder]; 
    [super touchesBegan:touches withEvent:event]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationPortrait || 
      interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
      interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

-(void)copyStationPlist 
{ 
    NSString* bikesDictionaryPath = [[NSBundle mainBundle] pathForResource:@"stations" ofType:@"plist"]; 
    NSDictionary* bikesDictionary = [[NSDictionary alloc] initWithContentsOfFile:bikesDictionaryPath]; 

    NSArray *bikePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *bikeDocumentsDirectory = [bikePath objectAtIndex:0]; 
    NSString *path = [bikeDocumentsDirectory stringByAppendingPathComponent:@"stations.plist"]; 

    NSString * error; 
    NSData * data = [NSPropertyListSerialization dataFromPropertyList:bikesDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 
    [data writeToFile:path atomically:YES]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 

Offcourse, le code touchesBegan ne fonctionne pas sur un tableView parce que l'on prend le relais. Comment puis-je démissionner du clavier?

Répondre

0

Vous pouvez créer un UIButton invisible avec une action pour démissionner du clavier.

En outre, vous voulez vous assurer de faire défiler l'affichage de votre table afin que le clavier ne couvre pas votre champ de texte.

2

vous pouvez sous-classe UITableView et de l'utiliser au lieu de la norme UITableView. Pourquoi? Parce que vous serez en mesure de faire grincer comme ce

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touvhesBegan:touches withRvrnt:event]; 
} 

cette méthode sera appelée à chaque fois que vous touchez votre table. donc vous serez en mesure de démissionner votre textField comme firstResponder à l'intérieur de cette méthode.

Questions connexes