I have a view that through a prepareSeque method should pass whatever is written in a textView to the next view.
Before I had textField and textfield.text in the seque method worked fine. But I can not get it to work with a textView.
I have a NSString property in the .h file: @property (weak, nonatomic) NSString *textString;
I syntesize it in the .m file: @synthesize textString =_textString;
In my textViewDidEndEditing i can see (through debug) that the text in the text View is picked up and that the textString is set.
- (void)textViewDidEndEditing:(UITextView *)textView {
NSString *theText = textView.text;
self.textString =theText;
}
However, when I then want to retrieve the textString in my Seque method it does not contain any text:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"go"]) {
ISecondViewController *vc = [segue destinationViewController];
vc.funnyString = self.textString;
}
If i enter: self.textString =@”Hi Hi”; in the Seque method funnyString will be passed with Hi Hi so that part works fine.
Have I just totally misunderstood the “get and set” of NSString in this case?
Your problem here is that you have been using a
weakproperty fortextStringso it will benilwhen the scope of your property get out of yourtextViewDidEndEditingmethod.Why? because the principle of weak reference is that it will be set to
nilas soon as the object you are referring to doesn’t exist anymore. This will be the case for the “theText” object which won’t exist at the end of your method. Use astrongproperty instead.