i'm a beginner in swift and i have been using this dismissKeyboard() method but it is not working for the keyboard extension.
#IBAction func donePressed (sender: UIButton) {
dismissKeyboard()
}
can anyone tell me why this doesn't work?
thanks.
EDIT: full code
import UIKit
class KeyboardViewController: UIInputViewController {
var keyboardView: UIView!
#IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadInterface()
}
func loadInterface() {
var keyboardNib = UINib(nibName: "KeyboardView", bundle: nil)
self.keyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
view.addSubview(self.keyboardView)
view.backgroundColor = self.keyboardView.backgroundColor
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput) {
// The app has just changed the document's contents, the document context has been updated.
}
#IBAction func buttonPressed (sender: UIButton) {
let title = sender.titleForState(.Normal)
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText(title!)
}
#IBAction func spacePressed (sender: UIButton) {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText(" ")
}
#IBAction func deletePressed (sender: UIButton) {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.deleteBackward()
}
#IBAction func donePressed (sender: UIButton) {
resignFirstResponder()
}
}
Try like that
self.dismissKeyboard()
Maybe try :
view.endEditing(true)
Dismisses the custom keyboard from the screen.
Swift:
self.dismissKeyboard()
Objective-C:
[self dismissKeyboard];
https://developer.apple.com/documentation/uikit/uiinputviewcontroller/1618196-dismisskeyboard?language=objc
Related
import UIKit
class ViewController: UIViewController {
// #IBAction func Btndel(_ sender: Any) {
//}
var Str:String?
override func viewDidLoad() {
super.viewDidLoad()
let items = [Str]
let SegM = UISegmentedControl(items:items as Any as? [Any])
SegM.selectedSegmentIndex = 0
SegM.frame=CGRect(x: 70, y: 130, width: 100, height: 50)
SegM.layer.cornerRadius = 8.0
SegM.backgroundColor = .orange
SegM.tintColor = .white
self.view .addSubview(SegM)
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func Btnadd(_ sender: Any)
{
var Str = 0;Str += 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
How to add and delete segments in a viewcontroller by clicking add button an delete button created in the same viewcontroller
How to insert and remove(add or delete) segments in swift4
class ViewController: UIViewController {
#IBOutlet weak var segment1: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func insert(_ sender: Any) {
segment1.insertSegment(withTitle: "\(segment1.numberOfSegments+1)", at: segment1.numberOfSegments, animated: true)
}
#IBAction func remove(_ sender: Any) {
segment1.removeSegment(at: segment1.numberOfSegments-1, animated: true)
}
}
You can insert segment by insertSegment method of UISegmentedControl and you can delete segment by removeSegment method. Let me take an example.
I create segmentController class and its UI in a storyboard.
Below is UI screenshot. In the storyboard, You can see two buttons Insert (+) and Remove (-) and UISegmentedControl. Insert button will insert segment at a specific position and Remove button will remove the segment at a specific position.
Below is code of segmentController class.
class segmentController: UIViewController {
#IBOutlet weak var segementControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func remove(_ sender: Any) {
segementControl.removeSegment(at: segementControl.numberOfSegments-1, animated: true)
}
#IBAction func insert(_ sender: Any) {
segementControl.insertSegment(withTitle: "\(segementControl.numberOfSegments+1)", at: segementControl.numberOfSegments, animated: true)
}
}
In the above code, on insert button click new segment will add at the last of segementControl. On remove button click the last segment will delete from segmentControl.
Hope it helps.
I have created a custom view xib and give that view class. Now I take a view in main vc and give that class but now I want to access custom view button action method in my main vc. So how can I do that?
Here is my custom view
import UIKit
class TextCustomisationVC: UIView {
#IBOutlet var contentView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit(){
Bundle.main.loadNibNamed("TextCustomisationVC", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
#IBAction func btnCloseCustomisation_Click(_ sender: Any) {
}
#IBAction func btnApplyCustomisation_Click(_ sender: Any) {
}
}
Now I create an outlet in my main VC and give that same class I can access those class outlets but now I want to access above button action method So how can I do that?
You can use delegate here which you can implement in the main VC.
create a protocol like this:
protocol ButtonActionDelegate {
func closeButtonPressed(_ sender:UIButton)
func applyButtonPressed(_ sender:UIButton)
}
Then create instance of the delegate in your view like this:
var delegate:ButtonActionDelegate?
Implement this delegate in the main VC like this:
extension mainVC : ButtonActionDelegate {
func closeButtonPressed(_ sender: UIButton) {
}
func applyButtonPressed(_ sender: UIButton) {
}
}
Then you can call the delegate methods respectively like this:
#IBAction func btnCloseCustomisation_Click(_ sender: Any) {
self.delegate?.closeButtonPressed(sender)
}
#IBAction func btnApplyCustomisation_Click(_ sender: Any) {
self.delegate?.applyButtonPressed(sender)
}
You can try
let cusView = TextCustomisationVC(frame:///)
if btn sender is used inside function
cusView.btnCloseCustomisation_Click(cusView.closeBtn)
otherwise send any dummy button
cusView.btnCloseCustomisation_Click(UIButton())
Edit:
protocol CustomTeller {
func closeClicked(UIButton)
}
class TextCustomisationVC: UIView {
var delegate: CustomTeller?
#IBAction func btnCloseCustomisation_Click(_ sender: UIButton) {
self.delegate?.closeClicked(sender:sender)
}
}
// in mainVC
let cusView = TextCustomisationVC(frame:///)
cusView.delegate = self
and implement
func closeClicked(sender:UIButton) {
// close button called
}
I am a beginner of xcode programming. I am trying to do an action, when i click button from A view, the button of B view will be hidden. I already know i can use button.hidden = true; for self view controller but I don't know how to control button from other view.
Thanks
#IBAction func TestBut(sender: UIButton) {
setting.hidden = false
}
Before, you create a custom view with button, button action and a protocol as:
protocol CustomViewDelegate {
func buttonPressed (sender: AnyObject)
}
class CustomView: UIView {
#IBOutlet weak var button: UIButton!
var delegate: CustomViewDelegate!
override func awakeFromNib() {
super.awakeFromNib()
}
class func loadViewFromXib() -> CustomView {
return NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as! CustomView
}
#IBAction func buttonPressed(sender: AnyObject) {
self.delegate.buttonPressed(sender)
}
}
In your ViewController.
class ViewController: UIViewController, CustomViewDelegate {
var firstView: CustomView?
var secondView: CustomView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.firstView = CustomView.loadViewFromXib()
self.secondView = CustomView.loadViewFromXib()
firstView!.frame = CGRectMake(0, 0, 100, 100)
secondView!.frame = CGRectMake(0, 200, 100, 100)
firstView!.delegate = self
secondView!.delegate = self
self.view.addSubview(firstView!)
self.view.addSubview(secondView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func buttonPressed(sender: AnyObject) {
if (sender as! UIButton) == self.firstView!.button {
self.secondView?.button.hidden = true
}else {
self.firstView?.button.hidden = true
}
}
}
the button has to be a property of the view( or view controller).
A call would look like this:
view.button.hidden = true
The purpose for my app
I have two ViewControllers,FrontSideViewController and BackSideViewController, in my little project.
I am about to flipping FrontSideViewController to BackSideViewController through using CATransition while each ViewController's data can be saved.
Describe of Problem
The views transited in the unexpected way which animation is not seem like the UIAnimationTransition.FlipFromLeft(or)Right thing.
After flipping views, the previous view's data can not be saved, which means when I flipped back the old view, its tiny data (something like textField's text) is just gone.
My Code
Same Logical function to flip between the views which be used in two Classes.
FrontSideViewController.swift
class FrontSideViewController: UIViewController {
var frontSideViewController: FrontSideViewController?
var backSideViewController: BackSideViewController?
var frontSide: Bool = true
#IBOutlet weak var flip_1Button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
frontSideViewController = storyboard?.instantiateViewControllerWithIdentifier("FrontSideViewController") as? FrontSideViewController
backSideViewController = storyboard?.instantiateViewControllerWithIdentifier("BackSideViewController") as? BackSideViewController
flip_1Button.addTarget(self, action: "flipViews", forControlEvents: UIControlEvents.TouchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()
}
func flipViews() {
UIView.beginAnimations("ViewSwitch", context: nil)
UIView.setAnimationDuration(0.6)
UIView.setAnimationCurve(.EaseInOut)
UIView.setAnimationTransition(.FlipFromLeft, forView: view, cache: true)
backSideViewController?.view.frame = self.view.frame
frontSideViewController?.willMoveToParentViewController(nil)
frontSideViewController?.view.removeFromSuperview()
frontSideViewController?.removeFromParentViewController()
self.addChildViewController(backSideViewController!)
self.view.addSubview(backSideViewController!.view)
backSideViewController?.didMoveToParentViewController(self)
}
}
BackSideViewController.swift
class BackSideViewController: UIViewController {
var frontSideViewController: FrontSideViewController?
var backSideViewController: BackSideViewController?
#IBOutlet weak var flip_2Button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
frontSideViewController = storyboard?.instantiateViewControllerWithIdentifier("FrontSideViewController") as? FrontSideViewController
backSideViewController = storyboard?.instantiateViewControllerWithIdentifier("BackSideViewController") as? BackSideViewController
flip_2Button.addTarget(self, action: "flipViews", forControlEvents: UIControlEvents.TouchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func flipViews() {
UIView.beginAnimations("ViewSwitch", context: nil)
UIView.setAnimationDuration(0.6)
UIView.setAnimationCurve(.EaseInOut)
UIView.setAnimationTransition(.FlipFromRight, forView: view, cache: true)
frontSideViewController?.view.frame = self.view.frame
backSideViewController?.willMoveToParentViewController(nil)
backSideViewController?.view.removeFromSuperview()
backSideViewController?.removeFromParentViewController()
self.addChildViewController(frontSideViewController!)
self.view.addSubview(frontSideViewController!.view)
frontSideViewController?.didMoveToParentViewController(self)
}
Please teach me how solve those two problems.
Thanks for your help.
Ethan Joe
I have an image view and I needed the user swipe this image down but i don't know how to do this. I put a log to know if the swiping its working and its working well, i want that the image moves when I swipe. I'm very new in iOS. Can you help me??
Here is my ViewController class:
class MainScreenViewController: UIViewController {
#IBOutlet var buttonMenuEntry: UIButton
#IBOutlet var buttonMenuExit: UIButton
#IBOutlet var mainscreenEntryView: UIImageView
#IBOutlet var mainscreenCard: UIImageView
override func viewDidLoad() {
super.viewDidLoad()
var swipeCardDown = UISwipeGestureRecognizer(target: self, action: "respondToGesture:")
swipeCardDown.direction = UISwipeGestureRecognizerDirection.Down
self.mainscreenCard.addGestureRecognizer(swipeCardDown)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonMenuClicked(sender: UIButton){
buttonMenuEntry.hidden = true
buttonMenuExit.hidden = false
}
func respondToGesture(gesture: UIGestureRecognizer){
println("Swiped down")
}
}
Thanks.
Simplest way would be:
func respondToGesture(gesture: UIGestureRecognizer){
UIView.animateWithDuration(0.35, animations: {
mainscreenCard.frame = CGRectMake(mainscreenCard.frame.origin.x,
self.view.frame.height,
mainscreenCard.frame.size.width,
mainscreenCard.frame.size.height)
})
}
Adjust your timing as you wish