简体   繁体   English

从自定义单元类访问UITextFields文本到主VC类

[英]Access UITextFields text from custom cells class to the main VCs class

I have a custom cell that I created in the storyboard which is linked to a class called customCell . 我在故事板中创建了一个自定义单元格,该单元格链接到名为customCell的类。 In it, I have UITextFields that are linked to customCell.h . 在其中,我有UITextFields链接到customCell.h The textField do have different tags for them. textField确实有不同的标签。

Then there is a class called mainVC . 然后有一个名为mainVC的类。 That class is the class of the viewController 该类是viewController的类

How can I get the textFields text in the mainVC class? 如何在mainVC类中获取textFields文本?

The easiest, but wrong way of doing it is like so: 最简单但却错误的做法是这样的:

CustomCell *myCell = [[CustomCell alloc] init];

The problem of doing this, is, it will create a new customCell , and therefore the textFields text will be a null. 这样做的问题是,它将创建一个新的 customCell ,因此textFields文本将为null。

You don't access the text field to get the text, you implement one of the UITextFieldDelegate (or UITextView, it's not clear which you have) methods in your view controller. 您不访问文本字段来获取文本,您在视图控制器中实现了一个UITextFieldDelegate(或UITextView,它不清楚您有哪些)方法。 In cellForRowAtIndexPath, set the controller (self) to be the delegate of the text field. 在cellForRowAtIndexPath中,将控制器(self)设置为文本字段的委托。 You probably want to implement textFieldDidEndEditing: and textFieldShouldReturn:. 您可能希望实现textFieldDidEndEditing:和textFieldShouldReturn:。 After the keyboard is dismissed, textFieldDidEndEditing: will be called, and you can get the text inside that method. 键盘解除后,将调用textFieldDidEndEditing:,您可以获取该方法内的文本。 If you need to get the indexPath of the cell the text field was contained in, you can use the table view method, indexPathForRowAtPoint:, passing the text field's origin or center for the point argument. 如果需要获取包含文本字段的单元格的indexPath,则可以使用表视图方法indexPathForRowAtPoint :,传递文本字段的原点或点参数的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM