EXC_BAD_ACCESS个问题

问题描述 投票:2回答:1

我正在制作一个诗歌应用程序,用户可以在其中编写您的诗歌并将其保存到文件中。当我单击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];
}
objective-c uitextview exc-bad-access
1个回答
1
投票

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

@synthesize newPoemTextView;
@synthesize newPoemTextViewBackground;

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

© www.soinside.com 2019 - 2024. All rights reserved.