My app crashes and throws this error
[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e0
2016-08-04 17:07:15.569 [3809:1375151]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e0'
*** First throw call stack:
(0x180d72db0 0x1803d7f80 0x180d79c4c 0x180d76bec 0x180c74c5c 0x1000e681c 0x1000e6a60 0x185f08be8 0x185f08b64 0x185ef0870 0x185f11360 0x185f07ed8 0x185f00c20 0x185ed104c 0x185ecf628 0x180d2909c 0x180d28b30 0x180d26830 0x180c50c50 0x182538088 0x185f3a088 0x1000f5c88 0x1807ee8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm trying to make a custom class for hide and show password so that I can use it in any UITextField in any UIViewController.
class HideShowIcon:NSObject {
var showPasswordImage = UIImage(named: "ic_show_password") as UIImage?
var hidePasswordImage = UIImage(named: "ic_hide_password") as UIImage?
func hideShowPasswordButton(hideText:UITextField) {
var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
var hideShow: UIButton = UIButton(type: UIButtonType.System)
hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: hideText.frame.size.height)
hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
hideText.rightView = hideShow
hideText.rightViewMode = UITextFieldViewMode.Always
hideShow.addTarget(self, action: #selector(HideShowIcon.hideShowPasswordTextField(_:hideText:)), forControlEvents: UIControlEvents.AllTouchEvents)
}
func hideShowPasswordTextField(sender: AnyObject,hideText:UITextField) {
var hideShow: UIButton = (hideText.rightView as? UIButton)!
if !hideText.secureTextEntry {
hideText.secureTextEntry = true
hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
} else {
hideText.secureTextEntry = false
hideShow.setImage(showPasswordImage, forState: UIControlState.Normal)
}
hideText.becomeFirstResponder()
}
}
You can not use selector with many parameters hideShowPasswordTextField(_:hideText:) for UIButton target. You can only use selector with one (or zero) parameter, in which UIButton will put itself: hideShowPasswordTextField(_:)
func hideShowPasswordTextField(sender: UIButton) {
//...
}
To use hideText in this function, you may declare it as property of your class:
var hideText: UITextField!
and assign value to it in class initialization step
Related
I'm trying to create an action that will underline a button when pressed by replacing its current title with an attributed string. However, this keeps resulting in an "unrecognized selector sent to instance" error. I've connected one IBAction to this button, and created an IBOutlet weak variable referring to it. To my knowledge these should not interfere with one another.
Here is my code for the action connected to the button
#IBAction func voButtonTapped(_ sender: Any) {
let voString = "vo"
let attributedvo = NSAttributedString(string: voString, attributes: underlineAttribute)
vButton.setAttributedTitle(attributedvo, for: .normal)
}
and this is the error below:
[__SwiftValue set]: unrecognized selector sent to instance 0x600001584510
underlinedAttribute:
let underlineAttribute: [NSAttributedString.Key: Any] = [
.font: UIFont(name: "Rockwell-Bold", size: 35) as Any,
.foregroundColor: ColorManager.specialYellow,
.underlineStyle: NSUnderlineStyle.single.rawValue]
vButton:
#IBOutlet weak var vButton: UIButton!
ColorManager:
struct ColorManager {
static let specialYellow = Color("Special Yellow")
}
The colorManager struct was causing the issue.
I had to change it from
static let specialYellow = Color("special yellow")
to
static let specialYellow = UIcolor(red: 0.85, green: 0.68, blue: 0.00, alpha: 1.00)
so essentially I couldn't use my original saved color and had to find a similar one from a hex pallet, then I used an online converter to convert it to a UIcolor
Why is this code causing a SIGABT error?
#IBAction func showDial(_ sender: Any) {
var dialBottom:CGFloat = lcDialBottom!.constant
if dialBottom == -600 {
//move up
dialBottom = -400
} else {
//move down
dialBottom = -600
}
UIView.animate(withDuration: 0.3,
animations: {
self.lcDialBottom.constant = dialBottom
self.view.layoutIfNeeded()
self.view.updateConstraintsIfNeeded()
self.btnShowDial.alpha = 0.0
})
}
Error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSLayoutConstraint setAlpha:]: unrecognized selector sent to instance 0x600002ff8d70'
I understand what the error is saying, NSLayoutConstraint does not have a setAlpha method, but I'm not calling it on a NSLC, I'm calling set alpha on a UIButton, which does have a setAlpha.
Here's my properties:
#IBOutlet weak var btnShowDial: UIButton!
#IBOutlet weak var lcDialBottom: NSLayoutConstraint!
Check connection of btnShowDial in storyboard. Maybe you connected this IBOutlet with layout constraint.
If I create a GestureRecognizer handler through InterfaceBuilder, I get a method that looks like this:
#IBAction func Tap(tap: UITapGestureRecognizer) {
var touchLocation = tap.locationInView(self.view)
imageView.transform.tx = touchLocation.x
imageView.transform.ty = touchLocation.y
}
//works great
If I manually create a GestureRecognizer, I have to create a function with no arguments, like this:
let tap = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
let imageSize = CGSize(width: 100, height: 100)
var iView = UIImageView(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: imageSize))
self.view.addSubview(iView)
let image2 = UIImage(named: "square.png");
iView.image = image2
iView.userInteractionEnabled = true
iView.addGestureRecognizer(tap)
tap.addTarget(self, action: "Tap")
}
func Tap( //why can't I put an argument in here?// ){
let tapAlert = UIAlertController(title: "Tap Pressed", message: "You just tapped", preferredStyle: .Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
}
//works but I've lost access to the sending object
If I add an argument to the Tap handler
func Tap(sender: UITapGestureRecognizer){
}
//crashes: [tapHandler]: unrecognized selector sent to instance
, it crashes on the Tap. I'd prefer to have a method with access to the sender object. Is there a way to manually create a GestureRecognizer handler and have access to the sender object, the same way you can when you do it through the Interface Builder?
Selector names are something that Swift inherits from Objective C. In Objective C a selector with single argument has name selector: as opposite to just selector, which means - no arguments. Change the name of the action in addTarget from Tap to Tap: and it should work then.
I want to add target to multiple buttons with sender parameter in Swift but I'm facing a problem
Here's my code
var index = 10
var btn = [UIButton]()
for var i = 0; i<=index; i++
{
btn.append(UIButton(frame : ... ))
btn[i].addTarget(self, action: "event_clicked", forControlEvents: UIControlEvents.TouchUpInside)
}
func event_clicked(sender : UIButton!)
{
sender.backgroundColor = UIColor.whiteColor()
}
it runs well but when I press one of the buttons, the error "libc++abi.dylib: terminating with uncaught exception of type NSException" shows. Please help me with this.
I found the answer by myself. Must write the selecter name "event_name:". Has to put the colon at the end of the selecter name.
Hey guys i am using the xcode6 beta 4.
Im trying to to give a button a size with the argument .frame but i got an expected declaration error. I hope you can tell me whats wrong with my code and why i get this error!
var button1 = UIButton.buttonWithType(UIButtonType.System) as UIButton
button1.frame = CGRectMake(100, 100, 40, 145) //Expected declaration
button1.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside)
menuView.addSubview(button)
You might be mistaken as to which line is throwing the error.
Look at line 4 in what you shared.
menuView.addSubview(button)
Shouldn't that be button1, not button?
menuView.addSubview(button1)
EDIT
As mentioned in my comment, you might be trying to access the button1 declaration inside the class declaration, all logic must be within functions. Only variable declarations can be outside of functions.
class Foo {
var menuView = UIView()
var button1: UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
button1.frame = CGRectMake(100, 100, 40, 145)
button1.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside)
menuView.addSubview(button1)
}
The above is not ok, you can declare the button1 variable at the class level but you cannot start accessing it until you're inside a function like below.
class Foo {
var menuView = UIView()
var button1: UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
func setupButton1() {
button1.frame = CGRectMake(100, 100, 40, 145)
button1.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside)
menuView.addSubview(button1)
}
}