【问题标题】:How do you hook up a custom UIView?你如何连接自定义 UIView?
【发布时间】: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


    【解决方案1】:

    是的,你是对的,问题在于子类化。为了使视图符合界面构建器中的子类,您需要

    1. 点击故事板中的 UIView
    2. 打开右侧检查器面板
    3. 单击身份检查器的第三个选项卡
    4. 添加您的课程

    然后尝试再次将其连接到您的 ViewController。

    编辑:之后你可能需要投 XYZDieView myView = (XYZDieView*)leftDieView;

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多