xcode 查看对象不连接到文件的所有者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5468310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:41:03  来源:igfitidea点击:

View Objects don't connect to the File's Owner

iphonexcodeinterface-builder

提问by Utumbu

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

我正在尝试将视图中的对象“连接”到File's Owner使用interface builder,但蓝线没有与它“链接”。这是我的代码:

CalculatorViewController.h:

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:

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  

回答by Parth Bhatt

Try and check if class of your File's Owner is set to your ViewController Class properly in your XIB.

尝试检查您的文件所有者的类是否在您的 XIB 中正确设置为您的 ViewController 类。

For checking Go to your XIB

检查去你的XIB

Click on File's Owner and Open the Inspector.

单击文件所有者并打开检查器。

In inspector, go to the last(fourth) tab and check whether you have set your class as <yourViewControllerName>

在检查器中,转到最后一个(第四个)选项卡并检查您是否已将类设置为 <yourViewControllerName>

Hope this helps you.

希望这对你有帮助。

EDIT:

编辑:

For better understanding I have added an image of where you need to look for the class.

为了更好地理解,我添加了一张图片,说明您需要在哪里查找课程。

enter image description here

在此处输入图片说明

Also please cross check that you have declared the variables with IBOutletPrefix in your ViewController's header file

另外请交叉检查您是否IBOutlet在 ViewController 的头文件中使用Prefix声明了变量

回答by ccjensen

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:

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

IBOutlet UIButton *button;

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:

在标题中定义 UIButton 后,切换到 IB。仔细检查文件的所有者是否设置为您的视图控制器类,然后向视图添加一个 UIButton。那么你应该能够:

  1. ctrl+drag from the component to the file's owner
  2. right-click on the file's owner and get a HUD styled popup showing all available IBOutlet's. click and drag from the UIButton one to the UIButton in your view.
  1. ctrl+从组件拖动到文件的所有者
  2. 右键单击文件的所有者并获得一个 HUD 样式的弹出窗口,其中显示了所有可用的 IBOutlet。单击并从 UIButton 拖动到视图中的 UIButton。

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.

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

Making connection from UIButton to IBAction

建立从 UIButton 到 IBAction 的连接

Hope this helps!

希望这可以帮助!

回答by Chandan Shetty SP

Did you set the file owner(in interface builder) to the class CalculatorViewController.

您是否将文件所有者(在界面构建器中)设置为类 CalculatorViewController。

回答by Tim

Im no expert on Interface Builder or Objective C, but did you use:

我不是 Interface Builder 或 Objective C 方面的专家,但您是否使用过:

@property (nonatomic, retain) IBOutlet UIButton *yourButton;

and then in the .m file:

然后在 .m 文件中:

@synthesize yourButton;

Im also not sure, but you may have to 'build' it first, then go into Interface Builder and then it may 'connect' up.

我也不确定,但您可能必须先“构建”它,然后进入 Interface Builder,然后它可能会“连接”起来。