Scott
Scott

Reputation: 444

How can I push this subview into it's own subclass?

I have a basic subview that slides up 80 from the top of the screen when a button is pushed. I've created another class - AdvancedView - that inherits from UIView. What I would like to do Is first of all, push all the code that's responsible for creating this subview into it's own class: AdvancedView. I would then like to somehow set it to where the top 100 of the subview is show showing when the main view loads. I'm going to put a button at on the top showing portion of this subview that will toggle it up and down (I know how to code the button). Here is the code that I need to put into AdvancedView and have the top 100 showing when the main view CalcViewController loads:

#import "CalcViewController.h"
#import "CalculatorBrain.h"
#import "AdvancedView.h"

@property (nonatomic, strong) AdvancedView *myNewView;
@synthesize myNewView = _myNewView;

- (AdvancedView *) myNewView
{
    if (!_myNewView) _myNewView = [[AdvancedView alloc] init];
    return _myNewView;
}

- (IBAction)subView {

    self.myNewView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.myNewView.frame = CGRectMake(0,489, 320, 200);
    [self.view addSubview:self.myNewView];
    float bottomYOfDigitBeingDisplayed = 75;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.myNewView addSubview:button];


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationDuration: 1.0];
    self.myNewView.frame = CGRectMake(0, bottomYOfDigitBeingDisplayed, 320, 400);
    [UIView commitAnimations];
}

Upvotes: 1

Views: 174

Answers (1)

Joel Kravets
Joel Kravets

Reputation: 2471

Your UIView subclass could look like this

.h

@interface AdvancedView : UIView {

}

@end

.m

@implementation AdvancedView

- (id) initWithFrame:(CGRect)rect{

   if(self = [super initWithFrame:rect]){

       self.backgroundColor = [UIColor groupTableViewBackgroundColor];

       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [button addTarget:self 
                   action:@selector(display:)
                   forControlEvents:UIControlEventTouchDown];
       [button setTitle:@"Show View" forState:UIControlStateNormal];
       button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

       [self addSubview:button];

   }

   return self;



}

- (void) display:(id)sender{

    float bottomYOfDigitBeingDisplayed = 75;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationDuration: 1.0];
    self.frame = CGRectMake(0, bottomYOfDigitBeingDisplayed, 320, 400);
    [UIView commitAnimations];
}

@end

Your UIViewController could look like this

#import "CalcViewController.h"
#import "CalculatorBrain.h"
#import "AdvancedView.h"

@property (nonatomic, strong) AdvancedView *myNewView;
@synthesize myNewView = _myNewView;

- (AdvancedView *) myNewView
{
    if (!_myNewView) _myNewView = [[AdvancedView alloc] initWithFrame:
                                                  CGRectMake(0,489, 320, 200);];
    return _myNewView;
}

- (IBAction)subView {

    int advancedTag = 199998;
    self.myNewView.tag = advancedTag;

    if(![self.view viewWithTag:advancedTag]){
        [self.view addSubview:self.myNewView];
    }
}

Upvotes: 1

Related Questions