第一次点击 UITextField 时发生奇怪的崩溃

发布于 2024-11-06 18:37:50 字数 2928 浏览 0 评论 0原文

我的 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");

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-11-13 18:37:50

我不认为这是在 openGL 视图中添加文本字段的最佳方法,事实上我不确定它是否有效。您可能需要将字段放在 UIViewController 子类中,然后将该类的视图添加到您的 openGLView 中,或者您可以获得一个很好的 UIViewWrapper 此处。它允许您将 UIView 元素添加到 CCLayer。希望这有帮助

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

尝试添加 NSZombieEnabled,如 http://www.cocoadev.com/index.pl?NSZombieEnabled

它应该写一些比“EXC_BAD_ACCESS”消息更有趣的东西

try adding NSZombieEnabled as described in http://www.cocoadev.com/index.pl?NSZombieEnabled

It should write something more instresting than "EXC_BAD_ACCESS" message

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文