在第二个视图控制器中编辑 UITableView 中的文本
我有一个静态表视图(FirstViewController),有 3 行,每行都有自己的部分。前两个单元格中有 UITextFields,当用户点击它们或单元格时可以编辑它们。最后一个单元格有一个 UILabel,当点击它时会推动 SecondViewController,其中包含一个 UITextField。当用户按下时,UILabel 的值需要是 UITextField 的值。
如果我在 SecondViewController 上创建一个委托属性(分配),并将其设置为 FirstViewController,那么如何保证 FirstViewController 仍位于内存中而不是空?据我了解,一旦视图控制器不是最顶层的视图控制器(可见的),它就可以被释放。那么,如果设备内存不足并释放 FirstViewController 时会发生什么,那么当用户按下返回方法时,将不会发送返回方法,因为 delegate
将为 nil,之后会生成一个新的实例FirstViewController 将被创建并弹出到屏幕上,而不从 SecondViewController 接收值。
我不想在 AppDelegate 中使用“全局”变量,因为我个人认为这有点混乱。
I have a static table view (FirstViewController), with 3 rows, each in their own section. the first two cells have UITextFields in them, which are editable when the user taps on them or the cell. The last cell has a UILabel, which when tapped pushes SecondViewController, which contains a UITextField. When the user presses back the value of the UILabel needs to be the value of the UITextField.
If I create a delegate property (assign) on the SecondViewController, which is set to FirstViewController, what guarantee is there that FirstViewController will still be in memory and not nill? As I understand it as soon a view controller is not the top most view controller (the visible one) it can be deallocated. So what would happen if the device runs out of memory, and deallocates the FirstViewController, then when the user presses back the return method will not be sent as delegate
will be nil, and after that a new instance of FirstViewController will be created and popped onto the screen, without receiving the value from the SecondViewController.
I don't want to use a "global" variable in the AppDelegate, as I personally think that's a bit messy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个 Data 类,在其中可以设置变量或数组的属性(用于在 UITableView 中显示数据)。在数据类中实现一个类方法,用于检查该对象是否已实例化。如果没有,它就会这样做。它是这样的:
现在在您的视图控制器中,您需要将此方法调用为:
并使用数组。
这样您就可以在不干扰
AppDelegate
的情况下分配数据,这是一个很好的做法。You can create a Data class where you can set the properties of variables or arrays (for displaying data in UITableView). Implement a class method in data class which checks that object has been instantiated or not. If not, it does that. It is something like this :
Now in your view controller you need to call this method as :
And use the arrays.
This way you can assign data without disturbing
AppDelegate
, which is a good practice.