我在我的應用程序中有一個視圖,它有一些基於服務器返回的項目數量的按鈕。因此,如果服務器返回說10個項目,應該有10個按鈕,點擊每個按鈕應該調用一個不同的人。Iphone的自定義UIButton
爲了上述目的,我創建了一個從UIButton派生的自定義按鈕類。
@implementation HopitalButton
@synthesize index;
@synthesize button_type;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImage* img = [UIImage imageNamed:@"dr_btn.png"];
[img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[self setBackgroundImage:img forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.698 green:0.118 blue:0.376 alpha:1] forState:UIControlStateNormal] ;
[self setFont:[UIFont fontWithName:@"Helvetica Bold" size:13]];
self.titleLabel.textColor = [UIColor colorWithRed:178 green:48 blue:95 alpha:1];
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
現在上述代碼的問題是,它不會創建類似於默認情況下在界面構建器中創建的按鈕的按鈕。邊界不見了。
我用下面的代碼創建上述類型的按鈕:
HopitalButton* hb = [[HopitalButton alloc] init];
hb.button_type = @"call";
hb.frame = CGRectMake(50, 50 + i * 67, 220, 40);
[self.scroll_view addSubview:hb];
[hb setTitle:[[[self.office_full_list objectAtIndex:i] objectForKey:@"Staff" ]objectForKey:@"FullName"] forState:UIControlStateNormal];
hb.index = [NSNumber numberWithInt:[self.button_items count]];
[self.button_items insertObject:hb atIndex:[self.button_items count]];
[hb addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
我沒有找到一個方法來設置這個自定義按鈕按鈕類型。 有沒有辦法可以做到這一點?或者有更好的方法來設計代碼。
但是當我使用索引來找出哪個按鈕被按下,因爲有在視圖中 – Amal 2009-11-02 19:32:05
多個按鈕設置標籤按鈕的索引是我的程序很重要。我更新了代碼 – coneybeare 2009-11-02 19:39:39
你從哪裏得到可拉伸的紅色按鈕圖像? – bentford 2009-11-03 01:22:52