2009-11-02 6 views
12

के लिए कस्टम UIButton मेरे पास मेरे ऐप में एक दृश्य है जिसमें सर्वर द्वारा लौटाई गई वस्तुओं की संख्या के आधार पर कई बटन हैं। तो यदि सर्वर 10 आइटम कहता है, तो 10 बटन होना चाहिए और प्रत्येक बटन पर क्लिक करना एक अलग व्यक्ति को कॉल करना चाहिए।आईफोन

उपरोक्त उद्देश्य के लिए मैंने 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]; 

मैं इस कस्टम बटन के लिए बटन प्रकार सेट करने के लिए एक रास्ता नहीं मिल रहा हूँ। क्या कोई तरीका है कि मैं इसे कर सकता हूं? या कोड को डिजाइन करने का एक बेहतर तरीका है।

उत्तर

27

आपको सबसे पहले एक सीमा के साथ एक खींचे छवि के साथ शुरू:

alt text http://grab.by/4lP

तो फिर तुम पृष्ठभूमि के रूप में विस्तारित छवि के साथ एक बटन बनाने के लिए और पाठ लागू होते हैं।

INDEX_OFFSET = 82753; // random 

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)]; 
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal]; 
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; 
[sampleButton setTag:<INDEX>+INDEX_OFFSET]; 
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal]; 
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:sampleButton]; 

जाहिर है, आप फ्रेम मूल और आकार अपने अनुप्रयोग मैच के लिए, साथ ही लक्ष्य, चयनकर्ता, और शीर्षक को समायोजित करने की आवश्यकता होगी। और

+0

लेकिन सूचकांक मेरा कार्यक्रम के लिए महत्वपूर्ण है के रूप में मैं इंडेक्स का उपयोग करके पता लगाने के लिए जो बटन दबाया गया था के बाद से वहाँ दृश्य – Amal

+0

में एक से अधिक बटन बटन के लिए कोई टैग सेट कर रहे हैं। मैंने कोड – coneybeare

+0

अपडेट किया जहां आपको खिंचाव लाल बटन छवि कहां मिली? – bentford

2

आप के बजाय initWithFrame: rect बुला नहीं होना चाहिए:

HopitalButton* hb = [[HopitalButton alloc] init]; 
6
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; 

setFont अब हटाई गई है, titleLabel.font संपत्ति का उपयोग करने के बजाय

sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20]; 
3

आप कस्टम के लिए व्यक्तिगत वर्ग का उपयोग कर सकते राउंडएक्ट बटन जो आपकी विशिष्ट फ्रेम शैली के साथ

के साथ पूरी परियोजना में उपयोगी हो सकता है 0
#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 
@interface CustomRoundRectButton : UIButton 
@end 


#import "CustomRoundRectButton.h" 
@implementation CustomRoundRectButton 
- (void)drawRect:(CGRect)rect 
{ 
[[self layer] setMasksToBounds:YES]; 
[self.layer setCornerRadius:10.0f]; 
[self.layer setBorderColor:[UIColor grayColor].CGColor]; 
[self.layer setBorderWidth:1.0]; 
} 
@end 

इसमें आपको बटन प्रकार कस्टम का चयन करना होगा और कस्टम क्लाउंडरक्टबटन के रूप में अपनी कक्षा का चयन करना होगा।

For Simple custom button we can use as below 

-(UIBarButtonItem*)BackButton 
{ 
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
[btn setFrame:CGRectMake(0, 0, 30, 30)]; 
[btn addTarget:self action:@selector(actionBack) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem*barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease]; 
return barBtn; 
}