简体   繁体   中英

Why my UITextField data is lost when we come back from Second view controller to main view controller?

I have three view controllers (first, second and third view controllers), first view controller and second view controller has 4 UITextField each one, after entering the data in text fields we can go from one VC to another along with data, finally we display data in third VC. The problem is when we come back from second to first the textfield is no more there. Not sure why this happens? How to solve this problem? Please help me resolve this issue.

when you push or present a new UIViewController you can send data with it but as soon as you reach the second UIViewController your first UIViewController releases the space from phone hence all your data in previous screen is lost.

and the only option you have is to send back the data you send while coming to second UIViewController and you can do it is by simply creating delegate class to send data.

create a header file lets say Sample.h and create this method inside it you can choose the datatype you want.

- (void)secVCDidDismisWithData:(NSDictionary *)data;

import header file in second UIViewController in .h file and put this line of code there

@property (nonatomic, assign)   id<Sample> delegate;

then you have to make custom pop button or dismiss button and put this code

[self.navigationController popViewControllerAnimated:YES];
if(_delegate && [_delegate respondsToSelector:@selector(secVCDidDismisWithData:)])
{
    [_delegate secVCDidDismisWithData:@{@"key1":@"value1",@"key2":@"value2"}];
}

then on the first UIViewController .m file import your second UIViewController which I am sure you already have done to present or push the screen then just below that add put this line

@interface FirstVC () <Sample>

and then create the object of second UIViewController and set delegate to self and the implement the delegate method you made like this

- (void)secVCDidDismisWithData:(NSDictionary *)data{
     NSLog(@"%@",data);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM