I am very new to swift, extremely new, and I will probably feel like an idiot for asking this question. My very simple goal is that when my button is tapped the button's title will disappear, but unfortunately the app is crashing every time I run it and I don't know what I'm doing wrong. Thank you for the help.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var background1: UIImageView!
@IBOutlet weak var screenButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.screenButton.setTitle("Tap!", forState: UIControlState.Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func screenTapped(sender: AnyObject) {
self.screenButton.setTitle("", forState: UIControlState.Normal)
}
}
2015-04-10 17:13:41.465 TopScore[6391:414281] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key screenTapped.' *** First throw call stack: ( 0 CoreFoundation 0x0000000104fc0a75 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000106b18bb7 objc_exception_throw + 45 2 CoreFoundation 0x0000000104fc06b9 -[NSException raise] + 9 3 Foundation 0x00000001053dbd43 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259 4 CoreFoundation 0x0000000104f0a5e0 -[NSArray makeObjectsPerformSelector:] + 224 5 UIKit 0x0000000105b1a4ed -[UINib instantiateWithOwner:options:] + 1506 6 UIKit 0x0000000105978a88 -[UIViewController _loadViewFromNibNamed:bundle:] + 242 7 UIKit 0x0000000105979078 -[UIViewController loadView] + 109 8 UIKit 0x00000001059792e9 -[UIViewController loadViewIfRequired] + 75 9 UIKit 0x000000010597977e -[UIViewController view] + 27 10 UIKit 0x0000000105898509 -[UIWindow addRootViewControllerViewIfPossible] + 58 11 UIKit 0x00000001058988a1 -[UIWindow _setHidden:forced:] + 247 12 UIKit 0x00000001058a4f8c -[UIWindow makeKeyAndVisible] + 42 13 UIKit 0x000000010584f0c2 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732 14 UIKit 0x0000000105851e3e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349 15 UIKit 0x0000000105850d35 -[UIApplication workspaceDidEndTransaction:] + 179 16 FrontBoardServices 0x00000001086ca243 __31-[FBSSerialQueue performAsync:]_block_invoke + 16 17 CoreFoundation 0x0000000104ef5c7c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12 18 CoreFoundation 0x0000000104eeb9c5 __CFRunLoopDoBlocks + 341 19 CoreFoundation 0x0000000104eeb785 __CFRunLoopRun + 2389 20 CoreFoundation 0x0000000104eeabc6 CFRunLoopRunSpecific + 470 21 UIKit 0x00000001058507a2 -[UIApplication _run] + 413 22 UIKit 0x0000000105853580 UIApplicationMain + 1282 23 TopScore 0x0000000104de1cee top_level_code + 78 24 TopScore 0x0000000104de1d2a main + 42 25 libdyld.dylib 0x00000001072f4145 start + 1 26 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
At some point, you accidentally set up an outlet called screenTapped
. You need to find that outlet in Interface Builder in your storyboard and delete it.
What you want is an action from the button to screenTapped:
. An action and an outlet are two very different things.