2011-11-25 68 views
2

我該如何着手添加對在UIView子類中創建的按鈕的操作?子類viewcontroller中的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

的子視圖從視圖控制器,對於視圖中的每個按鈕做這樣的事情:

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

或者你可以設置界面生成器中的連接通過從按鈕拖動到文件所有者(視圖控制器)的頭文件。這會要求您定義一個方法名稱,並將其鏈接起來。

+0

我以編程方式純粹創建視圖。我將添加我的代碼。 – morcutt