[英]How do you hook up a custom UIView?
Writing my first iphone app through this tutorial--I have a custom UIView with the method showDie and I have two UIView elements on my storyboard that I've hooked up to my ViewController. 通过本教程编写我的第一个iphone应用程序-我有一个使用showDie方法的自定义UIView,并且在故事板上有两个已连接到ViewController的UIView元素。 However, both the UIView elements have the error message "No visible @interface for 'UIView' declares the selector 'showDie'. I think that's because the UIViews aren't subclassing XYZDieView, but when I control-clicked from my Storyboard, only UIView appeared in the dropdown--no XYZDieView. I tried just changing the text manually in the ViewController.h, but that didn't work (I didn't think it would). Am I on the right track? How do I use my subclass? (xcode 5)
但是,两个UIView元素均显示错误消息“'UIView'的无可见@interface声明了选择器'showDie'。我认为这是因为UIViews不在XYZDieView的子类中,但是当我从情节提要中按住Control键单击时,只有UIView出现在下拉菜单中-没有XYZDieView。我尝试仅在ViewController.h中手动更改文本,但这没有用(我认为不会)。我走在正确的轨道上吗?子类(xcode 5)
XYZDieView.m 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 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 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
Yes you're right, the problem is subclassing. 是的,您是对的,问题是子类化。 In order to have a view conform to a subclass in the interface builder you need to
为了使视图符合接口构建器中的子类,您需要
Then try to connect it to your ViewController again. 然后尝试再次将其连接到ViewController。
Edit: Afterwards you may need to cast XYZDieView myView = (XYZDieView*)leftDieView;
编辑:之后,您可能需要
XYZDieView myView = (XYZDieView*)leftDieView;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.