gpt4 book ai didi

ios - UIView 未显示在 Storyboard 上

转载 作者:行者123 更新时间:2023-12-01 18:52:50 25 4
gpt4 key购买 nike

我创建了一个 UIView(KGCalloutView) 并将其放置在我的 Storyboard(Main.storyboard) 中,方法是拖动 View 对象并将类设置为 KGCalloutView。但是,当我运行该项目时,该 View 永远不会显示在 Storyboard 中。

我第一次尝试解决需要添加以下方法,但是它导致我相信一个无限循环(可以理解)并最终崩溃。

- (void)awakeFromNib {
if ([[NSBundle mainBundle] loadNibNamed:@"KGCalloutView" owner:self options:nil]) {
[self.view setFrame:[self bounds]];
[self addSubview:self.view];
}
}

接下来我尝试在下面添加init方法,但也没有解决问题:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
}

return self;
}

我试图发布我的 .xib 和 Main.storyboard 的图像,但我想我的声誉还不够高。

这是 KGCalloutView.h:
@interface KGCalloutView : UIView <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic) IBInspectable NSString *title;
@property (strong, nonatomic) IBOutlet KGCalloutView *view;

@property (weak, nonatomic) IBOutlet UITextField *titleField;
@property (weak, nonatomic) IBOutlet UIPickerView *typePickerView;

@end

这是 KGCalloutView.m:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
}
return self;
}

- (void)awakeFromNib {
if ([[NSBundle mainBundle] loadNibNamed:@"KGCalloutView" owner:self options:nil]) {
[self.view setFrame:[self bounds]];
[self addSubview:self.view];
}
}

-(void)layoutSubviews{
self.titleField.text = @"test";
self.typePickerView.dataSource = self;
self.typePickerView.delegate = self;
}

这是我的 ViewController.h:
@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet KGCalloutView *callout;

@end

这是我的 ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
}

最佳答案

您需要在 ViewController 中加载 View 才能看到它。

首先,确保将 View 的文件所有者类设置为 ViewController(看起来你已经这样做了)

file owner

二、将 View 连接到 ViewController 的属性(标注)

第三,不要在 KGCalloutView.m 中实现,而是在 ViewController.m 中执行以下代码

- (void)viewDidLoad {
[super viewDidLoad];

if ([[NSBundle mainBundle] loadNibNamed:@"KGCalloutView" owner:self options:nil]) {
//This will load view from storyboard to IBOutlet callout
[self.callout setFrame:[self.view bounds]];
[self.view addSubview:self.callout];
}
}

关于ios - UIView 未显示在 Storyboard 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com