Weird crash when first tapping UITextField
I have a text field in my Cocos2d app and when I tap it to start typing, the app just crashes. I don't have much more to explain, here's my code: .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;
}
I'm running on Xcode 4 with iOS 4.3 and when I tap one of the fields I get EXC_BAD_ACCESS, and Xcode points me to 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 indicates the line: int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
Thanks.
i dont think thats the best way to add a text field in your openGL view in fact im not sure it will work. You probably either need to have your fields in a UIViewController subclass and then add the view of that class to your openGLView
, or there is a good UIViewWrapper you could get here. It lets you add UIView elements to your CCLayer. Hope this helps
try adding NSZombieEnabled as described in http://www.cocoadev.com/index.pl?NSZombieEnabled
It should write something more instresting than "EXC_BAD_ACCESS" message
精彩评论