视图对象无法连接到文件所有者 [英] View Objects don't connect to the File's Owner

查看:80
本文介绍了视图对象无法连接到文件所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用界面构建器将视图中的对象连接到文件的所有者,但是蓝线没有与它联系。这是我的代码:

I am trying to "Connect" my objects in the view to the File's Owner using the interface builder, but the blue line does not 'link' with it. Here is my code:

CalculatorViewController.h

#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"

@interface CalculatorViewController : UIViewController {
    IBOutlet UILabel *display;
    IBOutlet UIButton *button;
    CalculatorBrain *brain;
    BOOL userIsInTheMiddleOfTypingANumber;
}

- (IBAction)digitPressed: (UIButton *)sender;
- (IBAction)operationPressed: (UIButton *)sender;

@end

CalculatorViewController.m

#import "CalculatorViewController.h"

@implementation CalculatorViewController

- (CalculatorBrain *)brain
{
    if (!brain) {
        brain = [[CalculatorBrain alloc] init];
    }
    return brain;
}


- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [[sender titleLabel] text];
    if (userIsInTheMiddleOfTypingANumber) {
        [display setText: [[display text] stringByAppendingString:digit]];
    } else {
        [display setText:digit];
        userIsInTheMiddleOfTypingANumber = YES;
    }
}

- (IBAction)operationPressed: (UIButton *)sender 
{
    if (userIsInTheMiddleOfTypingANumber) {
        [[self brain] setOperand: [[display text] doubleValue]];
        userIsInTheMiddleOfTypingANumber = NO;
    }
    NSString *operation = [[sender titleLabel] text];
    double result = [[self brain] performOperation:operation];
    [display setText: [NSString stringWithFormat: @"%g", result]];
}
@end  


推荐答案

确保文件所有者设置为CalculatorViewController后,请确保CalculatorViewController.h文件中的IBOutlet类型与您尝试连接它的UI组件类型相匹配。您的标题为UILabel定义了一个IBOutlet,这意味着只能将UILabel连接到它。
如果组件属于不同类型,假设是UIButton,那么您可以更改头文件以包含IBOutlet,如下所示:

Once you have made sure the File's Owner is set to be CalculatorViewController, make sure that the IBOutlet type in the CalculatorViewController.h file matches the UI component type you are trying to connect it to. Your header defines an IBOutlet for a UILabel, which means only a UILabel can be connected to it. If the component is of a different type, lets say a UIButton, then you would change your header file to include an IBOutlet like so:

IBOutlet UIButton *button;

在标题中定义了UIButton后,切换到IB。仔细检查文件的所有者是否已设置为viewcontroller类,然后将UIButton添加到视图中。然后你应该能够:

Once you have defined the UIButton in your header, switch to IB. Double check that file's owner is set to your viewcontroller class, then add a UIButton to the view. Then you should be able to either:


  1. ctrl +从组件拖动到文件的所有者

  2. 右键单击文件的所有者,然后获得一个HUD样式的弹出窗口,显示所有可用的IBOutlet。单击并从UIButton中拖动到视图中的UIButton。

如果您希望IB中的UIButton触发您定义的一个IBActions,您将与行动建立联系。我通常通过右键单击文件所有者来执行此操作,该所有者将显示所有可用的IBActions和IBOutlets。

If you want the UIButton in IB to trigger one of your defined IBActions, you will make a connection to the action. I usually perform this by right-clicking on the File's Owner, which will show all available IBActions and IBOutlets.

希望这会有所帮助!

这篇关于视图对象无法连接到文件所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆