3

Custom class input

I'm attempting to add a custom view controller to my storyboard. The view controller KTResearchUploadViewController appears in the "Custom Class" class dropdown, and I have selected it. Inside the view controller (on the storyboard), I have a button that is linked to KTResearchUploadViewController to call a method handleUploadButtonPress on primary action.

When the button is pressed, I receive this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController handleUploadButtonPress:]: unrecognized selector sent to instance 0x11c33b740'

It looks like it's trying to call handleUploadButtonPress on UIViewController rather than KTResearchUploadViewController. What am I doing wrong here?

KTResearchUploadViewController.h

#import <UIKit/UIKit.h>

@interface KTResearchUploadViewController : UIViewController

@end

KTResearchUploadViewController.m

#import "KTResearchUploadViewController.h"

@interface KTResearchUploadViewController ()

@end

@implementation KTResearchUploadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)handleUploadButtonPress:(id)sender {
    NSLog(@"Button pressed!");
}

@end
SimpleJ
  • 13,812
  • 13
  • 53
  • 93
  • 1
    https://stackoverflow.com/questions/17852287/should-i-put-ibactions-in-the-header-file-or-not Try adding the ibaction to your .h – solenoid Aug 24 '17 at 19:45

2 Answers2

1

Add the IBAction to your .h

#import <UIKit/UIKit.h>

@interface KTResearchUploadViewController : UIViewController
- (IBAction)handleUploadButtonPress:(id)sender;
@end

Also make sure there is a connection from your button in IB to your code

https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ConnectTheUIToCode.html

solenoid
  • 954
  • 1
  • 9
  • 20
  • I tried both of these, and now I'm receiving this error: `'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key uploadButton.'` – SimpleJ Aug 24 '17 at 20:10
  • where is the key uploadButton from? – solenoid Aug 24 '17 at 20:26
  • `uploadButton` is the name I gave the connection between the button in the storyboard and `KTResearchUploadViewController` – SimpleJ Aug 24 '17 at 20:29
  • Try changing that to handleUploadButtonPress - I can test it out in about 10 if its not working still – solenoid Aug 24 '17 at 20:30
  • I have `handleUploadButtonPress` defined in both the .m and .h files. `uploadButton` is just a reference to the `UIButton` (I'm not actually using this though). – SimpleJ Aug 24 '17 at 20:32
  • I just did it up real quick and it worked fine - but the name of the connection should be the name of the action. http://imgur.com/a/iZxHO – solenoid Aug 24 '17 at 20:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152795/discussion-between-solenoid-and-simplej). – solenoid Aug 24 '17 at 20:59
0

I'm not sure if this is the cause of the original problem but I've just had a similar problem and ended up here. Eventually I figured out that my ViewController sub-class was not a member of the target that I was running in Xcode. For some reason in this case it just defaults to using ViewController instead.

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165