通过本教程编写我的第一个iphone应用程序 - 我有一个自定义的UIView,方法是showDie,我的故事板上有两个UIView元素,我已经连接到我的ViewController。但是,两个UIView元素都有错误消息"没有可见的@interface用于' UIView'声明选择器' showDie'。我认为是因为UIViews没有对XYZDieView进行子类化,但是当我从我的Storyboard中单击控制时,只有UIView出现在下拉列表中 - 没有XYZDieView。我尝试在ViewController.h中手动更改文本,但这不起作用(我没想到)。我是在正确的轨道上吗?我如何使用我的子类? (xcode 5)
XYZDieView.m
-(void)showDie:(int)num
{
if (self.dieImage == nil){
self.dieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:self.dieImage];
}
NSString *fileName = [NSString stringWithFormat:@"dice%d.png", num];
self.dieImage.image = [UIImage imageNamed:fileName];
}
XYZViewController.h
#import <UIKit/UIKit.h>
#import "XYZDieView.h"
@interface XYZViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *sumLabel;
@property (strong, nonatomic) IBOutlet UIButton *rollButton;
@property (strong, nonatomic) IBOutlet UIView *leftDieView;
@property (strong, nonatomic) IBOutlet UIView *rightDieView;
@end
XYZViewController.m
#import "XYZViewController.h"
#import "XYZDiceDataController.h"
@interface XYZViewController ()
@end
@implementation XYZViewController
- (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)rollButtonClicked:(UIButton *)sender {
XYZDiceDataController *diceDataController = [[XYZDiceDataController alloc] init];
int roll = [diceDataController getDiceRoll];
int roll2 = [diceDataController getDiceRoll];
[self.leftDieView showDie:roll];
[self.rightDieView showDie:roll2];
int sum = roll + roll2;
NSString *sumText = [NSString stringWithFormat:@"Sum is %i", sum];
self.sumLabel.text = sumText;
}
@end
答案 0 :(得分:1)
是的,你是对的,问题是子类化。为了使视图符合接口构建器中的子类,您需要
然后尝试再次将它连接到ViewController。
编辑:之后您可能需要投射XYZDieView myView = (XYZDieView*)leftDieView;