【问题标题】:crashing application when entering UITextField进入 UITextField 时应用程序崩溃
【发布时间】:2011-04-05 11:27:40
【问题描述】:

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

在视图中确实加载了:

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

【问题讨论】:

  • 当我注释掉这一行时 //HesloField.delegate = self;那么它不会掉下来也不会隐藏键盘
  • 在调试模式下运行,看看它在哪里崩溃以及什么消息。还请应用以小写字母开头的 ivars 等命名约定。阅读和理解是一种痛苦。
  • 我先调试它,但我找不到它落在哪里。首先我调试然后我问... :)

标签: objective-c cocoa-touch uitextfield


【解决方案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

【讨论】:

  • 还是一样,没用
  • 做一件事..复制粘贴你的整个类文件...让我看看..你又犯了一个错误..
  • .m 检查你的笔尖没问题。当你崩溃的时候?
  • 当我点击进入文本字段时
  • UITextViewDelegate 或 UITextFieldDelegate ?
猜你喜欢
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
相关资源
最近更新 更多