gpt4 book ai didi

ios - 在不同屏幕中重用 xib

转载 作者:行者123 更新时间:2023-11-30 11:12:46 25 4
gpt4 key购买 nike

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

View have inactive state

View have active state

一个屏幕具有相同的 View ,但 UI 略有不同:

enter image description here

所以,我的问题是:我可以使用一个 xib 并调整状态(事件、非事件)并更改不同屏幕的 ui 吗?我该怎么做?

最佳答案

这是此类的一个示例

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

- (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
}

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

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

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

在您想要显示自定义 UI 的 View Controller 中

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

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

#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

.m 文件

#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

这就是在 View Controller 中使用它的方式

LevelAndRankDetails* toolTipCustomView = [[LevelAndRankDetails alloc] initWithFrame:CGRectMake(0, 0, 250, 66)];
toolTipCustomView.backgroundColor = [UIColor blackColor];
[toolTipCustomView setLevel:@(10) andRanking:@(3)];

关于ios - 在不同屏幕中重用 xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52018674/

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