ios - View之间传输时变量值没有改变

标签 ios uiviewcontroller

我有两个 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/

相关文章:

ios - 无法设置任何 IBOutlet

ios - 即使多个 subview Controller 的 View 不可见,是否可以将多个 subview Controller 与父 UIViewController 关联起来?

ios - 什么时候在 iOS 中检索图像高度是安全的

iphone - 如何通过正则表达式匹配字符串值?

ios - Google 标签管理器错误

ios - swift 崩溃 : function signature specialization <Arg[0] = Owned To Guaranteed>

ios - 在 Swift 项目中使用以 Objective-c 编写的库类

ios - 如何在 Swift 的 UIAlertView 中调整附件 View 的大小

iphone - 为什么我的新 View Controller 没有出现?

c# - View 已添加到 UIViewControllerWrapperView,但显示不正确