【发布时间】:2014-06-20 23:34:14
【问题描述】:
通过本教程编写我的第一个 iphone 应用程序——我有一个自定义 UIView,方法是 showDie,我的故事板上有两个 UIView 元素,我已经连接到我的 ViewController。但是,这两个 UIView 元素都有错误消息“'UIView' 没有可见的@interface 声明选择器'showDie'。我认为这是因为 UIViews 不是 XYZDieView 的子类,但是当我控制时-从我的情节提要中单击,下拉列表中只出现了 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
【问题讨论】:
标签: ios iphone objective-c cocoa-touch uiview