Maybe this is a simple problem, but I spend some time try to solve it and so far I failed.
I want to show custom view with a few of button after I clicked a block of text. I try to add and remove this view form subview but it's doesn't work.
Can you give me some tips about my problem?
Thank you for your help.
my code
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var myTextField: UITextField!
#IBOutlet weak var simpleView: SimpleView!
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.delegate = self
self.simpleView.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidBeginEditing(textField: UITextField!) {
println("works")
self.view.addSubview(simpleView)
}
func textFieldDidEndEditing(textField: UITextField!) {
println("works2")
}
}
You'd better set the hidden property of simpleView instead of removing and adding the view.
put a container view in your main view and set the visibility hidden. when you want to show the popup, set the visibility to visible and load the view inside the container. To get the popup
look and feel ,you can use a transformation to animate.
Related
error when try to assign a string to the UITextView.
coding as below:-
class ViewController: UIViewController{
#IBOutlet weak var txtResult: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtResult.text = "ABC"
}
}
Go to your code and delete the #IBOutlet line of code that you created
Delete this in Storyboard:
I think you have few referencing outlets there for 1 object. You need to have only 1. So better to remove all of them.
Add new outlet again.
It should work.
Providing more error details might help to solve your issue. But you can still solve it by checking few things-:
1) Check for UITextView outlet in storyboard-:
2) Check if you have given correct class to controller . In my case it's UnderlineViewController check for yours. Your controller should have same class.
2.a) Go to storyboard click on controller and select identity inspector.
2.b) check class name.
Now add text in controller class-:
import UIKit
class UnderlineViewController: UIViewController {
#IBOutlet weak var textViewData: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textViewData.text = "Tushar"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
OUTPUT-:
MAKE SURE YOU HAVE NO MORE THEN 1 OUTLET FOR ANY VIEW IN SIMILAR
CONTROLLER.
I realize this question has been asked numerous times before, but I can't quite get the solutions to work, even by just copying and pasting them, and suspect that most swift documentation spans the three versions since swift's release.
I'm attempting to do something as simple as storing a variable from a field input and not having much luck.
import UIKit
class ViewController: UIViewController {
#IBOutlet var userNumber: UILabel!
#IBOutlet var userField: UITextField!
#IBAction func userButton(_ sender: UIButton) {
let userInput = userField.text
//some action
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You should check whether you have set your textfield's delegate with respect to parent view controller.
Go to storyboard.
Select textfield.
Right click on it.
set delegate from textfield to view controller
I have a very simple app so far. Two view controllers. I've set up a new .swift file for the second view. On each view I have a button that when pressed, changes a label to say "Pressed". Pretty simple.
On the first view controller everything works as expected. However, on the second view controller the app crashes when I press the button. I've set up IBOutlets and actions for all appropriate parts.
Does anyone have any insight?
code:
import UIKit
class PlayViewController: UIViewController {
#IBOutlet weak var newCardButton: UIButton!
#IBOutlet weak var labelTest: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func newCardButtonPressed(sender: UIButton) {
self.labelTest.text = "Pressed"
}
}
Screenshot:
Screenshot after crash- http://i.imgur.com/CHt8kA5.png
I think you should change the sender part like this.
#IBAction func newCardButtonPressed(sender: AnyObject) {
self.labelText.text = "Pressed"
}
If your connections are not set properly your app also crash. Delete them and reconnect it.From Utilities/connections inspector.
So I have a UITextView and some placeholder text inside. When the user taps inside the the view, I want to execute some code, i.e. clear the placeholder text. I was trying to create an IBAction but it won't let me. I looked it up online and found this UITextViewDelegate Protocol Reference but I can't figure out how to use it. A lot of the examples I've found for working with delegates are Objective-C and I am working in Swift.
Sorry for the simple question I'm new at this.
Thanks!
Given an IBOutlet to a text view someTextView, all you need to do is make your class conform to UITextViewDelegate, set that text view's delegate to self, and implement the textViewDidBeginEditing method:
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var someTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
someTextView.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
println("Some code")
}
}
The View Controller should adhere to UITextViewDelegate. Then make sure to implement textViewDidBeginEditing delegate methods. The below code should clear the default place holder text when the user starts editing the textview.
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidBeginEditing(textView: UITextView) {
self.textView.text = ""
}
}
I'm programming a iOS app in Swift language and all I have to do is create a custom input for a textfield.
I created an additional View Controller with two buttons and what I want is this view controller (instead of the keyboard) to pop-up when I highlight my textfield.
Basically what I want is to create a small custom keyboard, but I just want it to be inside my app: I found lots of tutorials about creating custom keyboards, but it is not the same as having a simple View Controller that pops-up when text field is highlighted.
Can you suggest how to assign my view controller to textField.inputViewController in Swift?
Thanks
You can assign your own viewcontroller to inputViewcontroller:
Your viewController has to be a subclass of UIInputViewController for example:
class CustomInputViewController: UIInputViewController {
#IBOutlet var insertTextButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.inputView?.translatesAutoresizingMaskIntoConstraints = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func insertText(_ button: UIButton){
self.textDocumentProxy.insertText((button.titleLabel?.text)!);
}
}
Here with only one button insertTextButton which I design in the xib-file.
In your main view controller you need a subclass of your textfield (or textview):
class textfield: UITextField {
var _inputViewController : UIInputViewController?
override public var inputViewController: UIInputViewController?{
get { return _inputViewController }
set { _inputViewController = newValue }
}
}
which you assign to your textfield.
Now you can assign your own inputViewcontroller to your textfield, for example:
class ViewController: UIViewController {
private var customInputViewController = CustomInputViewController(nibName: "CustomInputViewController",
bundle: nil)
#IBOutlet var textField: textfield!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.inputViewController = customInputViewController
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I used the xib-file with the name CustomInputViewController.xib to design the keyboard
As far as I know, you cannot use a view controller. You need to make your own view and assign it to the inputView field. Make sure the view has a delegate so it knows which field to use:
MyInputView keyboard = ...
field.inputView = keyboard
keyboard.delegate = field