ios - 使用 subview 类的功能将 subview 添加到主视图

标签 ios iphone objective-c uiview

我想将我的自定义 UIView 添加到我的主视图。

我想使用函数 initWithTitle:description: like:

AchievementView *achievement = [[AchievementView alloc] initWithTitle:@"Test" description:@"The Descr"]];

但这行不通。 tThe initWithTitle:description: 函数必须作为类方法实现。

成就 View .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

成就 View .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];


}

在主视图中调用方法:

- (IBAction)share:(id)sender { 
      AchievementView *achievement = [[AchievementView alloc] init];
      achievement.achievementTtl = @"Test";
      achievement.achievementDescr = @"TestDEscr";
     [achievement showAchievement];
}

我无法使用此功能将 subview 添加到主视图。 我认为“ self ”是错误的。那里应该有什么?

[self addSubview:self.theAchievementView];

最佳答案

您有 2 个问题:如何初始化 View 以及如何将它添加到屏幕。

您可以使用 initWithTitle:description: 方法,您只需在类的实例上使用它,而不是类本身:

[[AchievementView alloc] initWithTitle:@"Test" description:@"The Descr"]];

您是对的,您在 addSubview 调用中有错误的 self。您需要对父 View 的引用。您可以将它传递到您的 AchievementView 类中,但在类外管理它会更清晰。

我还注意到 AchievementViewUIView 的子类,但是您正在其中创建另一个 UIView。直接使用AchievementView会更简单。您的代码应类似于以下内容:

AchievementView.h

@interface AchievementView : UIView

@property (strong) NSString *achievementTtl;
@property (strong) NSString *achievementDescr;

- (id)initWithTitle:(NSString*)achievementTitle description:(NSString*)achievementDescription;

@end

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

主视图

- (IBAction)share:(id)sender { 
    AchievementView *achievement = [[AchievementView alloc] initWithTitle:@"Test" description:@"TestDEscr"];
    [self.view addSubview:achievement];
}

关于ios - 使用 subview 类的功能将 subview 添加到主视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20504495/

相关文章:

iphone - 将NSMutableIndexSet的每个元素加1

ios - applicationWillEnterForeground 的问题

ios - 是否可以通过其动态单元格高度来更改 Collection View 的高度?如果可能的话我们应该使用哪种委托(delegate)方法?

ios - 多个 GoogleService-Info 支持

iphone - 我应该在哪里保存我想长期保存的数据和文件,以及如何防止 iCloud 备份它们

iphone - 调整 UITabBarItem 的大小

iphone - NSLog 指针语法

objective-c - 检测并删除文本中的日期字符串

c++ - 如何在 C++ 中模拟 NSData 对象?

ios - Swift iOS Playground : Error sending deferral props