2012-08-06 100 views
1

如果这是一个糟糕的问题,我很抱歉,因为我是iOS开发新手。所以这里是我的问题:我声明了一个类变量NSString,该字符串从textView中分配一个字符串值,但是当我尝试从其他方法访问字符串时,该应用程序崩溃。下面的代码:访问方法之间的NSString导致应用程序崩溃

接口:(ClassName.h)

@interface ClassName: UIViewController <UITextViewDelegate>{} 
@property (nonatomic, assign)NSString *strSomeText; 
@end 

实施:(ClassName.m)

@implementation 
@synthesize strSomeText; 
- (void)textViewDidChange:(UITextView *)textView{ 
    strSomeText = textView.text; 
    NSLog(@"%@", strSomeText); //This line works just fine 
} 
- (void)textViewDidEndEditing:(UITextView *)textView{ 
    NSLog(@"%@", strSomeText); //this line causes the app to crash 
} 
@end 

感谢您的帮助! Loc。

回答

4

您的问题可能是由于您使用assign作为您的财产。这意味着字符串可以被释放,而你仍然有一个引用它。尝试使用copy代替:

@property (nonatomic, copy) NSString *strSomeText; 

那么你应该用你的属性访问你的textViewDidChange:方法:

self.strSomeText = textView.text; 
+0

非常感谢杰西。有用! – user945711 2012-08-06 15:11:15