【问题标题】:variable value hasn't been changed when transmisson between ViewsViews之间传输时变量值没有改变
【发布时间】:2011-04-28 12:34:01
【问题描述】:

我有两个视图,每个视图都关联了一个 UIViewController 类。
在 view1(名为 RechercherViewController)中,我有一个简单的 UITextField,用户在其中输入内容,然后单击一个按钮,当单击此按钮时,用户被重定向到 view2(名为 StationsSurLaCarteViewController ) 我必须在 UILabel 中向他展示他在之前的视图中输入的内容。我的计划按我的意愿工作得很好,但是对于第一个 essai,我的意思是第一个值没有改变,尽管用户返回并更改了它,他总是(在 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];


}

在第二个视图中,此代码位于 viewDidLoad 中: StationsSurLaCarte.m:

- (void)viewDidLoad {

    TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication sharedApplication]delegate];
    [label setText:topStation.data];


    [super viewDidLoad];




}

我不知道,但我怀疑我是否遗漏了某些必须释放才能始终保留用户输入的新值的内容。

【问题讨论】:

    标签: ios uiviewcontroller


    【解决方案1】:

    在goToStationsSurLaCarteView中,由于不是每次都重新创建stationSurLaCarteViewController(使用alloc+init),viewDidLoad只会在第一次调用presentModalViewController时被调用。

    一个简单的解决方法是将标签设置代码移动到 StationsSurLaCarte.m 中的viewWillAppear:(或viewDidAppear:):

    -(void)viewWillAppear:(BOOL)animated
    {
        TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication sharedApplication]delegate];
        [label setText:topStation.data];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 2014-10-11
      • 2011-06-02
      • 2018-01-20
      • 2022-01-06
      • 2010-09-19
      • 1970-01-01
      相关资源
      最近更新 更多