进入UITextField时崩溃应用程序

时间:2011-04-05 11:27:41

标签: objective-c cocoa-touch uitextfield

当我点击完成按钮或文本字段之外时,我用它来隐藏键盘:

  

在视图中加载:

PidField.returnKeyType = UIReturnKeyDone;
PidField.delegate = self;
HesloField.returnKeyType = UIReturnKeyDone;
HesloField.delegate = self;
  

然后在程序中:

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {

    if (theTextField == PidField) {
        [PidField resignFirstResponder];
    }
    if (theTextField == HesloField) {
        [HesloField resignFirstResponder];
    }

    return YES;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    [PidField resignFirstResponder];
    [HesloField resignFirstResponder];

}

这是很好的测试代码,我多次使用它,但现在当我进入文本字段时,它会让我的应用程序崩溃。我不明白为什么或在哪里我必须找到错误

感谢

//
//  InfoViewController.m
//  PhotoScroller
//
//  Created by Csaba on 3/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "InfoViewController.h"


@implementation InfoViewController
@synthesize PidField8, HesloField8;

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

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

- (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.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed:205/255.0 green:234/255.0 blue:242/255.0 alpha:1]; 

    PidField8.returnKeyType = UIReturnKeyDone;
    PidField8.delegate = self;
    HesloField8.returnKeyType = UIReturnKeyDone;
    HesloField8.delegate = self;

}


-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {

    if (theTextField == PidField8) {
        [PidField8 resignFirstResponder];
    }
    if (theTextField == HesloField8) {
        [HesloField8 resignFirstResponder];
    }

    return YES;

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    [PidField8 resignFirstResponder];
    [HesloField8 resignFirstResponder];

}


- (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);
}

@end

1 个答案:

答案 0 :(得分:1)

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{

    [textField resignFirstResponder];
    return YES;
}

删除此内容并尝试...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    [PidField resignFirstResponder];
    [HesloField resignFirstResponder];

}

并在.h文件中写委托  UITextViewDelegate