问题
I have Xcode 4 and I created an application using the Tab Bar template (and not View based app). There is a UISwitch in each of these tabs and when I change it, a UILabel switches between ON and OFF. Very simple app and no confusion. Xcode 4 creates two tabs for me by default. I need a third tab as well, so I drag TabBarItem from the Objects Library and drop it on the existing TabBarController. I create a new file, subclass of UIViewController and the following code goes into three tabs.
The following is the interface
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
UISwitch *switch1;
UILabel *status1;
}
@property (nonatomic,retain) IBOutlet UISwitch *switch1;
@property (nonatomic,retain) IBOutlet UILabel *status1;
- (IBAction) switch1Change;
@end
The following is the implementation
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize switch1;
@synthesize status1;
- (IBAction) switch1Change
{
if (switch1.on)
status1.text = @"ON";
else
status1.text = @"OFF";
}
The same code repeats for SecondViewController and ThirdViewController with ivars changing to switch2,status2 and switch3,status3. The link to the project is here
When I run it on the simulator, everything works fine for the first and second tab. When I open the third tab, I get the following error "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: [ setValue: forUndefinedKey:]: this class is not key value coding-complaint for the key switch3."
When I remove the UISwitch from the ThirdView.xib, I don't get this error. Only when I add the switch control, I get this error. Can somebody please explain what is happening?
回答1:
In Interface Builder, your third view controller is of class UIViewController
(and doesn't have outlets for status3 or switch3). Change its class to ThirdViewController
, wire up the outlets, and it should work.
来源:https://stackoverflow.com/questions/5786862/xcode-4-tab-bar-application-problem-in-executing-third-tab