我必须保留 UIDatePicker 模态视图中的日期吗?
我的 UIDatePicker 视图发生崩溃,我认为这是因为我没有保留选择器选择的日期。谁能告诉我这是否正确?
我有一个用于选择 toDate
和 fromDate
范围的模式视图。这些值被传递到模态视图中,并在视图关闭时从视图中取出。 该视图有一个 UIDatePicker 和一个分段按钮,用于在起始日期和起始日期之间切换。
每次分段控制切换时,我都会将选择器日期设置为匹配的起始日期或起始日期。当选择器值发生变化时,我会相应地更新起始日期或起始日期。在这些日期之间切换几次后,视图会崩溃。
我没有保留选择器选择的日期,所以我猜测当我将选择器日期的值从 toDate
设置为 fromDate
时,toDate 正在发布,因此当我将选择器切换回
toDate
时,它会崩溃。
另外,要在视图之外使用选择器中选择的日期,是否需要保留该日期,因为选择器将与日期一起发布?
这对任何人都有意义吗?
I'm getting crashes from my UIDatePicker view and I think it's because I'm not retaining the pickers selected date. Can anyone tell me if this could be correct?
I have a modal view for selecting a toDate
and a fromDate
range. These values are passed into the modal view and grabbed out of the view when it's dismissed.
The view has one UIDatePicker and a segmented button for switching between the to and from dates.
Every time the segmented control switches I set the pickers date to the matching to or from date. When the picker value changes I update the to or from dates accordingly. The view crashes after a couple of switches between these dates.
I'm not retaining the pickers selected date so I'm guessing when I set the value of the pickers date from the toDate
to the fromDate
the toDate
is being released so when I switch the picker back to the toDate
it's going to crash.
Also to use the selected date from the picker outside the view will the date need to be retained as the picker will be released along with the date?
Does this make sense to anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要从 UIDatePicker 获取日期值,并且您将在函数范围之外使用它(例如,在多个 AutoreleasePool 周期中),则确实需要保留一个副本。
从 UIDatePicker 获取日期将保留引用,但它将自动释放,因此只有在自动释放池被销毁之前才有效。
请记住在完成后发布您的参考资料。
如果只是在函数内临时使用它,则不需要如上所述保留它。
If you need to grab the date value from a UIDatePicker, you indeed need to retain a copy if you'll be using it outside the scope of the function (say, over multiple AutoreleasePool cycles).
Getting the date from a UIDatePicker will retain a reference but it will be autoreleased so effectively is only valid until the autorelease pool is destroyed.
Remember to release your reference once you've finished with it.
For simply using it temporarily inside a function, you won't need to retain it as stated above.
为什么不将 viewvcontroller 或其他类的属性设置为您使用的日期:
self.date = date;
将日期定义为带有保留属性的@property。这样您应该能够在应用程序的其他位置使用日期,并且当您执行运行循环时它不会自动释放。
Why don't you set a property of your viewvcontroller or other class to the date you get using:
self.date = date;
Define date to be a @property with a retain attribute. That way you should be able to use date in other places of your app, and it won't be autoreleased when you go through the runloop.