viewcontroller中的子类UIView动作

时间:2011-11-25 08:01:05

标签: iphone objective-c

如何向UIView子类中创建的按钮添加操作?

这是我的代码。这是我第一次在UIView子类中以编程方式编写我的视图。我创建了“指示”按钮,如下所示:

directionsButton = [self buttonWithTitle:@"Directions" target:self selector:@selector(getDirections:) frame:directionsFrame image:buttonImage insertImage:directionsIcon];

现在如何在视图控制器中添加“getDirections”选择器?以下UIView子类被添加为BookViewController

的子视图
#import "BookView.h"

#define GRAY_BOTTOM 44
#define PADDING 5
#define THUMB_WIDTH 50
#define THUMB_HEIGHT 43
#define DIRECTIONS_BUTTON_WIDTH 94
#define BUTTON_HEIGHT 34
#define BUTTON_VERTICAL_INSET 2

static NSArray* grayBottomButtons = nil;

@interface BookView (PrivateMethods)

- (void)setupSubviewsWithContentFrame:(CGRect)frameRect;
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image insertImage:(UIImage*)insertImage;

@end

@implementation BookView

// @synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self setupSubviewsWithContentFrame:frame];

        self.backgroundColor = [UIColor clearColor]; // Make sure clear Background
        self.opaque = NO;

    }
    return self;
}

- (void)setupSubviewsWithContentFrame:(CGRect)frameRect {

    // Setup views

    // Create frame for white background
    CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    WhiteRoundedView *whiteBg = [[WhiteRoundedView alloc] initWithFrame:frame];

    // Create frame for gray bottom view
    CGRect gFrame = CGRectMake(0, (self.frame.size.height - GRAY_BOTTOM), self.frame.size.width, GRAY_BOTTOM);
    GrayBottomView *grayBg = [[GrayBottomView alloc] initWithFrame:gFrame];

    // Create salon thumbnail view
    UIImageView *salonThumb = [[UIImageView alloc] initWithFrame:CGRectMake(PADDING, PADDING, THUMB_WIDTH, THUMB_HEIGHT)];
    // Set salonThumb image
    salonThumb.image = [UIImage imageNamed:@"SalonThumb.png"];

    // Set salonName UILabel
    UILabel *salonName = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), PADDING, 200, 15)];
    // Set text
    salonName.text = @"Salon Aria";
    // Set font
    salonName.font = [UIFont fontWithName:@"PTSans-Bold" size:15.0];
    // Set Alignment
    salonName.textAlignment = UITextAlignmentLeft;
    // Set font color
    salonName.textColor = [UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:1.0];


    // Set salonAddressLineOne UILabel (Line One)
    UILabel *salonAddressLineOne = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), (PADDING + 15), 140, 13)];
    // Set text
    salonAddressLineOne.text = @"6325 Main St, Suite 140";
    // Set font
    salonAddressLineOne.font = [UIFont fontWithName:@"PTSans-Regular" size:13.0];
    // Set Alignment
    salonAddressLineOne.textAlignment = UITextAlignmentLeft;
    // Set font color
    salonAddressLineOne.textColor = [UIColor colorWithRed:40.0f/255.0 green:40.0f/255.0 blue:40.0f/255.0 alpha:1.0];

    // Set salonAddressLineTwo UILabel (Line Two)
    UILabel *salonAddressLineTwo = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), (PADDING + 28), 140, 13)];
    // Set text
    salonAddressLineTwo.text = @"Woodridge, IL 60517";
    // Set font
    salonAddressLineTwo.font = [UIFont fontWithName:@"PTSans-Regular" size:13.0];
    // Set font color
    salonAddressLineTwo.textColor = [UIColor colorWithRed:40.0f/255.0 green:40.0f/255.0 blue:40.0f/255.0 alpha:1.0];
    // Set Alignment
    salonAddressLineTwo.textAlignment = UITextAlignmentLeft;

    // Set the background image with stretchable type for all buttons on this view
    UIImage *buttonImage = [[UIImage imageNamed:@"UIButton.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];

    // Set the insert image (directions icon)
    UIImage *directionsIcon = [[UIImage imageNamed:@"DirectionsIcon.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
    // Set the directions button frame
    CGRect directionsFrame = CGRectMake(self.frame.size.width - PADDING - DIRECTIONS_BUTTON_WIDTH, PADDING, DIRECTIONS_BUTTON_WIDTH, BUTTON_HEIGHT);
    // Add the button
    directionsButton = [self buttonWithTitle:@"Directions" target:self selector:@selector(getDirections:) frame:directionsFrame image:buttonImage insertImage:directionsIcon];

    // Create the salonMap with frame
    salonMap = [[MKMapView alloc] initWithFrame:
                CGRectMake(PADDING, (PADDING*2 + THUMB_HEIGHT), (self.frame.size.width - PADDING*2), (self.frame.size.height - THUMB_HEIGHT - PADDING*3 - GRAY_BOTTOM))];

    // Add view in proper order and location

    [self addSubview:whiteBg];

    [whiteBg addSubview:grayBg];

    [whiteBg addSubview:salonThumb];

    [whiteBg addSubview:salonName];

    [whiteBg addSubview:salonAddressLineOne];

    [whiteBg addSubview:salonAddressLineTwo];

    [whiteBg addSubview:directionsButton];

    [whiteBg addSubview:salonMap];

    [self setNeedsDisplay];

}

- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image insertImage:(UIImage*)insertImage
{
    // Create button
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    // Set button content alignment
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    // Set button title
    [button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    // Set button title color
    [button setTitleColor:[UIColor colorWithRed:91.0f/255.0 green:82.0f/255.0 blue:82.0f/255.0 alpha:1.0] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    // Add the background image
    [button setBackgroundImage:image forState:UIControlStateNormal];
    // Add Events
    [button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
    // in case the parent view draws with a custom color or gradient, use a transparent color
    [button setBackgroundColor:[UIColor clearColor]];  
    // Set titleShadowColor this way (apparently, titleLabel.shadowcolor does not work)
    [button setTitleShadowColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:.75] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];

    // Set button titleLabel properties  
    button.titleLabel.font = [UIFont fontWithName:@"PTSans-Bold" size:13.0];
    button.titleLabel.shadowOffset = CGSizeMake(1, 1);    

    // If there is an insertImage icon
    if(insertImage != nil) 
    {
        // padding between image and text
        int seperate = 6; 
        // Image and title frame (used to center text/image)
        int width = (button.titleLabel.frame.size.width + insertImage.size.width + seperate);
        int buttonWidth = button.frame.size.width;
        int xPadding = (buttonWidth - width)/2;
        int yPadding = (button.frame.size.height - insertImage.size.height)/2;

        // Create an image view for the image (standard icon is 12x13)
        UIImageView *insertImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPadding, yPadding, insertImage.size.width, insertImage.size.height)];
        // add the insertImage to the view
        [insertImageView setImage:insertImage];
        // add the insertImageView to the button
        [button addSubview:insertImageView];
        // Add offset with title
        button.titleEdgeInsets = UIEdgeInsetsMake(BUTTON_VERTICAL_INSET, insertImage.size.width + seperate, 0, 0);
    }
    else 
    {
        // No image so no horizontal offset but PTSans font needs a 2 vertical offset
        button.titleEdgeInsets = UIEdgeInsetsMake(BUTTON_VERTICAL_INSET, 0, 0, 0);
    }
    return button;
}


@end

1 个答案:

答案 0 :(得分:1)

从视图控制器中,对于视图中的每个按钮,执行以下操作:

[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];

或者,您可以通过按住Ctrl键从按钮拖动到文件所有者(视图控制器)的头文件来设置Interface Builder中的连接。这将要求您定义方法名称,并将其全部链接到您。