2011-05-03 60 views
2

我正在製作一個詩歌應用程序,用戶可以在其中編寫詩歌並將其保存到文件中。當我點擊UITextView添加和編輯文本時,它立即導致EXC_BAD_ACCESS。有人可以幫我解釋爲什麼會發生這種情況。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]; 
} 
+0

只是讓你知道'newPoemTextViewBackground'永遠不會被合成。 – Joe 2011-05-03 17:57:15

+0

補充說,現在,但它仍然給我同樣的錯誤,當keyboad打開。 – 2011-05-03 18:16:38

+0

**如果發生崩潰,請發回追蹤。** – bbum 2011-05-03 20:27:21

回答

1

你應該綜合無論是在SecondViewController.m文件屬性。

@synthesize newPoemTextView; @synthesize newPoemTextViewBackground;

你也應該實現Keyoard的所有方法,這將有助於你。

+0

我綜合了另一部分,我把它遺漏了,它也會崩潰,我會嘗試像以前的建議者那樣實現鍵盤代表。如果發生任何事情,會回覆。 – 2011-05-05 08:06:55

相關問題