ios - UITextfield 在 UIPageViewController (Swift) 中非常快速地显示和关闭
<p><p>我有一个 <code>UIPageViewController</code> 作为我的应用程序的演练演示。演练基本上总共有5页。第 4 页有一个以编程方式设置的 <code>UITextField</code>。不要介意标签或图像等其他对象。</p>
<p>我遇到的问题是,当用户到达第 4 页时,会向用户显示一个文本字段,我希望键盘通过 <code>becomeFirstResponder</code> 自动显示。</p>
<p>当我使用文本字段加载第四个 View 时,它会很快在模拟器和我的设备上显示,然后又返回。它基本上通过 <code>becomeFirstResponder</code> 代码显示,但不知何故在 1 秒内辞职。</p>
<p>我已经以编程方式以及通过 Storyboard<code>UITextfield</code> 进行了尝试,以查看这是否有所作为,但两者似乎都不起作用。 </p>
<p>执行标签的代码也是我以编程方式执行 <code>UITextField</code> 的地方,但对于本示例,我删除了它并改用了 IBOutlet,如您所见。 </p>
<p>另外,在这个 <code>case 3</code> 中,我会使用 <code>becomeFirstResponder</code> 来激活键盘,但它仍然没有按预期工作。</p>
<p>如你所见,现在我把它留在了 <code>viewWillAppear</code> 方法中。</p>
<p>这是我针对这个特定场景的代码:</p>
<pre><code> import UIKit
class WalkthroughViewController: UIViewController {
@IBOutlet var headingLabel:UILabel!
@IBOutlet var contentLabel:UILabel!
@IBOutlet var contentImageView:UIImageView!
@IBOutlet var pageControl:UIPageControl!
@IBOutlet var forwardButton:UIButton!
// This was added via storyboard via drag and drop
@IBOutlet var nameField: UITextField!
// This is a UITextfield programmatically
let textField = UITextField(frame: CGRectMake(20, 200.0, 330.0, 40.0)) // x=x-cord, y=y-cord, Width, Height
// May 2 Updates
func textFieldFunc() {
textField.textAlignment = NSTextAlignment.Center
textField.textColor = UIColor.wetAsphaltColor()
textField.backgroundColor = UIColor.whiteColor()
textField.font= UIFont(name: "avenir", size: 21)
textField.borderStyle = UITextBorderStyle.None
textField.autocapitalizationType = UITextAutocapitalizationType.Words // If you need any capitalization
textField.becomeFirstResponder()
self.view.addSubview(textField)
}
func nameLabel() {
let label = UILabel(frame: CGRectMake(15, 180, 265, 33))
label.center = CGPointMake(185, 160)
label.textColor = UIColor.cloudsColor()
label.font= UIFont(name: "avenir", size: 30)
label.textAlignment = NSTextAlignment.Center
label.text = "Whats your name?"
self.view.addSubview(label)
}
@IBAction func submit(sender: AnyObject) {
// Thisis going to handle the name later and it will then reguster the user for later use within the app
}
var index = 0
var heading = ""
var imageFile = ""
var content = ""
override func viewDidLoad() {
super.viewDidLoad()
headingLabel.text = heading
contentLabel.text = content
contentImageView.image = UIImage(named: imageFile)
pageControl.currentPage = index
// 0...2
switch index {
case 0...2: forwardButton.setTitle("NEXT", forState: UIControlState.Normal)
nameField.hidden = true
// 3
case 3:
nameLabel()
nameField.hidden = false
case 4:
forwardButton.setTitle("DONE", forState: UIControlState.Normal)
default: break
}
print("Index: \(index)")
}
@IBAction func nextButtonTapped(sender: UIButton) {
switch index {
case 0...3: // 2
let pageViewController = parentViewController as! WalkthroughPageViewController
pageViewController.forward(index)
case 4: // 3
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true, forKey: "finishedWalkedThrough")
dismissViewControllerAnimated(true, completion: nil)
default: break
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if nameField.hidden == false {
nameField.becomeFirstResponder()
}
}
</code></pre>
<p>}</p>
<p>我的问题是,我该如何解决这个问题,以便当第 4 页以滑动方式呈现时,<code>UITextfield</code> 可以像普通 View 一样呈现键盘?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>正如我在一篇非常相似的帖子中看到的,这确实有效。我设法通过使用这个来解决它:</p>
<pre><code>override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if index == 3 {
dispatch_async(dispatch_get_main_queue(), {() -> Void in
let strongSelf: WalkthroughViewController = self
strongSelf.textField.becomeFirstResponder()
})
}
}
</code></pre>
<p>现在,当我滚动到第 4 页(从 0 开始的第 3 页)时,它会像普通的 <code>UITextField</code> 那样加载带有 <code>.becomeFirstResponder</code> 的键盘。</p></p>
<p style="font-size: 20px;">关于ios - UITextfield 在 UIPageViewController (Swift) 中非常快速地显示和关闭,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37085024/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37085024/
</a>
</p>
页:
[1]