【问题标题】:Change UILabel text of custom UIView doesn't work更改自定义 UIView 的 UILabel 文本不起作用
【发布时间】:2013-09-23 09:18:20
【问题描述】:

我有一个自定义的UIView,它是用 xib 构建的,它有一个名为 CustomModuleUIView 的文件所有者,其中包含标签和按钮。我在我的 Rootviewcontroller 中调用了这个自定义视图,并成功地使用initWithCoder 方法显示它。问题是我既不能从customMouleUIView 也不能从根ViewController 更改UILabel 的默认文本。我在initWithCoder 中找到了告诉我进行自定义初始化的示例,但它对我不起作用,没有任何变化,并且没有任何错误,它显示默认文本。

这是我的自定义 UIView xib

这是我的根视图控制器

这是我的代码哦自定义 UIView .h

 #import <UIKit/UIKit.h>

@interface ModuleCustomUIView : UIView

-(void) setup;
@property (weak, nonatomic) IBOutlet UIImageView *_moduleIcon;
@property (retain, nonatomic) IBOutlet UILabel *_moduleName;
- (IBAction)openDemo:(id)sender;
- (IBAction)close:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *_moduleImage;
@property (strong, nonatomic) IBOutlet UILabel *_positionLabel;

@end

.m 的代码,我使用 setup 方法来初始化我的 UIView,因为我无法调用

[[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil] ;

在导致无限循环的 initWithCoder 内部。

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
    }
    return self;
}

-(void) setup
{
    NSString *xib= @"CustomUIView";
    NSArray *array=[[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil] ;
    UIView *view =[array objectAtIndex:0];
    //code that doesn't work
    [_positionLabel setText:@"hello world"];
    //
    [self addSubview:view];
}

这是我的根视图控制器.h

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_moduleCustomView setup];
    [self.view addSubview:_moduleCustomView];
    //code doesn't work 
    [_moduleCustomView setText:@"hello world"];
}

即使在加载时我也无法更改文本

【问题讨论】:

  • 请发布您的代码。
  • 您是否将 xib 中的标签链接到视图控制器中的相应 IBOutlet?
  • 我现在只能在 8 小时后添加评论,这是 stackoverflow 告诉我的。
  • 我已将 xib 和标签与文件所有者中的 IB 链接起来

标签: ios objective-c ipad uiview xib


【解决方案1】:

我发现了我的错误,它与文件所有者有关,我已经在检查器中更改了它,但是通过选择 uiview 而不是文件的所有者,我将 NSObject 更改为我的类名并重新连接标签。

【讨论】:

    【解决方案2】:

    我猜 Files Owner 属性应该是您的“根视图控制器”。

    【讨论】:

    • 我无法将基于 UIView 的 xib 与我认为的 UIViewController 链接?
    • 抱歉误读...您是否在设置方法中尝试过[view.positionLabel setText:@"hello world"];?还是 [_moduleCustomView.positionLabel setText:@"hello world"]; 在您的 viewDidLoad 方法中?
    • 是的,这是我所做的没有任何改变,昨天我尝试了 awakeFromNib 方法,我可以改变它,但我想动态编辑它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多