我有一个UIPageViewController
作为我的应用的演练演示文稿。演练基本上总共有5页。第4页以编程方式设置UITextField
。不要介意标签或图像等其他对象。
我遇到的问题是,当用户到达第4页时,会向用户显示一个文本字段,我希望键盘能够通过becomeFirstResponder
自动显示。
当我使用文本字段加载第4个视图时,它会在模拟器和设备上快速显示,然后再返回。它基本上通过becomeFirstResponder
代码显示,但不知何故在1秒内就会重新开始。
我已经通过编程方式和故事板UITextfield
尝试了这一点,看看是否有所作为,但两者似乎都不起作用。
执行标签的代码也是我以编程方式执行UITextField
的地方,但是对于这个例子,我删除了它并使用了IBOutlet,如你所见。
另外,在这个case 3
中,我会让becomeFirstResponder
激活键盘,但它仍然没有按预期工作。
现在我把它留在viewWillAppear
方法中,如你所见。
以下是此特定方案的代码:
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()
}
}
}
我的问题是,如何解决这个问题,以便当第4页以幻灯片方式显示时,UITextfield
可以像普通视图一样呈现键盘?
答案 0 :(得分:1)
正如我在一篇非常相似的文章中所看到的,这实际上是有效的。我设法用这个来解决它:
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()
})
}
}
现在,当我滚动到第4页(从0开始的第3页)时,它会使用.becomeFirstResponder
作为正常UITextField
加载键盘。
答案 1 :(得分:0)
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
[self.yourTextField becomeFirstResponder];
});
答案 2 :(得分:0)
对于Swift 3:
DispatchQueue.main.async(execute: {() -> Void in
let strongSelf: WalkthroughViewController = self
strongSelf. textField.becomeFirstResponder()
})
我没有必要指定ViewController的索引位置