我怎样才能将这个子视图推入它自己的子类?

时间:2012-02-11 13:43:43

标签: objective-c ios xcode

我有一个基本的子视图,当按下按钮时,它会从屏幕顶部向上滑动80。我创建了另一个类 - AdvancedView - 继承自UIView。我想做的事情首先,将负责创建此子视图的所有代码推送到它自己的类中:AdvancedView。然后我想以某种方式将它设置为子视图的前100个显示何时加载主视图的位置。我将在顶部显示一个按钮,显示该子视图的一部分,它将上下切换(我知道如何编码按钮)。这是我需要放入AdvancedView的代码,并在主视图CalcViewController加载时显示前100个:

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

1 个答案:

答案 0 :(得分:1)

您的UIView子类看起来像这样

·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

你的UIViewController看起来像这样

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