我有Xcode 4,我使用Tab Bar模板创建了一个应用程序(而不是基于View的应用程序)。每个选项卡中都有一个 UISwitch ,当我更改它时, UILabel 在 ON 和 OFF 之间切换。非常简单的应用程序,没有混乱。默认情况下,Xcode 4为我创建了两个选项卡。我还需要第三个选项卡,所以我从对象库中拖动 TabBarItem 并将其放在现有的 TabBarController上。我创建了一个新文件, UIViewController的子类,以下代码分为三个选项卡。
以下是界面
#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
以下是实施
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize switch1;
@synthesize status1;
- (IBAction) switch1Change
{
if (switch1.on)
status1.text = @"ON";
else
status1.text = @"OFF";
}
对于SecondViewController和ThirdViewController重复相同的代码,其中ivars更改为switch2,status2和switch3,status3。该项目的链接是here
当我在模拟器上运行它时,第一个和第二个选项卡的一切正常。当我打开第三个选项卡时,出现以下错误“因未捕获的异常而终止应用程序'NSUnknownKeyException',原因:[setValue:forUndefinedKey:]:此类不是键值开关3的键值编码投诉。”
当我从 ThirdView.xib 中删除UISwitch时,我没有收到此错误。只有当我添加开关控件时,才会出现此错误。有人可以解释一下发生了什么吗?
答案 0 :(得分:1)
在Interface Builder中,您的第三个视图控制器属于UIViewController
类(并且没有status3或switch3的出口)。将其班级更改为ThirdViewController
,连接出口,它应该可以正常工作。