输入 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除它并尝试...
并在 .h 文件中写入委托
UITextViewDelegate
remove this and try...
and in .h file write delegate
UITextViewDelegate