【发布时间】:2011-04-26 06:38:41
【问题描述】:
我有 Xcode 4,我使用 Tab Bar 模板(而不是基于视图的应用程序)创建了一个应用程序。每个选项卡中都有一个 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 时,我没有收到此错误。只有当我添加开关控件时,我才会收到此错误。有人可以解释发生了什么吗?
【问题讨论】: