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];
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 SecondViewController.m 文件中综合这两个属性。
您还应该实现 Keyboard 的 all 方法,这会对您有很大帮助。
You should synthesize both the property in SecondViewController.m file.
You should also implement Keyboard's all method, that will help you a lot.