1

I don't understand! I'm a novice, so this may be my fundamental misunderstanding of Objective-C, life, the universe and/or everything; please be gentle.

  1. I set the inputView for a text field to be self.datePicker - a standard UIDatePicker, which has been initialised in viewDidLoad.
  2. I add some subviews to the datePicker. Sounds crazy, I know, but bear with me.
  3. I now have that field resign first responder, and call self.datePicker = nil. Even more crazy, yes.
  4. I tap the textField again, and expect one of two things. Either one, no datePicker. I destroyed it, right? Needs to be re-allocated and initialised. Or two, a fresh datePicker, without the extra subviews. Not sure why I expect this (again, I destroyed it!), but hey, it's possible.
  5. What I get is the same picker, complete with the added subviews, plus any date that was previously set in it! What the what?!? says I.

I know you're wondering why I want to do this. Why add views to datePicker at all, and why not just remove them from their superview when needed. But let's assume I have a good reason for doing this, and doing it this way. Can anyone correct my obviously wonky understanding of object behaviour?

4

2 回答 2

1

不,您没有破坏,只是取消了 viewController 上的属性。我认为您的设计可能有点偏离,但如果您创建了某种自定义日期选择器,您应该添加一个重置方法,该方法应该将自定义日期选择器返回到基本状态。

要将其从属性中删除UITextFieldinputView日期选择器的新实例中。

于 2012-11-28T14:31:54.783 回答
1

听起来有两个对 datePicker 的引用,但您只是将其中一个设置为 nil。如果我跟着你,两个引用是 self.datePicker 和 textField.inputView。但是您只是将第一个设置为零。所以对象被 textField.inputView “保留”。

一般来说,依靠 ARC 来销毁对象似乎很不稳定。此外,一旦 datePicker 被销毁,textField.inputView 就不会是 nil 而不是新的 datePicker 吗?

于 2012-11-28T14:36:22.723 回答