问题
I'm experimenting with using table views inside a Storyboard to build forms, but I can't figure out a simple way to access the form fields when the table is inside a container.
This is what I have:
- a custom view controller (LoginController) with a Container View where the form goes
- a static UITableViewController embedded in the Container
- fields (email, password, etc.) inside cells in the UITableViewController
My question is: what's the simplest way to access the value of the email and password fields from LoginController? There doesn't seem to be any way to link the fields to IBOutlets in the LoginController. I could make a custom table view controller for every form I build, or do something with the segue, but that's getting too complicated. Is there any way to keep everything in the storyboard?
回答1:
If you want a table view, then you have two choices. You can use dynamic prototypes instead of a static table, so you can add a table view directly to the login controller (and size it how you want). Or, you can do it like you are doing in your question with an embedded table view controller. In that case, you could make the login controller the delegate of the table view controller in prepareForSegue. That method would be called when the login controller is instantiated, so you can set yourself as delegate there. You would then need to define a delegate protocol in the table view controller to send the data you collect back to the delegate. Doing it this way, there's no way to set up everything in the storyboard (in terms of direct outlets, because you can't set up outlets across controllers).
来源:https://stackoverflow.com/questions/18940116/using-uitableview-inside-container-view-for-forms-how-do-i-access-the-form-fie