简体   繁体   中英

Add Subview to main view with function of subview class

I want to add my custom UIView to my main view.

I want to use the function 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.

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

-(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. 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)).

Then I would seriously reconsider how you are dealing with views and subviews. If you do things properly, your initwithtitle will work just fine.

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:

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

You are correct that you have the wrong self in your addSubview call. 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.

I also noticed that AchievementView is a subclass of UIView , but you are creating another UIView inside it. It would be simpler to directly use the AchievementView . Your code should be similar to the below:

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

Main view

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM