输入 UITextField 时应用程序崩溃

发布于 2024-10-30 06:39:39 字数 2835 浏览 0 评论 0原文

当我单击“完成”按钮或在文本字段外部时,我用它来隐藏键盘:

视图中确实加载了:

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

I use this to hide keyboard when i click done button or outside of text field:

in view did load:

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

then in program:

-(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];

}

It is good and tested code i used it many times, but now when i enter the textfield it makes my app crash. i do not understand it why or where i have to find the error

thanks

//
//  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-11-06 06:39:39
- (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

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

    [textField resignFirstResponder];
    return YES;
}

remove this and try...

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

    [PidField resignFirstResponder];
    [HesloField resignFirstResponder];

}

and in .h file write delegate
UITextViewDelegate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文