I am trying to add a UISegmentedControl to my app, but it seems that i always get [UISegmentedControl longValue]: unrecognised selector sent to instance 0x22f44e00
This is how i declare my UISegmentedControl
#IBOutlet var speedControl: UISegmentedControl
override func viewDidLoad() {
super.viewDidLoad();
self.speedControl.addTarget(self, action: "selectedSegmentDidChange:", forControlEvents: .ValueChanged);
self.speedControl.selectedSegmentIndex = 0;
}
func selectedSegmentDidChange(segmentedControl: UISegmentedControl) {
NSLog("method called");
}
How do i solve this?
I noticed none of my codes i wrote was causing the error, but it was the storyboard itself.
I right-clicked on the UISegmentControl, I noticed there were weird connections made to unknown source. I deleted them by pressing the "X", and everything is fine now
Related
I have read many tutorials and even the official Apple documentation and must not understand what is wrong with this code.
var dueDatePicker = UIDatePicker()
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.inputView = dueDatePicker
dueDatePicker.addTarget(self, action: #selector(datePickerValueChanged(_:)), for: UIControlEvents.valueChanged)
}
func datePickerValueChanged(_ sender: UIDatePicker){
//Do Stuff
}
At runtime, I click on the textField and the UIDatePicker appears. The function that the selector points to is executed. As soon as I click a UI object outside of the UIDatePicker, the app crashes with this error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[YourApp.PromiseViewController
dueDateChanged:]: unrecognized selector sent to instance 0x100b12ae0'
What I don't understand is that the "selector" or pointer to the desired function is recognized initially. However, when I trigger another event from another UI Object this exception is thrown.
Why is this happening?
Shouldn't this exception be triggered when datePickerValueChanged() is called initially?
Just add #objc in front of your function
#objc func datePickerValueChanged(_ sender: UIDatePicker){
//Do Stuff
}
The error is telling you that an action with the selector dueDateChanged(_:) has been added as a target action.
More than one target action can be added to a control. Somewhere, maybe in your storyboard or xib, you have another action added to dueDatePicker.
I've spent 2 days trying to get a single tap to work on a single view (clicking anywhere in the screen). I've tried every variation of fixing this problem I could find. Inside the DVC Class, with #IBActions on everything, renaming the view as an #IBAction etc. I can't get any other error except : "[UIView score:]: unrecognized selector sent to instance"
class DataViewController: UIViewController, UIGestureRecognizerDelegate {
var tappy = UITapGestureRecognizer()
override func viewDidLoad() {
self.tappy = UITapGestureRecognizer(target:self.view, action:"score:")
self.tapView!.addGestureRecognizer(self.tappy)
}
}
func score(sender:UITapGestureRecognizer!) throws {
print("tapped")
}
One problem is that your action: selector, "score:", doesn't correspond to what Objective-C sees when you declare your method as func score(sender:UITapGestureRecognizer!) throws. It sees "score:error:". The simplest solution is to delete throws, since "score:error:" cannot be an action method signature for a tap gesture recognizer.
Moreover, as #dan has pointed out, score is not in self.view but in self. So you also need to change your target:.
and thanks in advance for taking the time to help.
Inside my CellForRowAtIndexPath, I have the following line:
cell.timeView.addTarget(self, action: "ButtonDidPress:", forControlEvents: UIControlEvents.TouchUpInside)
and my selector function is:
func ButtonDidPress (sender: DesignableView!){
let view:DesignableView = sender
cell.timeView.shadowColor = UIColor.yellowColor()
table.reloadData()
}
and the error i get is:
unrecognized selector sent to instance
I'm thinking that perhaps one can't send a View as a selector (am I using the correct terminology?), but how else can I target that particular view in that cell?
UPDATE:
I also tried using gestureRecognizer instead:
var tap = UIGestureRecognizer(target: self, action: Selector( "viewDidTap:"))
cell.timeView.addGestureRecognizer(tap)
and
func viewDidTap (sender: DesignableView!){
but I got the same error here.
Thanks!
There's a couple of strange things happening in your code. It seems you want to change the shadowColor property of timeView when a user touch it, right?
Two possible solutions are:
(This one is IMO the better one) Change DesignableView to inherit from UIButton. Then you can set:
timeView.addTarget(self, action: "ButtonDidPress:", forControlEvents: .TouchUpInside). Make sure you set it just once for each cell. Otherwise you will get multiple calls on one tap.
Use UITapGestureRecognizer, but you should put it in your UITableViewCell subclass, not to the view controller. Also, the sender in viewDidTap is not the view itself, but the recognizer. So the method will go like this:
func viewDidTap(sender: UITapGestureRecognizer) {
let location = sender.locationInView(sender.view)
if timeView.hitTest(location, withEvent: nil) == timeView {
timeView.shadowColor = UIColor.yellowColor()
// table.reloadData() - you don't need to reload the table
}
}
I am creating a post to twitter app and want to add various text to the UITextField from buttons that can be selected. The UIButton are for various hashtags to speed the process of writing a tweet. I have tried a couple the solutions found on Stack but none seem to work and they are all in Objective-C.
Screenshot of compose tweet view before buttons touched.
http://postimg.org/image/5qoyk6673/
Screenshot of compose tweet view after button selected and text added to text field.
http://postimg.org/image/vp08wsa6b/fa7c7a83/
class TweetComposeViewControler: UIViewController, UITextViewDelegate {
var selectedAccount : ACAccount!
#IBOutlet var tweetContent: UITextView!
#IBAction func specializedButton(sender: UIButton) {
tweetContent.text = sender as UIButton
tweetContent.text.stringByAppendingString(specializedButton(titleLabel.text))
}
func insertHashtag(sender: UIButton!) {
tweetContent.text = tweetContent.text.stringByAppendingString(sender.titleLabel.text)
}
You can set the UITextFields text to the title of your UIButton:
txtField.text = hashTagButton.titleLabel.text
And if you want to append the text:
txtField.text = textField.text.stringByAppendingString(hashTagButton.titleLabel.text)
In order to have your buttons update the text upon pressing them you need to add a target selector for when the buttons are pressed. This can be done via Storyboard or programmatically.
Programmatically
You would add the same selector to all buttons using this:
hashtagButton.addTarget(self, action: Selector("insertHashtag:"), forControlEvents: .TouchUpInside)
This will call the insertHashtag function whenever the hashtagButton is pressed. Because of the : at the end of the selector, it will also pass itself as a parameter so you can use it to get the button's title rather than creating a different selector for each button.
func insertHashtag(sender: UIButton!) {
txtField.text = textField.text.stringByAppendingString(sender.titleLabel!.text)
}
Using IBAction
#IBAction func insertHashtag(sender: AnyObject) {
txtField.text = sender as UIButton
textField.text.stringByAppendingString(btn.titleLabel!.text)
}
Here you cast the sender paramter as a UIButton since you know a UIButton is the type of the object which called it.
If you know that only UIButton's will cause this method you can do this:
#IBAction func insertHashtag(btn: UIButton ) {
txtField.text = sender as UIButton
textField.text.stringByAppendingString(btn.titleLabel!.text)
}
Response to your update
The code you added needs to be fixed to look like this:
#IBAction func specializedButton(sender: UIButton) {
tweetContent.text.stringByAppendingString(sender.titleLabel!.text)
}
Here sender is your UIButton and you pass it as a UIButton so you do not need to convert it.
I'm writing some unit tests and, because of the nature of this particular app, it's important that I get as high up the UI chain as possible. So, what I'd like to do is programmatically trigger a button-press, as if the user had pressed the button in the GUI.
(Yes, yes -- I could just call the IBAction selector but, again, the nature of this particular app makes it important that I fake the actual button press, such that the IBAction be called from the button, itself.)
What's the preferred method of doing this?
It turns out that
[buttonObj sendActionsForControlEvents:UIControlEventTouchUpInside];
got me exactly what I needed, in this case.
EDIT: Don't forget to do this in the main thread, to get results similar to a user-press.
For Swift 3:
buttonObj.sendActions(for: .touchUpInside)
An update to this answer for Swift
buttonObj.sendActionsForControlEvents(.TouchUpInside)
EDIT: Updated for Swift 3
buttonObj.sendActions(for: .touchUpInside)
Swift 3:
self.btn.sendActions(for: .touchUpInside)
If you want to do this kind of testing, you’ll love the UI Automation support in iOS 4. You can write JavaScript to simulate button presses, etc. fairly easily, though the documentation (especially the getting-started part) is a bit sparse.
In this case, UIButton is derived from UIControl. This works for object derived from UIControl.
I wanted to reuse "UIBarButtonItem" action on specific use case. Here, UIBarButtonItem doesn't offer method sendActionsForControlEvents:
But luckily, UIBarButtonItem has properties for target & action.
if(notHappy){
SEL exit = self.navigationItem.rightBarButtonItem.action;
id world = self.navigationItem.rightBarButtonItem.target;
[world performSelector:exit];
}
Here, rightBarButtonItem is of type UIBarButtonItem.
For Xamarin iOS
btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);
Reference
Swift 5:
class ViewController: UIViewController {
#IBOutlet weak var theTextfield: UITextField!
#IBOutlet weak var someButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
theTextfield.text = "Pwd"
someButton.sendActions(for: .touchUpInside)
}
#IBAction func someButtonTap(_ sender: UIButton) {
print("button tapped")
}
}
It's handy for people who write Unit Tests without UI Tests ;-)
Swift 5 way to solve it for UIBarButtonItem, which does not have sendAction method like UIButton etc.
extension UIBarButtonItem {
func sendAction() {
guard let myTarget = target else { return }
guard let myAction = action else { return }
let control: UIControl = UIControl()
control.sendAction(myAction, to: myTarget, for: nil)
}
}
And now you can simply:
let action = UIBarButtonItem(title: "title", style: .done, target: self, action: #selector(doSomething))
action.sendAction()
Swift 4:
self .yourButton(self)