How to hook up "Pop Up Button" event handler on selection update? - ios

I added a "Pop Up Button" from XCode Object Library, and wanted to hook it up with event handler that handles menu item selection update (for app's language selection).
The button is already created in *.xib file, and what kind of function should I create to hook up with button events?
I created a IBAction handler like this:
#IBOutlet weak var languageSettingButton: UIButton!
#IBAction func onLanguageSelected() {
// handling selection
// ...
}
But dragging the selector to this IBAction function did not work. What kind of selector function is it expecting? How do I hook it up?
My usage with two menu options regarding two app languages:
The Pop Up Button:
Update:
Followed #Charles Srstka's comment:
The IBAction func needs to have a sender param:
#IBAction func onLanguageSelected(_ sender: Any?) {
}
However the interesting thing is, we are able to hook up the entire button with the IBAction func, but not the menu item's selector, what should we do with the selector here? does it need to be hooked up with a function too?

Finally figured out the solution:
Based on Charles' comment, in view controller file, add a selection handler with sender param:
#IBAction func onSelectionUpdate(_ sender: Any?) {
// add logic here
}
In the pop up button's menu item's selector hook, drag it all the way to "File's owner", after dragging, a small menu will show available IBAction functions, choose the one we just created.

Related

How to connect UIButton to action using only interface builder

At the moment I am setting up my buttons for my keyboard with the following code:
func setupButtons() {
for subview in self.view.subviews {
if subview.isKindOfClass(UIButton) {
setupButton(subview as! UIButton)
}
}
}
func setupButton(btn: UIButton) {
btn.addTarget(self, action: #selector(KeyboardViewController.keyPressed(_:)), forControlEvents: .TouchUpInside)
}
But is there anyway I can skip this and all the target inside the layout builder so I can save a little bit of time looping through buttons on each keyboard view?
Sure, there are two ways to connect objects to Interface Builder, through outlets and actions.
class ViewController: UIViewController {
#IBOutlet weak var doStuffButton: UIButton!
#IBAction func doStuff(sender: UIButton) {
//do stuff
}
}
After adding that code look in interface builder and click on the view controller for this class.
Click on the connections inspector
Under outlets you will now see doStuffButton, however over the circle to the right of that, press control and click with your mouse, and drag it over to the button you want to connect it to and release. Setting outlets is NOT required for enabling actions. I just wanted to show this to you as well.
In the same pane you will also see received actions and the doStuff: method. Click and drag in the same way and release on the button. Select which type of event you want to process this action (normal is touch up inside).
Once you're all hooked up it should look like this:
There are many other variations of how to do this, but I thought this would get you a quick start.
If you're trying to ask how to do this without coding anything, just go into the assistant editor view of Xcode and Ctrl-drag from your button to the controllers class file. Then when the pop up displays, change outlet to action and give it a method name. This creates the IBAction method for you.
But in reality, the way you are doing it now with the for loop is far better. Especially if you have many buttons.

IBAction on button won't be called in custom keyboard

I am making a custom keyboard that has a search bar. In order for a user to be able to interact with it, they must be able to:
1- Input text with individual letter buttons.
2- Tap the search button.
As for number 1, I have working IBActions that are called at every button press. For example, here's the action for the letter "m":
#IBAction func mPressed(button: UIButton) {
searchBar.text! += "m"
}
However, the search button IBAction will not call:
#IBAction func searchPressed(button: UIButton) {
print("searchPressed")
}
When I connect the search button in storyboard to the "mPressed" action, that action is called. But when I reconnect it back to the "searchPressed" function above, it doesn't work again.
I have also made sure that I correctly connected the button to the action.
The search action is connected to the search button
The 'm' action is connected to the 'm' button
Thanks!
In your screenshot search button connected to method searchButtonPressed, but in code you have method searchPressed.
Delete connection to searchButtonPressed and reconnect to searchPressed and it will work.

How to create IBAction from UISegmentControl?

I am dealing with UISegmentControl in my project. I am able to create outlet & outlet collection of UISegmentControl with the help of drag & drop. But not able to create IBAction of Segment Control.
I think I am missing very small thing. Any Idea how should I proceed now?
For more clarity you can refer this screenshot.
Try this code in your controller :
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBAction func segmentedControl(sender: AnyObject) {
if segmentedControl.selectedSegmentIndex == 1 {
// Do something with the first button
}
else {
// Do something with the second button, etc...
}
}
Then drag it from your code to your segmentedControl
This is same as button actions the only difference is instead of selecting TouchUpInside or other button events you should use "ValueChanged"
UISegmentControll will respond to value changed action
You need to click assitant Navigator in the top corner right hand side to open Implementation file then drag segment control to implementation file after the #implementation directive that will pop the dialog screen.

Swift: 'Tap to Begin' button - delete itself

I have a tap to begin button in my main view controller. It is linked to an IBAction that will detect when it is touched. However, I am wondering what would be the best way of hiding the button: deleted, moved off screen, or hidden completely - and what code should I put in my IBAction to do this?
Thanks
The easiest way is to just hide it. Create an outlet connection so you can reference the button as an class instance property, then set its hidden property to true from the tap handler:
#IBOutlet private weak var btnBegin: UIButton!
#IBAction func didTapBegin() {
...
btnBegin.hidden = true
}

How do I connect custom keyboard to text field?

I created a custom keyboard using buttons, but now I'm stuck with buttons that don't do anything.
How will I connect them to a text field?
First, you should be aware of the inputView property of the UITextField/UITextView classes. Basically you can have a custom view (your keyboard for example) and swap it with the default keyboard.
You can get some inspiration from this tutorial that shows how to create a custom keyboard - and how to connect it to your textfield/textview.
You can also read this answer for another way to connect your keyboard to a textfield/textview.
You can handle buttons touchUpInside Action like this to insert text in uitextfield
#IBAction func btnQuestionAction(_ sender: UIButton)
{
self.textDocumentProxy.insertText("?")
}
And to delete character using custom keyboard, You can use this:
#IBAction func btnBackspaceAction(_ sender: UIButton)
{
self.textDocumentProxy.deleteBackward()
}
Hope it will help you :)

Resources