Question

I had created a button earlier as an IBAction and named it 'hi.' I manually deleted the code from my .h and .m file, and deleted the button from the storyboard and now I'm getting the error below:

2014-02-08 18:31:53.135 MadLibs2[7645:70b] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key hi.'

How do I remove the 'hi' reference?

//  JBViewController.h
//  MadLibs2

#import <UIKit/UIKit.h>

@interface JBViewController : UIViewController

// Properties manage an object's internal data
//// A properties data is stored in an instance variable or ivar (e.g., *sliderLabel)
// The property accesses ivars via getter/setter methods (aka accessors)
// put @property in front of the declaration of the ivar
// We access properties using the self keyword
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
- (IBAction)sliderChanged:(id)sender;
- (IBAction)toggleShowHide:(id)sender;

@property (weak, nonatomic) IBOutlet UIView *settingsView;

@property (weak, nonatomic) IBOutlet UISwitch *endingSwitch;

@property (weak, nonatomic) IBOutlet UILabel *petsLabel;

@property (weak, nonatomic) IBOutlet UIStepper *petsStepper;

- (IBAction)stepperChanged:(id)sender;

- (IBAction)createStory:(id)sender;
@end

//  JBViewController.m
//  MadLibs2

#import "JBViewController.h"

@interface JBViewController ()

@end

@implementation JBViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    //why not NSInteger?
    int numberAsInt = (int) (slider.value + 0.5f);
    NSString *newText = [[NSString alloc] initWithFormat:@"%d", numberAsInt];
    //self is used to access properties
    /// What is 'text' used for? Is it a method?
    self.sliderLabel.text = newText;
}

- (IBAction)toggleShowHide:(id)sender {
    UISegmentedControl *segmentedControl =
    (UISegmentedControl *)sender;
    NSInteger segment = segmentedControl.selectedSegmentIndex;
    if (segment == 0)
        // self is used to access properties
        [self.settingsView setHidden:YES];
    else
        [self.settingsView setHidden:NO];

}
- (IBAction)stepperChanged:(id)sender {
    // update petsLabel to increase or decrease value
    UIStepper *stepper = (UIStepper *) sender;
    //NSLog([NSString stringWithFormat:@"%d",(int)stepper.value]);
    int stepperNum = (int)stepper.value;
    NSString *numPets = [[NSString alloc] initWithFormat:@"%d", stepperNum];
    self.petsLabel.text = numPets;
}

- (IBAction)createStory:(id)sender {
}
@end
Was it helpful?

Solution 2

You should select the button and look in the "connections inspector", the reference should appear there.

OTHER TIPS

This only happens when you delete the IBOutlet from your .h file but not in storyboard.

If you can still see the IBOutlet when you run the program, chances are you did not remove it from storyboard.

Perhaps some of your IBOutlets have no titles, text, or color, and you think you have deleted them, but they (or one of them, which causes the problem) is / are still there.

Adding Screenshot:

For example:

I have here a segmented control and a button, which is not visible because it has no title, text, nor background color:

enter image description here

But the button is actually still there as evidenced when I highlight it by dragging the mouse cursor from top left to bottom right:

enter image description here

And if I click on it accidentally while thinking that I deleted it (and also the connections and actions in .h), I would get the error.

Right click on the UIButton within the storyboard - you will see the list of all the actions currently setup. Click the small "x" close to your "hi" action and remove it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top