首次点击 UITextField 时出现奇怪的崩溃
Posted
技术标签:
【中文标题】首次点击 UITextField 时出现奇怪的崩溃【英文标题】:Weird crash when first tapping UITextField 【发布时间】:2011-05-13 19:10:09 【问题描述】:我的 Cocos2d 应用程序中有一个文本字段,当我点击它开始输入时,应用程序就崩溃了。我没有更多要解释的,这是我的代码: .h:
@interface myScene : CCLayer <UITextFieldDelegate> .......
@property (nonatomic, retain, readonly) UITextField *first;
@property (nonatomic, retain, readonly) UITextField *second;
.mm:
@implementation myScene
@synthesize first, second;
......
first = [[UITextField alloc] initWithFrame:CGRectMake(160.0, 160.0, 200.0, 30.0)];
first.placeholder = @"first";
first.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction suppor
first.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard)
first.returnKeyType = UIReturnKeyDone;
first.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
first.autocapitalizationType = UITextAutocapitalizationTypeNone;
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
first.transform = transform;
first.adjustsFontSizeToFitWidth = YES;
first.textColor = [UIColor blackColor];
[first setFont:[UIFont fontWithName:@"Arial" size:14]];
first.backgroundColor = [UIColor clearColor];
first.borderStyle = UITextBorderStyleRoundedRect;
first.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
[[[CCDirector sharedDirector] openGLView] addSubview: first];
second = [[UITextField alloc] initWithFrame:CGRectMake(130.0, 160.0, 200.0, 30.0)];
second.placeholder = @"second";
second.returnKeyType = UIReturnKeyDone;
second.transform = transform;
second.adjustsFontSizeToFitWidth = YES;
second.textColor = [UIColor blackColor];
[second setFont:[UIFont fontWithName:@"Arial" size:14]];
second.backgroundColor = [UIColor clearColor];
second.borderStyle = UITextBorderStyleRoundedRect;
second.secureTextEntry = YES;
second.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
[[[CCDirector sharedDirector] openGLView] addSubview: second];
...............
- (BOOL)textFieldShouldReturn:(UITextField *)textField
// the user pressed the "Done" button, so dismiss the keyboard
[textField resignFirstResponder];
return YES;
我在带有 ios 4.3 的 Xcode 4 上运行,当我点击其中一个字段时,我得到 EXC_BAD_ACCESS,Xcode 将我指向 main.m:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
NSAutoreleasePool *pool = [NSAutoreleasePool new];
int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
[pool release];
return retVal;
Xcode 表示行:int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
谢谢。
【问题讨论】:
贾利亚是对的。您确实需要解释更多内容,这就是您在应用崩溃时收到的确切消息和其他行为。 发生崩溃时,在调试器中输入 bt 并粘贴到这里。 【参考方案1】:我不认为这是在您的 openGL 视图中添加文本字段的最佳方式,事实上我不确定它是否会起作用。您可能需要将您的字段放在 UIViewController 子类中,然后将该类的视图添加到您的openGLView
,或者有一个很好的 UIViewWrapper 您可以获得here。它允许您将 UIView 元素添加到您的 CCLayer。希望这会有所帮助
【讨论】:
【参考方案2】:尝试按照http://www.cocoadev.com/index.pl?NSZombieEnabled 中的说明添加 NSZombieEnabled
它应该写一些比“EXC_BAD_ACCESS”消息更有趣的东西
【讨论】:
以上是关于首次点击 UITextField 时出现奇怪的崩溃的主要内容,如果未能解决你的问题,请参考以下文章
在 NSMutablearray 中添加新实例时出现奇怪的崩溃
从 didFinishLaunchingWithOptions 访问核心数据时出现奇怪的崩溃
在键盘演示中点击 UITextView 或 UITextField 时出现 UIScrollView 问题