切换视图控制器时如何保留UIDatePicker和UITextField值? [英] How to Retain UIDatePicker and UITextField Values When Switching View Controllers?

查看:77
本文介绍了切换视图控制器时如何保留UIDatePicker和UITextField值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个View Controller. View Controller#2具有UIDatePickerUITextField.

I have two View Controllers. View Controller #2 has a UIDatePicker and a UITextField.

如果我返回到View Controller#1然后又回到View Controller#2,则UIDatePicker失去了其先前选择的日期,并且UITextField为空.

If I go back to View Controller #1 then back to View Controller #2, the UIDatePicker has lost its previous selected date and the UITextField is blank.

它是通过以下方式初始化的:

It was initialized this way:

@property (retain, nonatomic) IBOutlet UIDatePicker *datepick;

我要使用Push Segue进入第二视图控制器,然后使用以下方法返回:

I am getting to the 2nd View Controller using a Push Segue and getting back using:

[self.navigationController popViewControllerAnimated:YES];

如何保存" UIDatePicker日期和UITextField值,以便在返回该View Controller时始终存在该值?

How can I 'SAVE' the UIDatePicker date and UITextField value so that it is always there when returning to that View Controller?

我已经搜索并找到了观点,假设和建议,但没有解决方案.

I have searched and found opinions, hypotheticals, and suggestions but no solutions.

谢谢.

推荐答案

之所以会发生这种情况,是因为(如果您正确地管理内存)在弹出第二个视图控制器时会释放它(返回第一个).因此,当您要返回到它时,实际上是创建一个具有默认值的全新实例.

This happens, because (if you manage memory properly) the second view controller is released when you pop it (go back to the first one). So when you want to go back to it, you actually create a completely new instance, with default values.

一种解决方案是确保您对视图控制器1中的视图控制器2有很强的引用.在第一个视图控制器中添加一个属性:

One solution would be to make sure you have a strong reference to view controller 2, from view controller 1. Add a property in the first view controller:

@property (nonatomic, retain) SecondViewController *mySecondViewController;

实施吸气剂:

- (SecondViewController *)mySecondViewController {
    if (!mySecondViewController) {
        mySecondVieWController = [[SecondViewController alloc] init...];
    }
    return mySecondViewController;
}

然后,当您要显示它时:

Then when you want to present it:

[self.navigationController pushViewController:self.mySecondViewController animated:YES];

第一次调用它时,mySecondViewController仍为nil,因此getter对其进行初始化.下次调用getter时,它只会返回相同的实例.

The first time you call it, mySecondViewController will still be nil, so the getter initializes it. The next time you call the getter, it simply returns the same instance.

别忘了在dealloc中释放视图控制器.顺便说一句,请考虑使用自动引用计数(ARC).

Don't forget to release the view controller in dealloc. And by the way, consider using Automatic Reference Counting (ARC).

这种方法相对易于实现,但缺点是,即使不再需要第二个视图控制器,它也保留在内存中.

This approach is relatively easy to implement, but it has to downside, that the second view controller is kept in memory, even when it's not needed anymore.

另一种方法是将选定的日期或文本传递回您的第一个视图控制器(可能通过自定义委托协议),而下次您要显示第二个视图控制器时,将这些值传递给它以设置日期和文字.

An other way would be to pass back the selected date or text to your first view controller (possibly via a custom delegate protocol) and the next time you want to present the second view controller, you pass it these values to set the date and text.

这篇关于切换视图控制器时如何保留UIDatePicker和UITextField值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆