Why can't I pass value using prepareForSegue?

ios, objective-c, segue

Solution

You should declare the 2 properties in B ViewController as `strong` or even better as `copy`:

@interface ResetPwdBySmsCodeViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) NSString *phone_no;
@property (strong, nonatomic) NSString *test;
@end

The reason why you can get `_test` is that you are assigning it a literal string value, which is allocated by the compiler in a specific memory area. It is thus never deallocated and the `weak` variable will indefinitely point at it (until you explicitly assign a new value to `_test`, that is.)

On the other hand, you assign `vc.phone_no` a property from another object that might not exist anymore when you access the `weak` property (since a `weak` property is nil-ed when the object it points to is deallocated.) Hence the need for `strong` or `copy`.

Problem

I have two `UIViewControllers` .In A `ViewController` I have code: ``` - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"ToResetPwd_NumberSegue"]) { ResetPwdBySmsCodeViewController* vc = (ResetPwdBySmsCodeViewController*)[segue destinationViewController]; vc.phone_no = _TF_phoneOrEmail.text;//TF_phoneOrEmail is a UITextField vc.test = @"dagaga"; } } ``` In B .h I have code ``` @interface ResetPwdBySmsCodeViewController : UIViewController<UITextFieldDelegate> @property (weak, nonatomic) NSString *phone_no; @property (weak, nonatomic) NSString *test; @end ``` In B .m I can get the value of `_test`,but not `_phone_no` I am sure that `vc.phone` is well in the `prepareForSegue`

Original source