Carlos Perez
Carlos Perez

Reputation: 122

EXC_BAD_ACCESS questions

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];
}

Upvotes: 2

Views: 348

Answers (1)

Nirav Jain
Nirav Jain

Reputation: 5107

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.

Upvotes: 1

Related Questions