EXC_BAD_ACCESS问题

时间:2011-05-03 17:48:32

标签: objective-c uitextview exc-bad-access

我正在创作一个诗歌应用程序,用户可以在其中编写诗歌并将其保存到文件中。当我单击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];
}

1 个答案:

答案 0 :(得分:1)

您应该在SecondViewController.m文件中合成该属性。

@synthesize newPoemTextView; @synthesize newPoemTextViewBackground;

你还应该实现Keyoard的所有方法,这将有助于你。