swift Turn a generated imageView into a UIButton in code - ios

I am trying to add a tap gesture to a generated Image and it works ok but keeps crashing the program once I click on the image. What did I do wrong here. Any help is appreciated.
let tapGesture = UITapGestureRecognizer(target: self, action: "image Tapped")
let imageView = UIImageView(image: UIImage(data: data));
imageView.addGestureRecognizer(tapGesture)
imageView.userInteractionEnabled = true
This is the error I get
2016-07-12 09:19:30.241 XXX[2170:27661] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ‘-[MyViewController image Tapped]: unrecognized selector sent to instance 0x7fe811e30560'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e2a7d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000011144ddeb objc_exception_throw + 48
2 CoreFoundation 0x000000010e2b0d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010e1f6cfa ___forwarding___ + 970
4 CoreFoundation 0x000000010e1f68a8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000110391b28 _UIGestureRecognizerSendTargetActions + 153
6 UIKit 0x000000011038e19a _UIGestureRecognizerSendActions + 162
7 UIKit 0x000000011038c197 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
8 UIKit 0x0000000110394655 ___UIGestureRecognizerUpdate_block_invoke898 + 79
9 UIKit 0x00000001103944f3 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
10 UIKit 0x0000000110381e75 _UIGestureRecognizerUpdate + 2634
11 UIKit 0x000000010ff0e48e -[UIWindow _sendGesturesForEvent:] + 1137
12 UIKit 0x000000010ff0f6c4 -[UIWindow sendEvent:] + 849
13 UIKit 0x000000010febadc6 -[UIApplication sendEvent:] + 263
14 UIKit 0x000000010fe94553 _UIApplicationHandleEventQueue + 6660
15 CoreFoundation 0x000000010e1cd301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x000000010e1c322c __CFRunLoopDoSources0 + 556
17 CoreFoundation 0x000000010e1c26e3 __CFRunLoopRun + 867
18 CoreFoundation 0x000000010e1c20f8 CFRunLoopRunSpecific + 488
19 GraphicsServices 0x0000000112d68ad2 GSEventRunModal + 161
20 UIKit 0x000000010fe99f09 UIApplicationMain + 171
21 ApivitaClient 0x000000010d6a1482 main + 114
22 libdyld.dylib 0x0000000111f1192d start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

replace
let tapGesture = UITapGestureRecognizer(target: self, action: "image Tapped")
with
let tapGesture = UITapGestureRecognizer(target:self, action: #selector(imageTapped))
and create func imageTapped() {}

The action part in the UITapGestureRecognizer initializer means which method will be executed. And the action part means who's responsible for that action, who's the delegate.
Use this to initialize the gesture recognizer:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
Please note the colon : after the name of the method (imageTapped). This means the gesture recognizer will call the imageTapped method and pass the sender parameter, which it will be useful if you want to determine which gesture recognizer was triggered. This is really useful if you have several images that can be tapped, so you don't have to build a method for each one.
Then you need to declare the imageTapped method:
func imageTapped(sender: UIGestureRecognizer) {
print("Image tapped")
}
You can declare the function to work without parameter as well, just declare the function without the sender parameter and delete the colon in the action selector. Anyway, I recommend to keep it, it may be useful somewhere and it's better if you learn it that way.

Related

Error while Changing the image of the button from an image to another when clicked on the button

Here is my code :
#IBAction func moreClicked(_ sender: UIButton) {
if sender.currentImage == #imageLiteral(resourceName: "more_off"){
sender.setImage(#imageLiteral(resourceName: "more_on"), for: .normal)
}
else
{
sender.setImage(#imageLiteral(resourceName: "more_off"), for: .normal)
}
}
I am getting this error:
2018-09-05 11:03:28.708661+0530 musicPlayer[25450:590989]
-[musicPlayer.SecondViewController more:]: unrecognized selector sent to instance 0x7fc1d4606d00
2018-09-05 11:03:28.715659+0530 musicPlayer[25450:590989] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'-[musicPlayer.SecondViewController more:]: unrecognized selector sent
to instance 0x7fc1d4606d00'
* First throw call stack: ( 0 CoreFoundation 0x00000001087ea1e6 exceptionPreprocess + 294 1 libobjc.A.dylib
0x0000000107a36031 objc_exception_throw + 48 2 CoreFoundation
0x000000010886b784 -[NSObject(NSObject) doesNotRecognizeSelector:] +
132 3 UIKit 0x0000000108e956db
-[UIResponder doesNotRecognizeSelector:] + 295 4 CoreFoundation 0x000000010876c898 ___forwarding_ + 1432 5 CoreFoundation
0x000000010876c278 _CF_forwarding_prep_0 + 120 6 UIKit
0x0000000108c683e8 -[UIApplication sendAction:to:from:forEvent:] + 83
7 UIKit 0x0000000108de37a4
-[UIControl sendAction:to:forEvent:] + 67 8 UIKit 0x0000000108de3ac1 -[UIControl _sendActionsForEvents:withEvent:] + 450
9 UIKit 0x0000000108de2a09
-[UIControl touchesEnded:withEvent:] + 580 10 UIKit 0x0000000108cdd0bf -[UIWindow _sendTouchesForEvent:] + 2729 11 UIKit
0x0000000108cde7c1 -[UIWindow sendEvent:] + 4086 12 UIKit
0x0000000108c82310 -[UIApplication sendEvent:] + 352 13 UIKit
0x00000001095c36af dispatchPreprocessedEventFromEventQueue + 2796
14 UIKit 0x00000001095c62c4
__handleEventQueueInternal + 5949 15 CoreFoundation 0x000000010878cbb1
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 16 CoreFoundation 0x00000001087714af
__CFRunLoopDoSources0 + 271 17 CoreFoundation 0x0000000108770a6f __CFRunLoopRun + 1263 18 CoreFoundation
0x000000010877030b CFRunLoopRunSpecific + 635 19 GraphicsServices
0x000000010ff76a73 GSEventRunModal + 62 20 UIKit
0x0000000108c67057 UIApplicationMain + 159 21 musicPlayer
0x000000010711cc27 main + 55 22 libdyld.dylib
0x000000010cd4f955 start + 1 ) libc++abi.dylib: terminating with
uncaught exception of type NSException
2018-09-05 11:03:28.708661+0530 musicPlayer[25450:590989]
-[musicPlayer.SecondViewController more:]: unrecognized selector sent to instance 0x7fc1d4606d00
The above message means that SecondViewController expects to have method called more: but when the action is triggered and instance from this class tries to find the more: method, it does not recognise such method and the application crashes.
What I think has happened here is: you have dragged action from your Xib/Storyboard to your VC class and named it more:. After this you have renamed it manually (not Refactor > Rename) to moreClicked:. This way the IBAction of the SecondViewController still points to method that is called more:, however your class does not have such method anymore.
So basically, you need to remove the already existing reference of the IBAction from your xib/storyboard and reconnect it with the new method: moreClicked:, and all should be working.

Swift Swipe gesture recognizer crashes program [duplicate]

This question already has answers here:
UISwipeGestureRecognizer throw unrecognised selector sent error
(2 answers)
Closed 5 years ago.
Hello I added a swift gesture recognizer and it keeps crashing my program
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("handleSwipes:")))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}
func handleSwipes(sender: UISwipeGestureRecognizer) {
if (sender.direction == .left ) {
headerLabel.text = "Zodiac Yearly"
}
if (sender.direction == .right ) {
headerLabel.text = "Characteristics"
}
}
This is the error I am getting
2017-09-08 20:17:33.355 Views[40942:2309262] -[Views.ViewController handleSwipes:]: unrecognized selector sent to instance 0x7fdd9bc0dc90
2017-09-08 20:17:33.361 Views[40942:2309262] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Views.ViewController handleSwipes:]: unrecognized selector sent to instance 0x7fdd9bc0dc90'
*** First throw call stack:
(
0 CoreFoundation 0x0000000111f40b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010f1e6141 objc_exception_throw + 48
2 CoreFoundation 0x0000000111fb0134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000111ec7840 ___forwarding___ + 1024
4 CoreFoundation 0x0000000111ec73b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010fbecf59 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
6 UIKit 0x000000010fbf4d57 _UIGestureRecognizerSendTargetActions + 109
7 UIKit 0x000000010fbf270b _UIGestureRecognizerSendActions + 225
8 UIKit 0x000000010fbf19ce -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 981
9 UIKit 0x000000010fbde152 _UIGestureEnvironmentUpdate + 1219
10 UIKit 0x000000010fbddc43 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
11 UIKit 0x000000010fbdce0a -[UIGestureEnvironment _updateGesturesForEvent:window:] + 274
12 UIKit 0x000000010f728eea -[UIWindow sendEvent:] + 4092
13 UIKit 0x000000010f6d5a84 -[UIApplication sendEvent:] + 352
14 UIKit 0x000000010feb95d4 __dispatchPreprocessedEventFromEventQueue + 2926
15 UIKit 0x000000010feb1532 __handleEventQueue + 1122
16 CoreFoundation 0x0000000111ee6c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x0000000111ecc0cf __CFRunLoopDoSources0 + 527
18 CoreFoundation 0x0000000111ecb5ff __CFRunLoopRun + 911
19 CoreFoundation 0x0000000111ecb016 CFRunLoopRunSpecific + 406
20 GraphicsServices 0x0000000113ed7a24 GSEventRunModal + 62
21 UIKit 0x000000010f6b8134 UIApplicationMain + 159
22 Views 0x000000010ec06a67 main + 55
23 libdyld.dylib 0x0000000112f6c65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(sender:)))
#objc func handleSwipes(sender: UISwipeGestureRecognizer) {
// ...
}
This should work in Swift 3 and 4.

Have multiple buttons have the same function?

I am creating an app where I need a button that generates more buttons. I have my code like this:
func showButtons(array:Array<UIButton>)
{
for(var i:Int = 0; i < array.count; i++)
{
array[i].setImage(UIImage(named: "StartButton.png"), forState: UIControlState.Normal)
array[i].frame = CGRectMake(CGFloat(arc4random_uniform(200)), CGFloat(arc4random_uniform(200)), 100, 100)
array[i].addTarget(self, action: "pressed", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(array[i])
}
}
The pressed button is this:
func pressed(sender:AnyObject?)
{
maybe()
}
But I know that is not the problem because another button, which was not create programmatically and has it's own action function, uses the same function and does not crash.
My crash report is here:
(104.0, 70.0)
2016-02-02 10:34:11.267 Test[5150:1087940] -[Test.ViewController pressed]: unrecognized selector sent to instance 0x7f8a8ac3be90
2016-02-02 10:34:11.283 Test[5150:1087940] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test.ViewController pressed]: unrecognized selector sent to instance 0x7f8a8ac3be90'
* First throw call stack:
(
0 CoreFoundation 0x0000000101c81e65 exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001039c1deb objc_exception_throw + 48
2 CoreFoundation 0x0000000101c8a48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101bd790a ___forwarding_ + 970
4 CoreFoundation 0x0000000101bd74b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001024a0194 -[UIApplication sendAction:to:from:forEvent:] + 92
6 UIKit 0x000000010260f6fc -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x000000010260f9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
8 UIKit 0x000000010260eaf8 -[UIControl touchesEnded:withEvent:] + 601
9 UIKit 0x000000010250f49b -[UIWindow _sendTouchesForEvent:] + 835
10 UIKit 0x00000001025101d0 -[UIWindow sendEvent:] + 865
11 UIKit 0x00000001024beb66 -[UIApplication sendEvent:] + 263
12 UIKit 0x0000000102498d97 _UIApplicationHandleEventQueue + 6844
13 CoreFoundation 0x0000000101bada31 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
14 CoreFoundation 0x0000000101ba395c __CFRunLoopDoSources0 + 556
15 CoreFoundation 0x0000000101ba2e13 __CFRunLoopRun + 867
16 CoreFoundation 0x0000000101ba2828 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x0000000106297ad2 GSEventRunModal + 161
18 UIKit 0x000000010249e610 UIApplicationMain + 171
19 Test 0x0000000101aa134d main + 109
20 libdyld.dylib 0x00000001044ca92d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I believe my problem is I have all of e buttons attached to the action "pressed" so it sends a crash. Does anyone know a way to fix this/replace this. I have looked at the posts showing how to programmatically make a UIButton in Swift.
Your code has not problem at all with the number of UIButton associated to the same function, you're missing the colon at the end of the selector for the UIBUtton as your function receive a parameter in his signature, so you need to change the following line:
array[i].addTarget(self, action: "pressed:", forControlEvents:.TouchUpInside)
I hope this help you.
thanks to God (or Latter... ) now on Xcode 8 / Swift 3 syntax is cleaner, so no need to add (:_); compiler will find the method.
so the following code works fine:
override func viewDidLoad() {
super.viewDidLoad()
btn.addTarget(self, action: #selector(buttonTapped), for: UIControlEvents.touchUpInside)
}
final func buttonTapped(sender: UIButton){
}
if You try to mess compiler, i.e:
final func buttonTapped(sender: UIButton. fake: int){..
You will be called (as signature is correct..) but the parameter "fake" will get garbage from stack.
Of course:
final func buttonTapped(){..
works and is called. (no param is taken from stack).
By the way I prefer old syntax that required multiple ":" for every param, as per previous code:
"pressed:"

Unrecognized Selector Swift 2 Purelayout Xcode7

Hello having an issue with an unrecognized selector sent to an instance exception I have searched around a bit but I've been unable to find a solution.
The error I believe is occurring in my use of PureLayout but still unsure of what is specifically causing the issue. One weird thing is that after using Carthage to make my purelayout framework I had to use a weird bridging header in order to import purelayout usually you import PureLayout.h but my code would not compile unless I imported PureLayout_Ios/Purelayout.h in my bridging header. And so I have to import PureLayout_iOS in my viewcontroller.
View:
var presenter: SignUpPresenterProtocol?
//lazy var logoImageView: UIImageView = UIImageView()
lazy var nextButton: UIButton = {
var button = UIButton()
button.backgroundColor = Styles.BUTTON_COLOR
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
self.setupSubviews()
self.setupConstraints()
self.setNeedsStatusBarAppearanceUpdate()
self.presenter?.viewDidLoad()
}
private func setupSubviews()
{
self.view.addSubview(self.nextButton)
self.view.addSubview(self.userNameTextField)
self.nextButton.addTarget(self, action: Selector("userDidSelectNext:"), forControlEvents: UIControlEvents.TouchUpInside)
//self.view.addSubview(self.logoImageView)
}
private func setupConstraints()
{
self.nextButton.translatesAutoresizingMaskIntoConstraints = false
self.nextButton.autoMatchDimension(.Width, toDimension: .Width, ofView: self.view, withMultiplier: 0.8)
self.nextButton.autoSetDimension(.Height, toSize: Styles.BUTTON_HEIGHT)
self.nextButton.autoCenterInSuperview()x
}
func userDidSelectNext(sender: AnyObject)
{
self.presenter?.userDidSelectNext()
}
And here is the error stack trace:
2015-07-28 08:51:58.218 app[10795:507285] -[UIButton autoMatchDimension:toDimension:ofView:withMultiplier:]: unrecognized selector sent to instance 0x7f9d02f8cad0
2015-07-28 08:51:58.222 app[10795:507285] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton autoMatchDimension:toDimension:ofView:withMultiplier:]: unrecognized selector sent to instance 0x7f9d02f8cad0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001079e18b5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001095c1df3 objc_exception_throw + 48
2 CoreFoundation 0x00000001079e9ead -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010793819a ___forwarding___ + 970
4 CoreFoundation 0x0000000107937d48 _CF_forwarding_prep_0 + 120
5 app 0x0000000107324984 _TFC7app12UsernameViewP33_A6105A8D44322785BCD6FD80914CAE7C16setupConstraintsfS0_FT_T_ + 180
6 app 0x000000010732422d _TFC7app12UsernameView11viewDidLoadfS0_FT_T_ + 93
7 app 0x0000000107324312 _TToFC7app12UsernameView11viewDidLoadfS0_FT_T_ + 34
8 UIKit 0x000000010837fa6e -[UIViewController loadViewIfRequired] + 860
9 UIKit 0x000000010837fdbd -[UIViewController view] + 27
10 UIKit 0x00000001082607e4 -[UIWindow addRootViewControllerViewIfPossible] + 61
11 UIKit 0x0000000108260ee1 -[UIWindow _setHidden:forced:] + 302
12 UIKit 0x00000001082726fc -[UIWindow makeKeyAndVisible] + 43
13 app 0x0000000107326afc _TFC7app11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 588
14 app 0x0000000107326c43 _TToFC7app11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 179
15 UIKit 0x00000001081f2bc4 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 272
16 UIKit 0x00000001081f3cea -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3287
17 UIKit 0x00000001081f9e97 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1755
18 UIKit 0x00000001081f7635 -[UIApplication workspaceDidEndTransaction:] + 188
19 FrontBoardServices 0x000000010c2b0b5e -[FBSSerialQueue _performNext] + 192
20 FrontBoardServices 0x000000010c2b0ecc -[FBSSerialQueue _performNextFromRunLoopSource] + 45
21 CoreFoundation 0x000000010790e171 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22 CoreFoundation 0x000000010790409c __CFRunLoopDoSources0 + 556
23 CoreFoundation 0x0000000107903553 __CFRunLoopRun + 867
24 CoreFoundation 0x0000000107902f68 CFRunLoopRunSpecific + 488
25 UIKit 0x00000001081f6fd2 -[UIApplication _run] + 402
26 UIKit 0x00000001081fb6f8 UIApplicationMain + 171
27 app 0x0000000107327e0d main + 109
28 libdyld.dylib 0x000000010a5f892d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
thank you guys!
Try replacing this line...
self.nextButton.addTarget(self, action: Selector("userDidSelectNext:"), forControlEvents: UIControlEvents.TouchUpInside)
with this instead...
self.nextButton.addTarget(self, action: "userDidSelectNext:", forControlEvents: UIControlEvents.TouchUpInside)
You can find more info in the apple document here: Objective C Selectors
After using carthage to update my purelayout library it was fixed, not sure what the issue was though, here is the latest changlog on github.

"terminating with uncaught exception of type NSException" in Swift

When I click on a button the app just crashes and I get this error:
2015-06-12 19:33:16.150 scroll view[79136:11092579] -[scroll_view.ViewController Action:]: unrecognized selector sent to instance 0x7f834148bdd0
2015-06-12 19:33:16.153 scroll view[79136:11092579] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[scroll_view.ViewController Action:]: unrecognized selector sent to instance 0x7f834148bdd0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000103766c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001052d1bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010376e0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001036c413c ___forwarding___ + 988
4 CoreFoundation 0x00000001036c3cd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000104006da2 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010411854a -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x0000000104117919 -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x00000001043b1a10 _UIGestureRecognizerUpdate + 9487
9 UIKit 0x0000000104053686 -[UIWindow _sendGesturesForEvent:] + 1041
10 UIKit 0x00000001040542b2 -[UIWindow sendEvent:] + 666
11 UIKit 0x000000010401a581 -[UIApplication sendEvent:] + 246
12 UIKit 0x0000000104027d1c _UIApplicationHandleEventFromQueueEvent + 18265
13 UIKit 0x00000001040025dc _UIApplicationHandleEventQueue + 2066
14 CoreFoundation 0x000000010369a431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x00000001036902fd __CFRunLoopDoSources0 + 269
16 CoreFoundation 0x000000010368f934 __CFRunLoopRun + 868
17 CoreFoundation 0x000000010368f366 CFRunLoopRunSpecific + 470
18 GraphicsServices 0x000000010774da3e GSEventRunModal + 161
19 UIKit 0x0000000104005900 UIApplicationMain + 1282
20 scroll view 0x000000010356ac67 main + 135
21 libdyld.dylib 0x0000000105a29145 start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here is my code:
var imageView: UIImageView!
var scrollView: UIScrollView!
var containerView = UIView()
var buttonScroll: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 1
imageView = UIImageView(image: UIImage(named: "Map 1.png"))
// 2
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
// 3
scrollView.contentSize = imageView.bounds.size
// 4
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.contentSize.height = scrollView.frame.size.height
var lvl1button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
lvl1button.frame = CGRectMake(250, 140, 100, 50)
let level1 = UIImage(named: "Lvl 1 Button.png") as UIImage!
lvl1button.setImage(level1, forState: .Normal)
lvl1button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
scrollView.addSubview(lvl1button)
You are setting up your button with an action selector called "Action:".
lvl1button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
But self, your view controller, has no such method so when Cocoa tries to call it on button-press, you crash. Unfortunately you did not show that part of your code so one can only guess, but there could be a lot a reasons for this mismatch: the colon could be wrong, the spelling or capitalization could be wrong, and so on. Or maybe you just forgot to write the method!

Resources