我想在我的應用程序中實現單選按鈕 我使用了https://www.cocoacontrols.com/controls/radiobutton(可可控件單選按鈕)。 我希望他們中的一個應該總是被默認選中。我怎麼能實現這一點。iPhone上的自定義單選按鈕的默認選擇
我的單選按鈕的代碼如下:
RadioButton *radioButtonForDay = [[RadioButton alloc] initWithGroupId:@"first group" index:0 ];
RadioButton *radioButtonForWeek = [[RadioButton alloc] initWithGroupId:@"first group" index:1 ];
RadioButton *radioButtonForMonth = [[RadioButton alloc] initWithGroupId:@"first group" index:2 ];
radioButtonForDay.frame = CGRectMake(40,65,22,28);
radioButtonForWeek.frame = CGRectMake(130,65,22,28);
radioButtonForMonth.frame = CGRectMake(195,65,22,28);
[self.view addSubview:radioButtonForDay];
[self.view addSubview:radioButtonForWeek];
[self.view addSubview:radioButtonForMonth];
[RadioButton addObserverForGroupId:@"first group" observer:self];
RadioButton.m
`
進口 「RadioButton.h」
@interface單選按鈕( ) - (void)默認值在裏面; - (void)otherButtonSelected:(id)sender; - (void)handleButtonTap:(id)sender; @end
@implementation單選按鈕
@synthesize的groupId = _groupId; @synthesize index = _index;
static const NSUInteger kRadioButtonWidth = 22; static const NSUInteger kRadioButtonHeight = 22;
static NSMutableArray * rb_instances = nil; static NSMutableDictionary * rb_observers = nil;
編譯標記 - 觀察員
+(無效)addObserverForGroupId:(的NSString *)的groupId觀察者:(!rb_observers)(id)的觀察者{ 如果{ rb_observers = [[ALLOC的NSMutableDictionary] INIT]; }
if ([groupId length] > 0 && observer) {
[rb_observers setObject:observer forKey:groupId];
// Make it weak reference
//[observer release];
}
}
編譯馬克 - 管理實例
+(無效)registerInstance:(單選*)單選按鈕{ 如果(!rb_instances){ rb_instances = [[NSMutableArray alloc] init]; }
[rb_instances addObject:radioButton];
// Make it weak reference
//[radioButton release];
}
編譯標記 - 類級處理程序
+(無效)buttonSelected:(單選按鈕*)RADIOBUTTON {
// Notify observers
if (rb_observers) {
id observer= [rb_observers objectForKey:radioButton.groupId];
if(observer && [observer respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){
[observer radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId];
}
}
// Unselect the other radio buttons
if (rb_instances) {
for (int i = 0; i < [rb_instances count]; i++) {
RadioButton *button = [rb_instances objectAtIndex:i];
if (![button isEqual:radioButton] && [button.groupId isEqualToString:radioButton.groupId]) {
[button otherButtonSelected:radioButton];
}
}
}
}
編譯指示標記 - 對象生命週期
- (id)initWithGroupId:(NSString *)groupId index:(NSUInteger)index { self = [super init];
if (self) {
_groupId = groupId;
_index = index;
// _selected = selected;
[self defaultInit];
}
return self;
}
- (無效)的dealloc { // [_的groupId釋放]; // [_button release]; // [super dealloc]; }
編譯標記 - 點擊處理
- (無效)handleButtonTap:(ID)發送方{ [_button的setSelected:YES]; [RadioButton buttonSelected:self]; }
- (無效)otherButtonSelected:(ID)發送方{ //當其他單選按鈕實例得到了選擇 調用如果(_button.selected){ [_button的setSelected:NO];
} }
編譯標記 - 單選按鈕初始化
- (無效)defaultInit { //設置容器視圖 self.frame = CGRectMake(0,0,kRadioButtonWidth,kRadioButtonHeight);
// Customize UIButton
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0,kRadioButtonWidth, kRadioButtonHeight);
_button.adjustsImageWhenHighlighted = NO;
[_button setImage:[UIImage imageNamed:@"RadioButton-Unselected"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:@"RadioButton-Selected"] forState:UIControlStateSelected];
[_button addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
[RadioButton registerInstance:self];
}
@end `
它的工作原理謝謝..... –
你的歡迎哥們:)你可以投票:) – Rajneesh071
@ Rajneesh071:我也用過這段代碼在我的應用程序,但它正在崩潰和釋放UIViewController的對象。你能幫我解決這個問題嗎? – Piyush