首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >resignFirstResponder

resignFirstResponder
EN

Stack Overflow用户
提问于 2009-12-18 23:11:17
回答 2查看 1.8K关注 0票数 0

我在退出键盘时遇到了一点小问题。我有一张有两部分的桌子。在第二节中,我加载了类stationenStartCellNumber,它有一个UITextField

当我按下UITextField以外的任何地方时,我希望键盘辞去第一响应者的职务,从而关闭键盘。它在我的牢房里工作得很好,但在我的UITableView上就不行了。

这是我的代码:stationenViewController.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

当然,touchesBegan代码不能在tableView上工作,因为该代码将接管该代码。我怎么才能辞职呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-18 23:20:14

您可以创建一个不可见的UIButton,其中包含一个退出键盘的操作。

此外,您还需要确保滚动您的表视图,以便键盘不覆盖您的文本字段。

票数 0
EN

Stack Overflow用户

发布于 2009-12-19 06:32:35

您可以将UITableView子类化,并使用它代替标准的UITableView。为什么?因为你能做这样的事

(

  • (void)touchesBegan:(NSSet *)触碰withEvent:(UIEvent *)事件{超级触摸屏贝根:使用event:event;}

每次您触摸您的表时都会调用此方法。因此,您可以在此方法中辞去textField作为firstResponder的职务。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1931200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档