Permalink
Browse files

サンプルを作成

  • Loading branch information...
1 parent b9a89a7 commit d54b3b687035805faf923a93df2e9ee227969b4d @t4ku committed Mar 24, 2012
View
23 RadioButton.xcodeproj/project.pbxproj
@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
+ A71CB0AE151C4E79009D3506 /* RadioButton-Selected.png in Resources */ = {isa = PBXBuildFile; fileRef = A71CB0AC151C4E79009D3506 /* RadioButton-Selected.png */; };
+ A71CB0AF151C4E79009D3506 /* RadioButton-Unselected.png in Resources */ = {isa = PBXBuildFile; fileRef = A71CB0AD151C4E79009D3506 /* RadioButton-Unselected.png */; };
A728E8F5133987E400CF1F36 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A728E8F4133987E400CF1F36 /* UIKit.framework */; };
A728E8F7133987E400CF1F36 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A728E8F6133987E400CF1F36 /* Foundation.framework */; };
A728E8F9133987E400CF1F36 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A728E8F8133987E400CF1F36 /* CoreGraphics.framework */; };
@@ -16,23 +18,28 @@
A728E908133987E400CF1F36 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = A728E906133987E400CF1F36 /* MainWindow.xib */; };
A728E90B133987E400CF1F36 /* RadioButtonViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A728E90A133987E400CF1F36 /* RadioButtonViewController.m */; };
A728E90E133987E400CF1F36 /* RadioButtonViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A728E90C133987E400CF1F36 /* RadioButtonViewController.xib */; };
+ A728E9161339886F00CF1F36 /* RadioButton.m in Sources */ = {isa = PBXBuildFile; fileRef = A728E9151339886F00CF1F36 /* RadioButton.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ A71CB0AC151C4E79009D3506 /* RadioButton-Selected.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "RadioButton-Selected.png"; sourceTree = "<group>"; };
+ A71CB0AD151C4E79009D3506 /* RadioButton-Unselected.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "RadioButton-Unselected.png"; sourceTree = "<group>"; };
A728E8F0133987E400CF1F36 /* RadioButton.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RadioButton.app; sourceTree = BUILT_PRODUCTS_DIR; };
A728E8F4133987E400CF1F36 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
A728E8F6133987E400CF1F36 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
A728E8F8133987E400CF1F36 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
- A728E8FC133987E400CF1F36 /* RadioButton-Info.plist */ = {isa = PBXFileReference; path = "RadioButton-Info.plist"; sourceTree = "<group>"; };
+ A728E8FC133987E400CF1F36 /* RadioButton-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "RadioButton-Info.plist"; sourceTree = "<group>"; };
A728E8FE133987E400CF1F36 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
- A728E900133987E400CF1F36 /* RadioButton-Prefix.pch */ = {isa = PBXFileReference; path = "RadioButton-Prefix.pch"; sourceTree = "<group>"; };
+ A728E900133987E400CF1F36 /* RadioButton-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RadioButton-Prefix.pch"; sourceTree = "<group>"; };
A728E901133987E400CF1F36 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
- A728E903133987E400CF1F36 /* RadioButtonAppDelegate.h */ = {isa = PBXFileReference; path = RadioButtonAppDelegate.h; sourceTree = "<group>"; };
+ A728E903133987E400CF1F36 /* RadioButtonAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RadioButtonAppDelegate.h; sourceTree = "<group>"; };
A728E904133987E400CF1F36 /* RadioButtonAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RadioButtonAppDelegate.m; sourceTree = "<group>"; };
A728E907133987E400CF1F36 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainWindow.xib; sourceTree = "<group>"; };
- A728E909133987E400CF1F36 /* RadioButtonViewController.h */ = {isa = PBXFileReference; path = RadioButtonViewController.h; sourceTree = "<group>"; };
+ A728E909133987E400CF1F36 /* RadioButtonViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RadioButtonViewController.h; sourceTree = "<group>"; };
A728E90A133987E400CF1F36 /* RadioButtonViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RadioButtonViewController.m; sourceTree = "<group>"; };
A728E90D133987E400CF1F36 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/RadioButtonViewController.xib; sourceTree = "<group>"; };
+ A728E9141339886F00CF1F36 /* RadioButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RadioButton.h; sourceTree = "<group>"; };
+ A728E9151339886F00CF1F36 /* RadioButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RadioButton.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -79,13 +86,17 @@
A728E8FA133987E400CF1F36 /* RadioButton */ = {
isa = PBXGroup;
children = (
+ A71CB0AC151C4E79009D3506 /* RadioButton-Selected.png */,
+ A71CB0AD151C4E79009D3506 /* RadioButton-Unselected.png */,
A728E903133987E400CF1F36 /* RadioButtonAppDelegate.h */,
A728E904133987E400CF1F36 /* RadioButtonAppDelegate.m */,
A728E906133987E400CF1F36 /* MainWindow.xib */,
A728E909133987E400CF1F36 /* RadioButtonViewController.h */,
A728E90A133987E400CF1F36 /* RadioButtonViewController.m */,
A728E90C133987E400CF1F36 /* RadioButtonViewController.xib */,
A728E8FB133987E400CF1F36 /* Supporting Files */,
+ A728E9141339886F00CF1F36 /* RadioButton.h */,
+ A728E9151339886F00CF1F36 /* RadioButton.m */,
);
path = RadioButton;
sourceTree = "<group>";
@@ -151,6 +162,8 @@
A728E8FF133987E400CF1F36 /* InfoPlist.strings in Resources */,
A728E908133987E400CF1F36 /* MainWindow.xib in Resources */,
A728E90E133987E400CF1F36 /* RadioButtonViewController.xib in Resources */,
+ A71CB0AE151C4E79009D3506 /* RadioButton-Selected.png in Resources */,
+ A71CB0AF151C4E79009D3506 /* RadioButton-Unselected.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -164,6 +177,7 @@
A728E902133987E400CF1F36 /* main.m in Sources */,
A728E905133987E400CF1F36 /* RadioButtonAppDelegate.m in Sources */,
A728E90B133987E400CF1F36 /* RadioButtonViewController.m in Sources */,
+ A728E9161339886F00CF1F36 /* RadioButton.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -276,6 +290,7 @@
A728E913133987E400CF1F36 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
View
BIN RadioButton/RadioButton-Selected.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN RadioButton/RadioButton-Unselected.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
30 RadioButton/RadioButton.h
@@ -0,0 +1,30 @@
+//
+// RadioButton.h
+// RadioButton
+//
+// Created by ohkawa on 11/03/23.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@protocol RadioButtonDelegate <NSObject>
+-(void)radioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString*)groupId;
+@end
+
+@interface RadioButton : UIView {
+ NSString *_groupId;
+ NSUInteger _index;
+ UIButton *_button;
+}
+@property(nonatomic,retain)NSString *groupId;
+@property(nonatomic,assign)NSUInteger index;
+
+-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index;
+
++(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer;
+
+-(void)defaultInit;
+-(void)otherButtonSelected:(id)sender;
+-(void)handleButtonTap:(id)sender;
+@end
View
133 RadioButton/RadioButton.m
@@ -0,0 +1,133 @@
+//
+// RadioButton.m
+// RadioButton
+//
+// Created by ohkawa on 11/03/23.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "RadioButton.h"
+
+
+@implementation RadioButton
+
+@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;
+
+#pragma mark - Observer
+
++(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer{
+ if(!rb_observers){
+ rb_observers = [[NSMutableDictionary alloc] init];
+ }
+
+ if ([groupId length] > 0 && observer) {
+ [rb_observers setObject:observer forKey:groupId];
+ // Make it weak reference
+ [observer release];
+ }
+}
+
+#pragma mark - Manage Instances
+
++(void)registerInstance:(RadioButton*)radioButton{
+ if(!rb_instances){
+ rb_instances = [[NSMutableArray alloc] init];
+ }
+
+ [rb_instances addObject:radioButton];
+ // Make it weak reference
+ [radioButton release];
+}
+
+#pragma mark - Class level handler
+
++(void)buttonSelected:(RadioButton*)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 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 otherButtonSelected:radioButton];
+ }
+ }
+ }
+}
+
+#pragma mark - Object Lifecycle
+
+-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index{
+ self = [self init];
+ if (self) {
+ _groupId = groupId;
+ _index = index;
+ }
+ return self;
+}
+
+- (id)init{
+ self = [super init];
+ if (self) {
+ [self defaultInit];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [_groupId release];
+ [_button release];
+ [super dealloc];
+}
+
+
+#pragma mark - Tap handling
+
+-(void)handleButtonTap:(id)sender{
+ [_button setSelected:YES];
+ [RadioButton buttonSelected:self];
+}
+
+-(void)otherButtonSelected:(id)sender{
+ [_button setSelected:NO];
+}
+
+#pragma mark - RadioButton init
+
+-(void)defaultInit{
+ // Setup container view
+ 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
View
3 RadioButton/RadioButtonViewController.h
@@ -7,8 +7,9 @@
//
#import <UIKit/UIKit.h>
+#import "RadioButton.h"
-@interface RadioButtonViewController : UIViewController {
+@interface RadioButtonViewController : UIViewController<RadioButtonDelegate> {
}
View
50 RadioButton/RadioButtonViewController.m
@@ -7,6 +7,8 @@
//
#import "RadioButtonViewController.h"
+#import "RadioButton.h"
+
@implementation RadioButtonViewController
@@ -25,13 +27,55 @@ - (void)didReceiveMemoryWarning
#pragma mark - View lifecycle
-/*
-// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
+ UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 400)];
+ container.backgroundColor = [UIColor lightGrayColor];
+ [self.view addSubview:container];
+
+ UILabel *questionText = [[UILabel alloc] initWithFrame:CGRectMake(0,0,280,20)];
+ questionText.backgroundColor = [UIColor clearColor];
+ questionText.text = @"Which color do you like?";
+ [container addSubview:questionText];
+
+ RadioButton *rb1 = [[RadioButton alloc] initWithGroupId:@"first group" index:0];
+ RadioButton *rb2 = [[RadioButton alloc] initWithGroupId:@"first group" index:1];
+ RadioButton *rb3 = [[RadioButton alloc] initWithGroupId:@"first group" index:2];
+
+ rb1.frame = CGRectMake(10,30,22,22);
+ rb2.frame = CGRectMake(10,60,22,22);
+ rb3.frame = CGRectMake(10,90,22,22);
+
+ UILabel *label1 =[[UILabel alloc] initWithFrame:CGRectMake(40, 30, 60, 20)];
+ label1.backgroundColor = [UIColor clearColor];
+ label1.text = @"Red";
+ [container addSubview:label1];
+
+ UILabel *label2 =[[UILabel alloc] initWithFrame:CGRectMake(40, 60, 60, 20)];
+ label2.backgroundColor = [UIColor clearColor];
+ label2.text = @"Green";
+ [container addSubview:label2];
+
+ UILabel *label3 =[[UILabel alloc] initWithFrame:CGRectMake(40, 90, 60, 20)];
+ label3.backgroundColor = [UIColor clearColor];
+ label3.text = @"Blue";
+ [container addSubview:label3];
+
+ [RadioButton addObserverForGroupId:@"first group" observer:self];
+
+ [container addSubview:rb1];
+ [container addSubview:rb2];
+ [container addSubview:rb3];
+
+ [rb1 release];
+ [rb2 release];
+
[super viewDidLoad];
}
-*/
+
+-(void)radioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString *)groupId{
+ NSLog(@"changed to %d in %@",index,groupId);
+}
- (void)viewDidUnload
{

0 comments on commit d54b3b6

Please sign in to comment.