我有两个 View ,对于每个 View 我都关联了一个 UIViewController 类。
在view1(名为RechercherViewController)中,我有一个简单的UITextField,用户在其中输入一些内容,然后单击一个按钮,单击此按钮时,用户将被重定向到view2(名为StationsSurLaCarteViewController) ),我必须在 UILabel 中向他展示他在上一个 View 中输入的内容。我的计划效果很好,如我所愿,但对于第一篇文章,我的意思是,尽管用户返回并更改了它,但第一个值没有改变,他总是(在 view2 的标签中)找到他第一次输入的内容。
所有声明都是正确的,这是我在 view1 中按钮的 IBAction 中的代码:
RechercherViewController.m
-(IBAction)goToStationsSurLaCarteView {
TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication sharedApplication]delegate];
topStation.data=[typeCarburantTextField text];
stationsSurLaCarteViewController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:stationsSurLaCarteViewController animated:YES];
}
在第二个 View 中,此代码位于 viewDidLoad 中: StationsSurLaCarte.m:
- (void)viewDidLoad {
TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication sharedApplication]delegate];
[label setText:topStation.data];
[super viewDidLoad];
}
我不知道,但我怀疑我是否错过了一些必须释放才能始终具有用户输入的新值的东西。
最佳答案
在goToStationsSurLaCarteView中,由于您不是每次都重新创建stationSurLaCarteViewController(使用alloc+init),因此viewDidLoad只会在第一次调用presentModalViewController时被调用。
一个简单的修复方法是将标签设置代码移动到StationsSurLaCarte.m中的viewWillAppear:
(或viewDidAppear:
):
-(void)viewWillAppear:(BOOL)animated
{
TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication sharedApplication]delegate];
[label setText:topStation.data];
}
关于ios - View之间传输时变量值没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5818639/