2

I am writing an app from the Tabbed Application template in xCode. Without any code, the app will switch tabs with no problems.

After adding code to the first view controller, I get an error when trying to switch tabs.

here is the error message:

2012-04-26 09:43:01.171 Triangle Solver[9516:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<TriSecondViewController 0x68827d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key solve.'
*** First throw call stack: (0x13ce022 0x155fcd6 0x13cdee1 0x9c6022 0x937f6b 0x937edb 0x952d50 0x23a71a 0x13cfdea 0x13397f1 0x23926e 0xdf1fc 0xdf779 0xdf99b 0xfb0a9 0xfaedd 0xf94aa 0xf934f 0xfae14 0x13cfe99 0x1b14e 0x1b0e6 0x2434bd 0x13cfe99 0x1b14e 0x1b0e6 0xc1ade 0xc1fa7 0xc1b13 0x245c48 0x13cfe99 0x1b14e 0x1b0e6 0xc1ade 0xc1fa7 0xc1266 0x403c0 0x405e6 0x26dc4 0x1a634 0x12b8ef5     0x13a2195 0x1306ff2 0x13058da 0x1304d84 0x1304c9b 0x12b77d8 0x12b788a 0x18626 0x2a0d 0x2975) terminate called throwing an exception(lldb)

firstViewResponder.h: #import

@interface TriFirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *triText1;
@property (weak, nonatomic) IBOutlet UITextField *triText2;
- (IBAction)calc:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *legA;
@property (weak, nonatomic) IBOutlet UILabel *legB;
@property (weak, nonatomic) IBOutlet UILabel *hypotenuse;
@property (weak, nonatomic) IBOutlet UILabel *angleA;
@property (weak, nonatomic) IBOutlet UILabel *angleB;

@property (weak, nonatomic) IBOutlet UIButton *button1;

@property (copy, nonatomic) NSString *text1;
@property (copy, nonatomic) NSString *text2;

@end

secondViewController.h:

@interface TriSecondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *sines1;
@property (weak, nonatomic) IBOutlet UITextField *sines2;
@property (weak, nonatomic) IBOutlet UITextField *sines3;
- (IBAction)solve2:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *angleA;
@property (weak, nonatomic) IBOutlet UILabel *sideA;
@property (weak, nonatomic) IBOutlet UILabel *angleB;
@property (weak, nonatomic) IBOutlet UILabel *sideB;
@property (weak, nonatomic) IBOutlet UILabel *angleC;
@property (weak, nonatomic) IBOutlet UILabel *sideC;

@property (weak, nonatomic) IBOutlet UISegmentedControl *segControl;
- (IBAction)aassas:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *sine1;
@property (weak, nonatomic) IBOutlet UILabel *sine2;
@property (weak, nonatomic) IBOutlet UILabel *sine3;

@end

firstViewController.m: @interface TriFirstViewController ()

@end

@implementation TriFirstViewController
@synthesize legA;
@synthesize legB;
@synthesize hypotenuse;
@synthesize angleA;
@synthesize angleB;
@synthesize button1;
@synthesize triText1;
@synthesize triText2;

@synthesize text1;
@synthesize text2;

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

- (void)viewDidUnload
{
    [self setTriText1:nil];
    [self setTriText2:nil];
    [self setLegA:nil];
    [self setLegB:nil];
    [self setHypotenuse:nil];
    [self setAngleA:nil];
    [self setAngleB:nil];
    [self setButton1:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)calc:(id)sender 
{
    double aa = 0;
    double ab = 0;
    double la = 0;
    double lb = 0;
    double h = 0;

    self.text1 = self.triText1.text;
    self.text2 = self.triText2.text;

    aa = [self.text1 doubleValue];
    lb = [self.text2 doubleValue]; 

    ab = 90 - aa;

    la = (tan((aa * (M_PI/180))) * lb);
    h = (pow(la, 2) + pow(lb, 2));
    h = sqrt(h);

    self.legA.text = [NSString stringWithFormat: @"%.2lf", la];
    self.legB.text = [NSString stringWithFormat: @"%.2lf", lb];
    self.angleA.text = [NSString stringWithFormat: @"%.2lf", aa];
    self.angleB.text = [NSString stringWithFormat: @"%.2lf", ab];
    self.hypotenuse.text = [NSString stringWithFormat: @"%.2lf", h];
}

@end

secondViewController.m:

@interface TriSecondViewController ()

@end

@implementation TriSecondViewController
@synthesize sine1;
@synthesize sine2;
@synthesize sine3;
@synthesize angleA;
@synthesize sideA;
@synthesize angleB;
@synthesize sideB;
@synthesize angleC;
@synthesize sideC;
@synthesize segControl;
@synthesize sines1;
@synthesize sines2;
@synthesize sines3;

BOOL mode = NO;

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

- (void)viewDidUnload
{
    [self setSines1:nil];
    [self setSine2:nil];
    [self setSines3:nil];
    [self setAngleA:nil];
    [self setAngleB:nil];
    [self setAngleC:nil];
    [self setSideA:nil];
    [self setSideB:nil];
    [self setSideC:nil];
    [self setSine1:nil];
    [self setSine2:nil];
    [self setSine3:nil];
    [self setSegControl:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (IBAction)solve2:(id)sender 
{
    if (mode)
    {
        double aa = [self.sines1.text doubleValue]; 
        double ab = [self.sines2.text doubleValue];
        double ac = 180 - aa - ab;
        double sa = [self.sines3.text doubleValue];
        double sb;
        double sc;

        //convert degrees to radians
        aa = (aa * (M_PI/180));
        ab = (ab * (M_PI/180));
        ac = (ac * (M_PI/180));

        sb = (sa/sin(aa))*sin(ab);
        sc = (sa/sin(aa))*sin(ac);

        self.angleA.text = [NSString stringWithFormat: @"%.2lf", aa];
        self.angleB.text = [NSString stringWithFormat: @"%.2lf", ab];
        self.angleC.text = [NSString stringWithFormat: @"%.2lf", ac];
        self.sideA.text = [NSString stringWithFormat: @"%.2lf", sa];
        self.sideB.text = [NSString stringWithFormat: @"%.2lf", sb];
        self.sideC.text = [NSString stringWithFormat: @"%.2lf", sc];
    }

    if (!mode) 
    {
        double aa = [self.sines1.text doubleValue];
        double ab;
        double ac;
        double sa = [self.sines2.text doubleValue];
        double sb = [self.sines3.text doubleValue];
        double sc;

        aa = (aa * (M_PI/180));

        ab = asin(sb*(sin(aa)/sa));
        ac = 180 - aa - ab;

        ac = (ac * (M_PI/180));

        sc = (sa/sin(aa))*sin(ac);

        self.angleA.text = [NSString stringWithFormat: @"%.2lf", aa];
        self.angleB.text = [NSString stringWithFormat: @"%.2lf", ab];
        self.angleC.text = [NSString stringWithFormat: @"%.2lf", ac];
        self.sideA.text = [NSString stringWithFormat: @"%.2lf", sa];
        self.sideB.text = [NSString stringWithFormat: @"%.2lf", sb];
        self.sideC.text = [NSString stringWithFormat: @"%.2lf", sc];
    }
}

- (IBAction)aassas:(id)sender 
{
    switch (self.segControl.selectedSegmentIndex) {
        case 0:
            self.sine1.text = @"Angle A/n";
            self.sine2.text = @"Angle B/n";
            self.sine3.text = @"Side A/n";
            mode = NO;
            break;

        case 1:
            self.sine1.text = @"Angle A/n";
            self.sine2.text = @"Side A/n";
            self.sine3.text = @"Side B/n";
            mode = YES; 
            break;

        default:
            break;
    }
}
@end
TPLeavitt
  • 23
  • 4

3 Answers3

3

The usual reason for this error is that somewhere in a xib file or storyboard you have a link from some control or view to an IBOutlet called 'solve' but you no longer have that property or variable defined in the code of the class where the link points.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • where are the xib files, all the links in the storyboard are good. – TPLeavitt Apr 26 '12 at 16:29
  • If you have a storyboard, you probably don't have xib files. Have you ever had something called 'solve' in this project? (Because the app thinks you did and that there's still a reference to it somewhere.) – Phillip Mills Apr 26 '12 at 16:35
  • Yes, I renamed them to different names and redid the links from the storyboard – TPLeavitt Apr 26 '12 at 16:50
  • @TPLeavitt: Apparently, you still have a xib or storyboard somewhere that contains the `solve` outlet and has it connected. The xib or storyboard in question either contains or is owned by the TriSecondViewController. – Peter Hosey Apr 26 '12 at 16:59
  • Right-click on your .storyboard file in the file list and choose Open As->Source Code from the popup menu. Then search for the word 'solve' in the XML source that is displayed. If you find it, the context should give you a hint as to where it's hiding. If it's not there, you have some different problem that's showing the same symptoms. – Phillip Mills Apr 26 '12 at 17:06
  • I found the problem. In the storyboard source code, there was one line referencing 'solve' as an outlet. – TPLeavitt Apr 27 '12 at 14:45
1

I think you could get the detailed info in Xcode debugger or gdb, the call stack is very important info. I did it and found the result that a identifier of tableviewcell is wrong.

Itachi
  • 5,777
  • 2
  • 37
  • 69
-1

UIViewController compliant to NSKeyValueCoding protocol, so when set a value for undeined key and you do not override

- (void)setValue:(id)value forUndefinedKey:(NSString *)key;

it will arise a NSUndefinedKeyException exception..

[self setTriText1:nil];
[self setTriText2:nil];
[self setLegA:nil];
[self setLegB:nil];
[self setHypotenuse:nil];
[self setAngleA:nil];
[self setAngleB:nil];
[self setButton1:nil];

may be here can brings that exception. why not you write it like this:

self.triText1 = nil;
self.triText2 = nil;
self.legA = nil;

as well for sencondViewContrlloer, if you use "set method" explicitly and write wrong method, similar exception will be happened..

ianwang
  • 59
  • 1
  • 5
  • There's no reason to override `setValue:forUndefinedKey:`, and dot syntax and message syntax do exactly the same thing (send a `setWhatever:` message). – Peter Hosey Apr 26 '12 at 16:57
  • i know it ,i just recommend a simple use method,prevent writing wrong method, your vote is a little unpassive。。 – ianwang Apr 27 '12 at 16:28