我的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");
谢谢。
最佳答案
我不认为这是在您的openGL视图中添加文本字段的最佳方法,实际上我不确定它是否会起作用。您可能需要将字段包含在UIViewController子类中,然后将该类的视图添加到您的openGLView
中,或者有一个不错的UIViewWrapper您可以得到here。它使您可以将UIView元素添加到CCLayer。希望这可以帮助
关于cocoa-touch - 第一次点击UITextField时发生奇怪的崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5996630/