EXC_BAD_ACCESS 问题

发布于 2024-11-04 20:18:53 字数 1754 浏览 1 评论 0原文

我正在制作一个诗歌应用程序,用户可以在其中写诗并将其保存到文件中。当我单击 UITextView 添加和编辑文本时,它会立即导致 EXC_BAD_ACCESS。有人可以帮我解释为什么会发生这种情况吗?

这是我在 .h 文件中的内容

#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController {
    UITextView *newPoemTextView;
    UIImageView *newPoemTextViewBackground;

}
@property (nonatomic, retain) IBOutlet UITextView *newPoemTextView;
@property (nonatomic, retain) IBOutlet UIImageView *newPoemTextViewBackground;
-(IBAction)closeKeyboard;
@end

,这是我在 .m 文件中的内容

@implementation SecondViewController
@synthesize newPoemTextView;
@synthesize newPoemTextViewBackground;

-(IBAction)closeKeyboard {
    // This part will write the information to the file
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"savedText.txt"];
    NSString *savedString = newPoemTextView.text;
    [savedString writeToFile:documentTXTPath atomically:YES];

    //This part hopefully closes the keyboard correctly, cross fingers
    [newPoemTextView resignFirstResponder];

}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    UIImage *image = [UIImage imageNamed: @"newPoemBackground.png"];
    [newPoemTextViewBackground setImage:image];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage
                                                                 imageNamed:@"background.png"]];
    self.view.backgroundColor = background;
    [background release];

    [super viewDidLoad];
}

I am making a poetry app in which users can write your poems and save them to a file. When I click on the UITextView to add and edit text it instantly causes a EXC_BAD_ACCESS. Can somebody please help me as to why this is happening.

This is what I have in the .h file

#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController {
    UITextView *newPoemTextView;
    UIImageView *newPoemTextViewBackground;

}
@property (nonatomic, retain) IBOutlet UITextView *newPoemTextView;
@property (nonatomic, retain) IBOutlet UIImageView *newPoemTextViewBackground;
-(IBAction)closeKeyboard;
@end

and this is what I have in the .m file

@implementation SecondViewController
@synthesize newPoemTextView;
@synthesize newPoemTextViewBackground;

-(IBAction)closeKeyboard {
    // This part will write the information to the file
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"savedText.txt"];
    NSString *savedString = newPoemTextView.text;
    [savedString writeToFile:documentTXTPath atomically:YES];

    //This part hopefully closes the keyboard correctly, cross fingers
    [newPoemTextView resignFirstResponder];

}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    UIImage *image = [UIImage imageNamed: @"newPoemBackground.png"];
    [newPoemTextViewBackground setImage:image];

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage
                                                                 imageNamed:@"background.png"]];
    self.view.backgroundColor = background;
    [background release];

    [super viewDidLoad];
}

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

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

发布评论

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

评论(1

烟凡古楼 2024-11-11 20:18:53

您应该在 SecondViewController.m 文件中综合这两个属性。

@synthesize newPoemTextView;
@synthesize newPoemTextViewBackground;

您还应该实现 Keyboard 的 all 方法,这会对您有很大帮助。

You should synthesize both the property in SecondViewController.m file.

@synthesize newPoemTextView;
@synthesize newPoemTextViewBackground;

You should also implement Keyboard's all method, that will help you a lot.

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