简体   繁体   English

您如何连接自定义UIView?

[英]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 为了使视图符合接口构建器中的子类,您需要

  1. Click on the UIView in your storyboard 在情节提要中单击UIView
  2. Open the right inspector panel 打开右侧的检查器面板
  3. Click on the third tab for identity inspector 单击第三个选项卡以进行身份​​检查
  4. Add your class 添加课程

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.

相关问题 如何将触摸插座添加到自定义UIView? - How do you add touch outlets to custom UIView? 如何在自定义UIView中连接UICollectionView-Swift - How to wire up UICollectionView in a custom UIView - Swift 在UIView动画期间,如何为UIView图层的子图层设置动画? - How do you animate the sublayers of the layer of a UIView during a UIView animation? 如何在UIContainerView中连接UICollectionViewController - How do I hook up UICollectionViewController in UIContainerView 在iPhone中,如何使UIView的幻灯片从左到右并彼此对接? - How do you make UIView's slide left to right and being butted right up against each other in iPhone? 如何获取自定义UIView的子视图以显示在文档大纲中? - How can you get the subviews of a custom UIView to appear in the Document Outline? 如何根据objective-c中的滚动方向创建一个从UINavBar后面向下或向上滑动的自定义UIView? - How do I create a custom UIView that slides down or up from behind a UINavBar depending on scroll direction in objective-c? iOS:弹出自定义UIView - iOS: Popping up custom UIView 当单元格被选中时,如何隐藏和显示uiview - How do you hide and show a uiview when cell is selected UITableView 使用UIKit Dynamics时如何更新UIView布局? - How do you update UIView layout when using UIKit Dynamics?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM