I am trying to use a calculator as a custom input view to an UITextField in swift.
I have a calculator separately defined in my app as CalculatorViewController.
I need to access the CalculatorViewController in the UITextField I have in my InputViewController.
I defined the following property in the InputViewController to access the calculator view
var textFieldInputView : UIView!{
let calculatorInputController = self.storyboard?.instantiateViewControllerWithIdentifier("calculatorVC") as! calculatorViewController
let calculatorInputView = calculatorInputController.view
return calculatorInputView
}
then added following code in the textField to access the textFieldInputView
textField.inputView = textFieldInputView
Now when I click the textField, the calculator popup,as per the below, which is not in the position where keyboard appears. Also none of the buttons working.
Much appreciated if some one could advise me how to get this fixed.
Resolved this as per the below.
Created a interface builder "keyboard.xib" with all the UIButton and UILabel for display.
Linked the keyboard.xib to calculatorViewController
Subclass the view controller (say DetailViewController) where the UITextFields are present as the subclass of calculatorViewController.
Created a UIView as per the below code
var customkeyboardView : UIView {
let nib = UINib(nibName: "CalculatorView", bundle: nil)
let objects = nib.instantiateWithOwner(self, options: nil)
let cView = objects[0] as! UIView
return cView
}
Assign the customkeyboardView as an inputView of the UITextField as indicated below
textField.inputView = customkeyboardView
Related
I want to access UIElements (e.g Labels and TextFields, etc) of viewController without making IBOutlet connections.
e.g. I have a UITextField in viewController and I want to access it like viewControllerName.textFieldName.text = "something I will set here"
or as the same concept of Android findViewById("id of element")
I have used "Tags" but it does not meet my requirement.
You will create UITextField programmatically, and access the UITextFieldName.text = "Some" programmatically.
Create a label programmatically Check This link
Yes, you can access UIElements(e.g Labels and TextFeilds etc) of viewController without making IBOutlet connections. First give UIElement a Tag.
Then try this. hope this would help:
If you want to access it in the same viewController
let label = self.view.viewWithTag(4) as? UILabel
label?.text = "Hello there"
and if you want to access it from other viewController
in firstViewControler
import UIKit
internal weak var FirstViewController: ViewController?
//in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
FirstViewController = self
}
it will make it accessible in all other viewControllers
then in secondViewController
let newlabel = homeViewController?.view.viewWithTag(4) as? UILabel
newlabel?.text = "new change"
I have created on custom View which contain one label and button
I have created outlet for both in UIView class
I have one UIViewController in which i have subview that custom view
now i want to change label name and want to do something on button click.
let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
firstView.addSubview(commonView)
commonView.lblCommon.text = "We can change label in particular Screen"
I am able to change label text but how can i get button action event in UIViewcontroller?
Use protocol
In your customView declare a protocol
protocol CustomViewProtocol : NSObjectProtocol{
func buttonTapped()
}
Now create a variable in custom view
weak var delegate : CustomViewProtocol? = nil
In your viewController confirm to protocol
extension ViewController : CustomViewProtocol {
func buttonTapped() {
//do whatever u waana do here
}
}
set self as delegate in view controller
let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
firstView.addSubview(commonView)
commonView.delegate = self
finally in IBAction of button in custom view trigger delegate
self.delegate?.buttonTapped()
In Swift 4:
If you want to catch the tap within your UIViewController do this in the viewDidLoad() after you instantiate your view from a nib (eg called aView):
let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
aView.addGestureRecognizer(tap)
Below viewDidLoad declare the following function and do whatever you want within its body:
#objc func tapAction() {
//Some action here
}
(A note: In Swift 4 the #objc annotation is obligatory, because it denotes that you want the function to be visible to the Obj-C runtime, which in UIKit handles (among other things) user interactions through its messaging system.)
Since this UIView instance belongs in the view hierarchy of this UIViewController it makes sense to handle the taps within this UIViewController. This gives you the ability to reuse this UIView within any other UIViewController and handle its tap logic from the respective UIViewController.
In case there is a particular reason you want the taps handled within the UIView, you can use the delegate pattern, a closure, or in a more advanced implementation a Reactive framework (ReactiveSwift or RxSwift/RxCocoa)
The way to do it with RxSwift/RxCocoa:
view.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext: { _ in
//react to taps
})
.disposed(by: stepBag)
more ways to manage Gestures, can be found at the RxSwift github page: https://github.com/RxSwiftCommunity/RxGesture
how to add mathwidget as a subview inside another UIViewController
Currently, mathwidget is working fine when loading UIViewController.
let subViewEE = MathWidgetClassName()
self.present(subViewEE, animated: true, completion: nil)
But when I am trying to add it as a subview inside present view controller nothing shows up, here is the code:
let mathWidget= MathWidgetClassName()
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
Can anyone help to display MathWidget as a subview in present UIViewController?
you are creating viewcontroller programmatically then you need to set frame and background color of it like,
let mathWidget = MathWidgetClassName()
mathWidget.view.bounds = self.view.bounds
mathWidget.view.backgroundColor = UIColor.green // you should set white here , it is for demonstration
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
If you have view controller in storyboard then you should do like,
let mathWidget = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardID") //storyBoardID is Storyboard id - can be set from identity inspector of storyboard
// mathWidget?.view.bounds = self.view.bounds
// mathWidget?.view.backgroundColor = UIColor.green
self.addChildViewController(mathWidget!)
self.view.addSubview((mathWidget?.view)!)
mathWidget?.didMove(toParentViewController: self)
I have two XIB views loaded that are accessed through swipe gestures and would like to update my second XIB's textbox from my first XIB.
Here is the swift code I have so far but it appears that it doesn't work from one view to the other:
// This code is placed in ViewController0.xib
let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
vc1.resultsTextBox?.text = "test"
// vc1.asd(1) //Tried calling a function that's in that view but it didn't work either.
If resultsTextBox is a UITextField then it is likely not yet initialized. That is done in viewDidLoad. The better strategy is to set some string variable to "test". Then in viewDidLoad, set the resultsTextBox.text
let vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
vc1.textBoxStr = "test"
override func viewDidLoad() {
super.viewDidLoad()
vc1.resultsTextBox?.text = textBoxStr
...
}
Hope that makes sense.
I want to create a segue to from an object (label) inside a tableview cell to another view controller.
example: imagine the instagram feed tableview - tapping the number of likes under an image will push a second view with the list of people who liked an image.
How can i do that?
I know it's possible when tapping a cell but i need a solution for when tapping a label inside the cell...
thanks!
You have to make the following steps :
Define userInteractionEnabled = true for the label using Interface Builder or in code, as you wish, (VERY IMPORTANT!!) without this the tap is not enabled.
Define an action to the Tap Gesture Recognizer to handle the tap inside the label.
To present the another ViewController you can do one of the following two things :
Make a modal segue from the Tap Gesture Recognizer to the another ViewController using the Interface Builder or make an action for the Tap Gesture Recognizer and then present the another ViewController manually using the presentViewController function.
I thought the first is more easy to present the another ViewController, Is up to you.
I hope this help you.
You can simply use a button and set its title instead of using a label. If you have to use a UILabel for some reason, then you can do so by setting its userInteractionEnabled property to true and then adding a UITapGestureRecognizer to it.
You can use UITapGestureRecognizer.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblTxt: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
recognizer.numberOfTapsRequired = 1;
lblTxt.addGestureRecognizer(recognizer)
lblTxt.userInteractionEnabled = true;
}
func handleTap(recognizer: UITapGestureRecognizer){
println("tapped!")
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("secondView") as SecondViewController
self.presentViewController(secondViewController, animated:true, completion:nil)
}