在不同的屏幕中重复使用Xib

时间:2018-08-25 15:25:45

标签: ios swift xib

我有4个屏幕,它们的视图几乎相同:

View have inactive state

View have active state

一个屏幕的视图相同,但用户界面略有不同:

enter image description here

所以,我的问题是:我可以使用一个xib并调整状态(活动,不活动)并为不同的屏幕更改ui吗?我该怎么办?

1 个答案:

答案 0 :(得分:0)

这是此类课程的一个例子

在自定义XIB类的.m文件中,如果您使用的是Objective-c。

Id_number = id_number

在自定义XIB类的.h文件中,如果您使用的是Objective-c。

- (void)foo:(NSString*)labelText andButtonText:(NSString*)buttonTitle {
  //Do your code here for some screen like change labels and button text
}

- (void)bar:(NSString*)labelText andButtonText:(NSString*)buttonTitle {
  //Do your code here for some another screen and change labels and button text
}

在视图控制器中要显示自定义UI的地方

创建xib的实例或通过interfacebuilder添加 在自定义类的实例上,根据需要调用该方法。

下面是我在我的一个项目中使用的类,用于获得清晰的理解。

- (void)foo:(NSString*)labelText andButtonText:(NSString*)buttonTitle;

- (void)bar:(NSString*)labelText andButtonText:(NSString*)buttonTitle;

.m文件

#import <UIKit/UIKit.h>
#import "DYRateView.h"

@interface LevelAndRankDetails : UIView
@property (nonatomic, strong) IBOutlet UIView* contentView;
@property (nonatomic, strong) IBOutlet UIView* viewContainer;
@property (nonatomic, strong) IBOutlet UILabel* lblLevel;
@property (nonatomic, strong) IBOutlet UILabel* lblRanking;
@property (weak, nonatomic) IBOutlet DYRateView *viewRate;

- (void)setLevel:(NSNumber*)level andRanking:(NSNumber*)ranking;
- (void)setupUI;
@end

这就是您在视图控制器中使用它的方式

#import "LevelAndRankDetails.h"
#import "AppDelegate.h"
#import "Constants.h"

@implementation LevelAndRankDetails

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]){
        [self commonSetup];
    }
    return self;
}

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

    if (self)
    {
        [self commonSetup];

    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)viewFromNibForClass {
    [[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
    [self addSubview:self.contentView];
    self.contentView.frame = self.bounds;
}

- (void)commonSetup {

    [self viewFromNibForClass];

    //For View's Corner Radius
    self.contentView.layer.cornerRadius = 12;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = kDefaultBackgroundGreyColor;
    self.viewContainer.backgroundColor = kDefaultBackgroundGreyColor;//[UIColor clearColor];
    self.backgroundColor = kDefaultBackgroundGreyColor;
    //self.viewContainer.backgroundColor = UIColorFromRGB(0xBB9657);//[kLearnFromLightColor colorWithAlphaComponent:0.5];

    self.viewRate.rate = 0;
    self.viewRate.editable = NO;
    self.viewRate.delegate = nil;
    self.viewRate.alignment = RateViewAlignmentCenter;
    self.viewRate.backgroundColor = [UIColor clearColor];
    [self.viewRate setEmptyStarImage:[UIImage imageNamed:@"StarEmpty"]];
    UIImage* imageFullStar = [[UIImage imageNamed:@"StarFull"] imageTintedWithColor:kSliderDarkYellowColor];
    [self.viewRate setFullStarImage:imageFullStar];
    self.lblLevel.textColor = [UIColor whiteColor];
    self.lblRanking.textColor = [UIColor whiteColor];
    //For Teacher label
}

- (void)setupUI {
    self.contentView.layer.cornerRadius = 0;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.viewContainer.backgroundColor = [UIColor clearColor];//[UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
}

- (void)setRanking:(CGFloat)ranking {
    self.viewRate.rate = ranking;
}

- (void)setLevel:(NSNumber*)level {
    self.lblLevel.text = [NSString stringWithFormat:@"Level : %@",level];
}

- (void)setLevel:(NSNumber*)level andRanking:(NSNumber*)ranking {

    if (level.integerValue > 0) {
        [self setLevel:level];
    }
    if (ranking.doubleValue > 0) {
        CGFloat rankingConverted = ranking.floatValue;
        [self setRanking:rankingConverted];
    }
}


@end