在关闭视图之前从 UITextField 读取最新值时出现问题

发布于 2024-10-24 23:19:51 字数 1151 浏览 2 评论 0原文

我有一个视图,我正在以模式方式显示以从用户那里获取登录信息。我有以下设置:

  • 视图控制器是一个 UITableViewController
  • 该表具有三个单元格:用户名和密码字段(两者都是 UITableViewCell 内的 UITextField 类型)和一个简单的单元格作为登录按钮。
  • textFieldDidEndEditing:message 中的 UITextField 读取值
  • 。文本字段由不同的标签值标识。

问题

我的问题是,当焦点位于 UITextField 中并且用户触摸登录按钮时,相应 UITextFieldtextFieldDidEndEditing :消息在didSelectRowAtIndexPath:之后发送。现在的问题是,我正在向 didSelectRowAtIndexPath: 中的 LoginViewControllerDelegate 发送一条新用户凭据的消息,而此时文本字段的值尚未读取。

一些想法

我有一些如何解决这个问题的想法,但每个想法都很复杂。

首先,我可以关闭登录视图,并在关闭期间通知委托人,但我想给予委托人(拥有此登录视图的人)完全控制权,我认为成功关闭登录视图应该是委托人的工作登录(登录视图仅读取凭据,委托验证这些凭据)。

其次,我还可以在呼叫代表之前读取用户名或密码,但随后我必须查找文本字段。如果视图不可见,我认为假设细胞确实存在是错误的。这只是一个很大的假设,但我不想让它变得正确。保留 UITextField 可以吗?但是,这样我就无法使用一些本身提供文本字段的自定义单元格。

基本上我想要以下内容:

  1. 检测到需要(有效)用户凭据,弹出登录视图
  2. 用户输入用户名和密码并调用完成。
  3. 委托人验证这些凭证。如果它们不起作用,则会显示一条消息,然后重试步骤 2。
  4. 凭据正常,因此关闭登录视图并继续。

I have a view that I'm displaying modally to get login information from a user. I have the following setup:

  • The view controller is a UITableViewController
  • The table has three cells: username and password fields (both of type UITextField within UITableViewCell) and one simple cell as the login button.
  • Values are read from UITextField in textFieldDidEndEditing:message
  • The text fields are identified by different tag values.

The problem

My problem is that when the focus is in a UITextField and user touches the login button, the respective UITextField's textFieldDidEndEditing:message is sent after the didSelectRowAtIndexPath:. Now the issue here is that I'm sending a message of new user credentials to my LoginViewControllerDelegate in the didSelectRowAtIndexPath: and at that time the text field's value is not read yet.

Some Ideas

I have some ideas how to fix this, but I have complications with each of them.

First, I could close the login view and the delegate is notified during the closing, but I want to give the delegate (one who owns this login view) full control and I think it should be the delegate's job to close the login view on successful login (the login view only reads the credentials, the delegate validates these).

Second, I could also read the username or password just before calling the delegate but then I'd have to look up the text fields. If the views are not visible, I think it is wrong to assume that the cells do exist. This is just a big if, but I wan't to make it right. Would it be ok to retain the UITextFields? This way, however, I can't use some custom cell that would itself provide the textfield.

Basically I want the following:

  1. Need for (valid) user credentials is detected, login view is popped up
  2. User inputs the username and password and invokes done.
  3. The delegate validates these credentials. If they do not work, a message is shown and try step 2 again.
  4. Credentials are ok, so close the login view and continue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-10-31 23:19:51

经过所有技巧之后,我终于想到,只需在调用委托之前辞职第一响应者就可以解决此问题。在表视图控制器中,我只需添加一个调用:

    [[self.view findFirstResonder] resignFirstResponder];

findFirstResonder 来自一个类别,可以在 此处

After all tricks I finally figured that simply resigning the first responder right before invoking the delegate fixed this. In the table view controller I simply had to add a call:

    [[self.view findFirstResonder] resignFirstResponder];

The findFirstResonder is from a category and can be found here.

寄离 2024-10-31 23:19:51

尝试此操作,而不是使用第三个单元格作为登录按钮。在登录视图上使用简单的按钮。在该按钮的 IBAction 上,您可以验证用户输入。

Try this instead of using third cell as login button. Use a simple button on login view. On that button's IBAction you can validate user input.

作死小能手 2024-10-31 23:19:51

您可以使用带有两个文本字段和一个 UIButton 的简单视图。按下按钮后,您可以通过 [yourView removeFromSuperview] 从超级视图中删除视图。您可以从 yourTextfield.text 接收数据。

我想这一定能解决问题

You can use a simple view with two textfields and one UIButton. After the button is pressed you can just remove the view from superview by [yourView removeFromSuperview] . You can receive the data from the yourTextfield.text.

I guess this must do the trick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文