ios - Objective C - 在 View Controller 的 UIView 子类上设置标签属性

标签 ios objective-c uiview

在我的应用程序中,我有一个在 Nib 文件中设计的自定义 Google map 图钉标记。大头针标记的一部分将显示预计到达时间。

这是我加载 Nib 并尝试设置自定义时间的方式:

self.markerPinFromNib = [[[NSBundle mainBundle] loadNibNamed:@"PinWithTimeView" owner:self options:nil] firstObject];
self.markerPinFromNib.estTime.text = @"20";

当我运行它时,出现错误:

-[UIView estTime]: unrecognized selector sent to instance

PinWithTimeView.h

#import <UIKit/UIKit.h>
@interface PinWithTimeView : UIView

@property (nonatomic, weak) IBOutlet UIImageView *rotateCircle;
@property (nonatomic, strong) IBOutlet UILabel *estTime;

@end

谁能指出我做错了什么?

最佳答案

在你的 PinWithTimeView.h 中

@interface PinWithTimeView : UIView

@property (nonatomic, weak) IBOutlet UIImageView *rotateCircle;
@property (nonatomic, weak) IBOutlet UILabel *estTime;
-(id)initWithEstTime:(NSString*)time;
@end

在你的 PinWithTimeView.m 中

-(id)initWithEstTime:(NSString*)time{
    self = [super init];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"PinWithTimeView" owner:self options:nil] objectAtIndex:0];
        self.estTime.text = time;

    }
    return self;
}

在你的 Controller 中

self.markerPinFromNib = [[PinWithTimeView alloc]initWithEstTime:@"20"];

关于ios - Objective C - 在 View Controller 的 UIView 子类上设置标签属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45261533/

相关文章:

ios - UISegmentedControl 的索引处的 UIView

ios - 使用对 SPPlaylistFolder 的folderId 引用

ios - 自动缩小 UIButton 文本之前的换行符

ios - 如何确定 iOS 通讯录中 ABPerson 的最后修改日期?

objective-c - 如何在 swift 中创建 Pixel_8 缓冲区

ios - 正确设置委托(delegate)

ios - UIViewController 将浮点值传递给 UIView 方法

ios - 带有 Storyboard的部分 View

iphone - 将控制权(响应者?)传递回下部 UI​​View,同时仍在上部 UIView 上执行淡出动画

ios - XCODE:在用户完成搜索后,如何从我的searchBar中隐藏光标?