【问题标题】:xib and SubUIView.class, how to add a property into that class? [closed]xib 和 SubUIView.class,如何在该类中添加属性? [关闭]
【发布时间】:2012-11-02 17:00:33
【问题描述】:

我创建了三个文件,有

MDActionBar.h,
MDActionBar.m,
MDActionBar.xib

MDActionBar.h

#import <UIKit/UIKit.h>
@interface MDActionBar : UIView{
    NSString* _type;
}
@property (retain, nonatomic) IBOutlet UIButton *label_tips;    
@property (nonatomic,retain) NSString* type;
-(id)initWithFrame:(CGRect)frame;
@end

MDActionBar.m

#import "MDActionBar.h"

@implementation MDActionBar
@synthesize type = _type;
-(id)initWithFrame:(CGRect)frame withType:(NSString*)itype{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:@"MDActionBar" owner:self options:nil];
        NSArray *theView =  [[NSBundle mainBundle] loadNibNamed:@"MDActionBar" owner:self options:nil];
        self = [theView objectAtIndex:0];
        self.frame = frame;
    }
}

我像这样使用这个类:

MDActionBar* mdActionBar = [[[MDActionBar alloc]initWithFrame:UI_TOOLBAR_POSITION_DOWN withType:@"done"] autorelease];
mdActionBar.type = @"done";

MDActionBar.m 中似乎没有名为“type”的属性,

NSLog(@"_type %@",self.type);

它总是在控制台中显示 null。

如果我加载一个 xib,而这个 xib 是一个 UIView,并且 UIView 中没有 'type' 属性,那么它会打印 null???

提前致谢。

【问题讨论】:

  • 你什么时候打电话给NSLog(@"_type %@",self.type);
  • @JackyBoy,谢谢。我用另一种方法调用它:- (IBAction)fn_pressedButton:(id)sender { NSLog(@"_type %@",self.type);}

标签: iphone ios uiview properties xib


【解决方案1】:

您在 init 中传递类型但从未设置类型。你需要把它存放在某个地方。在初始化中

self.type = itype

【讨论】:

  • 我已经做到了,但是在另一种方法中,它打印'null',它无法保持价值。
【解决方案2】:

将 xib 中的视图类从 UIView 更改为 MDActionBar

【讨论】:

  • 我在 xib 中做过,IBOutlet 工作正常。但 'type' 属性不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 2016-03-26
  • 2013-05-25
  • 2021-06-20
相关资源
最近更新 更多