[英]Add Subview to main view with function of subview class
I want to add my custom UIView to my main view. 我想将自定义UIView添加到我的主视图中。
I want to use the function initWithTitle:description: like: 我想使用函数initWithTitle:description:like:
AchievementView *achievement = [[AchievementView alloc] initWithTitle:@"Test" description:@"The Descr"]];
But this doesn't work. 但这不起作用。 tThe initWithTitle:description: function have to be implemented as class method.
initWithTitle:description:函数必须作为类方法实现。
AchievementView.h AchievementView.h
@interface AchievementView : UIView
@property (strong) NSString *achievementTtl;
@property (strong) NSString *achievementDescr;
@property (strong) UIView *theAchievementView;
- (void)initWithTitle:(NSString*)achievementTitle description:(NSString*)achievementDescription;
- (void)showAchievement;
@end
AchievementView.m AchievementView.m
-(void)initWithTitle:(NSString*)achievementTitle description:(NSString*)achievementDescription {
self.achievementTtl = achievementTitle;
self.achievementDescr = achievementDescription;
}
- (void)showAchievement {
// Create view popup
self.theAchievementView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 423)];
self.theAchievementView.layer.cornerRadius = 8;
self.theAchievementView.layer.masksToBounds = YES;
self.theAchievementView.layer.shadowRadius = 5;
self.theAchievementView.layer.shadowOpacity = .15;
[self.theAchievementView setBackgroundColor:[UIColor whiteColor]];
[self addSubview:self.theAchievementView];
}
Call the method in main view: 在主视图中调用方法:
- (IBAction)share:(id)sender {
AchievementView *achievement = [[AchievementView alloc] init];
achievement.achievementTtl = @"Test";
achievement.achievementDescr = @"TestDEscr";
[achievement showAchievement];
}
I can't add the subview to the main view with this function. 我无法使用此功能将子视图添加到主视图中。 I think the "self" is wrong.
我认为“自我”是错误的。 What should be there?
应该有什么?
[self addSubview:self.theAchievementView];
Number one, you are never adding achievement
as a subview of your main view. 第一,您永远不会将
achievement
添加为主视图的子视图。 Basically you are creating a view offscreen, adding a view to that view, then moving on with whatever you want to do without ever moving the first view onto the screen. 基本上你是在屏幕外创建一个视图,为该视图添加一个视图,然后继续你想要做的任何事情,而不必将第一个视图移动到屏幕上。
As a minimum you should do this: 至少你应该这样做:
- (IBAction)share:(id)sender {
AchievementView *achievement = [[AchievementView alloc] init];
achievement.achievementTtl = @"Test";
achievement.achievementDescr = @"TestDEscr";
[achievement showAchievement];
[self addSubview:achievement];
}
Along with setting the frame size of achievement
which is currently never set (and since most views by default mask to their layer bounds it will mask to (0,0,0,0)). 除了设置当前从未设置的
achievement
的帧大小(并且由于大多数视图默认掩码为其图层边界,因此它将掩盖为(0,0,0,0))。
Then I would seriously reconsider how you are dealing with views and subviews. 然后我会认真重新考虑你如何处理视图和子视图。 If you do things properly, your
initwithtitle
will work just fine. 如果你做得对,你的
initwithtitle
就可以了。
You have 2 questions: how to initialize the view and how to add it to the screen. 您有两个问题:如何初始化视图以及如何将其添加到屏幕。
You can use the initWithTitle:description:
method, you just have to use it on an instance of the class, not the class itself: 你可以使用
initWithTitle:description:
方法,你只需要在类的实例上使用它,而不是类本身:
[[AchievementView alloc] initWithTitle:@"Test" description:@"The Descr"]];
You are correct that you have the wrong self
in your addSubview
call. 你在
addSubview
调用中有错误的self
是对的。 You need a reference to the parent view. 您需要对父视图的引用。 You could pass it into your
AchievementView
class, but it is cleaner to manage that outside of the class. 您可以将它传递到
AchievementView
类中,但在类外部管理它更清晰。
I also noticed that AchievementView
is a subclass of UIView
, but you are creating another UIView
inside it. 我还注意到
AchievementView
是UIView
的子类,但是你在其中创建了另一个UIView
。 It would be simpler to directly use the AchievementView
. 直接使用
AchievementView
会更简单。 Your code should be similar to the below: 您的代码应类似于以下内容:
AchievementView.h AchievementView.h
@interface AchievementView : UIView
@property (strong) NSString *achievementTtl;
@property (strong) NSString *achievementDescr;
- (id)initWithTitle:(NSString*)achievementTitle description:(NSString*)achievementDescription;
@end
AchievementView.m AchievementView.m
- (id)initWithTitle:(NSString*)achievementTitle description:(NSString*)achievementDescription {
if (self = [super initWithFrame:CGRectMake(0, 0, 300, 423)]) {
self.achievementTtl = achievementTitle;
self.achievementDescr = achievementDescription;
self.layer.cornerRadius = 8;
self.layer.masksToBounds = YES;
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = .15;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
Main view 主要观点
- (IBAction)share:(id)sender {
AchievementView *achievement = [[AchievementView alloc] initWithTitle:@"Test" description:@"TestDEscr"];
[self.view addSubview:achievement];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.