मैं अपने फार्म के लिए चेकबॉक्स नियंत्रण जोड़ने की जरूरत है। मैं जानता हूँ कि iOS SDK में तरह का कोई नियंत्रण नहीं है। मैं ये कैसे करूं?
आईओएस आवेदन में चेकबॉक्स
यह भी मुझे पागल गाड़ी चला कर दिया गया है और मैं किसी अन्य समाधान है कि मेरे लिए अच्छी तरह से काम करता है और छवियों का उपयोग करने के लिए बचा जाता है पाया।
- इंटरफ़ेस बिल्डर के लिए एक नया लेबल वस्तु जोड़ें।
- Xcode में एक IBOutlet संपत्ति बना सकते हैं और इसे करने के लिए इसे कनेक्ट। नीचे दिए गए कोड मैं इसे बुलाया गया है में 'fullyPaid' के रूप में मैं अगर किसी को पूरी तरह से एक धनराशि का भुगतान किया है जानना चाहते हैं।
- 2 कार्यों के नीचे जोड़ें। 'TouchesBegan' फ़ंक्शन जांच करता है अगर आप 'fullyPaid' लेबल ऑब्जेक्ट के अंदर कहीं न कहीं छुआ और यदि हां, तो यह togglePaidStatus 'समारोह कहता है। 'TogglePaidStatus' समारोह दो श्रृंखलाएं जिनमें एक खाली बॉक्स (\ u2610) और एक जाँच बॉक्स (\ u2611) क्रमशः का प्रतिनिधित्व यूनिकोड वर्ण है सेट करता है। तो फिर यह तुलना क्या 'fullyPaid' ऑब्जेक्ट में वर्तमान में है और अन्य तार के साथ यह टॉगल करता है।
आप viewDidLoad समारोह में togglePaidStatus समारोह कॉल करने के लिए एक रिक्त स्ट्रिंग पर इसे शुरू में सेट कर सकते हैं।
जाहिर है आप उन चेकबॉक्स टॉगल करता है, तो लेबल सक्षम नहीं है को रोकने के लिए अतिरिक्त चेकों जोड़ सकते हैं, लेकिन यह है कि नीचे दिखाया गया है नहीं है।
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (CGRectContainsPoint([fullyPaid frame], [touch locationInView:self.view]))
{
[self togglePaidStatus];
}
}
-(void) togglePaidStatus
{
NSString *untickedBoxStr = [[NSString alloc] initWithString:@"\u2610"];
NSString *tickedBoxStr = [[NSString alloc] initWithString:@"\u2611"];
if ([fullyPaid.text isEqualToString:tickedBoxStr])
{
fullyPaid.text = untickedBoxStr;
}
else
{
fullyPaid.text = tickedBoxStr;
}
[tickedBoxStr release];
[untickedBoxStr release];
}
आमतौर पर, आप चेकबॉक्स जैसी कार्यप्रणाली के लिए UISwitch का प्रयोग करेंगे।
आपको दो चित्र (चेक / अनियंत्रित) के साथ एक छवि नियंत्रण का उपयोग और छवियों स्विचन जब वे नियंत्रण स्पर्श द्वारा हालांकि अपने खुद के रोल कर सकता /
आप विकल्पों में से एक समूह दिखा रहे हैं और उपयोगकर्ता उन में से एक का चयन कर सकते हैं, तो सही का चिह्न गौण और चयनित पंक्ति पर एक अलग पाठ रंग के साथ एक tableview का उपयोग करें।
आप एक ही विकल्प है, तो आपका सर्वश्रेष्ठ दांव एक स्विच का उपयोग करने के लिए है। आप नहीं या, एक जाँच बॉक्स के लिए एक खाली बॉक्स और चयनित छवि के सामान्य छवि स्थापित करने के लिए एक बटन का उपयोग करने के, नहीं करना चाहते कर सकते हैं। आप उन दो छवियों खुद कर सकते हैं या उनके लिए उपयोग करने के लिए शेयर ग्राफ़िक ढूंढने के लिए होगा।
करने के लिए विस्तार Adrean के विचार , मैं इस हासिल कर लिया है एक बहुत ही सरल दृष्टिकोण का उपयोग कर।
मेरा विचार बटन को बदलने (देता है कहने के लिए है checkBtn) पाठ अपनी दशा के आधार पर, और उसके बाद अपने में बटन के राज्य बदलने के IBAction।
नीचे दिए गए कोड मैं यह कैसे किया है:
- (void)viewDidLoad
{
[super viewDidLoad];
[checkBtn setTitle:@"\u2610" forState:UIControlStateNormal]; // uncheck the button in normal state
[checkBtn setTitle:@"\u2611" forState:UIControlStateSelected]; // check the button in selected state
}
- (IBAction)checkButtonTapped:(UIButton*)sender {
sender.selected = !sender.selected; // toggle button's selected state
if (sender.state == UIControlStateSelected) {
// do something when button is checked
} else {
// do something when button is unchecked
}
}
यहाँ iphone के लिए चेकबॉक्स का मेरा संस्करण है।
यह एकल वर्ग जो UIButton फैली हुई है। यह सरल तो मैं इसे यहाँ पेस्ट जाएगा।
CheckBoxButton.h फ़ाइल सामग्री
#import <UIKit/UIKit.h>
@interface CheckBoxButton : UIButton
@property(nonatomic,assign)IBInspectable BOOL isChecked;
@end
CheckBoxButton.m फ़ाइल सामग्री
#import "CheckBoxButton.h"
@interface CheckBoxButton()
@property(nonatomic,strong)IBInspectable UIImage* checkedStateImage;
@property(nonatomic,strong)IBInspectable UIImage* uncheckedStateImage;
@end
@implementation CheckBoxButton
-(id)init
{
self = [super init];
if(self)
{
[self addTarget:self action:@selector(switchState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self addTarget:self action:@selector(switchState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self addTarget:self action:@selector(switchState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)setIsChecked:(BOOL)isChecked
{
_isChecked = isChecked;
if(isChecked)
{
[self setImage:self.checkedStateImage forState:UIControlStateNormal];
}
else
{
[self setImage:self.uncheckedStateImage forState:UIControlStateNormal];
}
}
-(void)switchState
{
self.isChecked = !self.isChecked;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
@end
आप दृश्य स्टूडियो की विशेषता निरीक्षक में जाँच / अनियंत्रित के साथ-साथ isChecked संपत्ति के लिए छवियों को सेट कर सकते हैं।

अगली छवि पर की तरह स्टोरीबोर्ड या xib, सरल ऐड UIButton और सेट कस्टम कक्षा में CheckBoxButton को जोड़ने के लिए।

बटन UIControlEventValueChanged घटना, हर बार जब isChecked राज्य बदल गया है भेज देंगे।
मैं इस प्रोग्राम के रूप में करने के लिए, और भी समस्या यह है कि हिट क्षेत्र वास्तव में बहुत छोटा था हल चाहता था। यह माइक और माइक टिप्पणीकार आगा सहित विभिन्न स्रोतों से अनुकूलित है।
अपने शीर्षक में
@interface YourViewController : UIViewController {
BOOL checkboxSelected;
UIButton *checkboxButton;
}
@property BOOL checkboxSelected;;
@property (nonatomic, retain) UIButton *checkboxButton;
-(void)toggleButton:(id)sender;
और अपने कार्यान्वयन में
// put this in your viewDidLoad method. if you put it somewhere else, you'll probably have to change the self.view to something else
// create the checkbox. the width and height are larger than actual image, because we are creating the hit area which also covers the label
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(100, 60,120, 44)];
[checkBox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
// uncomment below to see the hit area
// [checkBox setBackgroundColor:[UIColor redColor]];
[checkBox addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
// make the button's image flush left, and then push the image 20px left
[checkBox setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[checkBox setImageEdgeInsets:UIEdgeInsetsMake(0.0, 20.0, 0.0, 0.0)];
[self.view addSubview:checkBox];
// add checkbox text text
UILabel *checkBoxLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 74,200, 16)];
[checkBoxLabel setFont:[UIFont boldSystemFontOfSize:14]];
[checkBoxLabel setTextColor:[UIColor whiteColor]];
[checkBoxLabel setBackgroundColor:[UIColor clearColor]];
[checkBoxLabel setText:@"Checkbox"];
[self.view addSubview:checkBox];
// release the buttons
[checkBox release];
[checkBoxLabel release];
और भी में इस विधि से रख:
- (void)toggleButton: (id) sender
{
checkboxSelected = !checkboxSelected;
UIButton* check = (UIButton*) sender;
if (checkboxSelected == NO)
[check setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
else
[check setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateNormal];
}
यहाँ Everyones कोड बहुत लंबे, थोड़ा गंदा है, और बहुत आसान किया जा सकता है। मुझे लगता है कि UIControl उपवर्ग है कि आप डाउनलोड करने और बाहर की जाँच और आप एक लगभग देशी चेकबॉक्स यूआई तत्व देता है सकते हैं GitHub पर एक परियोजना है:
उपवर्ग UIButton, नियंत्रक देखने, इसे चुनें और पहचान निरीक्षक में चेकबॉक्स के वर्ग के नाम को बदलने के लिए एक बटन छोड़ देते हैं।
#import "CheckBox.h"
@implementation CheckBox
#define checked_icon @"checked_box_icon.png"
#define empty_icon @"empty_box_icon.png"
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setImage:[UIImage imageNamed:empty_icon] forState:UIControlStateNormal];
[self addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)didTouchButton {
selected = !selected;
if (selected)
[self setImage:[UIImage imageNamed:checked_icon] forState:UIControlStateNormal];
else
[self setImage:[UIImage imageNamed:empty_icon] forState:UIControlStateNormal];
}
@end
मैं एक UITextField के साथ इसे बनाया कुछ भी अजीब ड्राइंग से बचने के लिए, लेकिन मैं NSString के लिए (यूनिकोड चरित्र 'जाँच चिह्न' (U + 2713)) पाठ के रूप में अंदर डाल टिक यूनिकोड इसे पसंद किया: @ "\ u2713"।
इस तरह, मेरी ज फ़ाइल में (UITextField 'UITextFieldDelegate' के लिए प्रोटोकॉल को लागू करने):
UITextField * myCheckBox;
मेरी viewDidLoad या समारोह में यूआई तैयार करने के लिए:
...
myCheckBox = [[UITextField alloc] initWithFrame:aFrame];
myCheckBox.borderStyle = UITextBorderStyleRoundedRect; // System look like
myCheckBox.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myCheckBox.textAlignment = NSTextAlignmentLeft;
myCheckBox.delegate = self;
myCheckBox.text = @" -"; // Initial text of the checkbox... editable!
...
फिर, स्पर्श घटना में reating और 'responseSelected' घटना फोन करने के लिए एक घटना चयनकर्ता जोड़ें:
...
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkboxSelected)];
[myCheckBox addGestureRecognizer:tapGesture];
...
अंत में है कि चयनकर्ता का जवाब
-(void) checkboxSelected
{
if ([self isChecked])
{
// Uncheck the selection
myCheckBox.text = @" -";
}else{
//Check the selection
myCheckBox.text = @"\u2713";
}
}
समारोह 'isChecked' केवल जांच करता है कि पाठ @ "\ u2713" जाँच चिह्न है। कुंजीपटल जब पाठ क्षेत्र का चयन किया जाता UITextField 'textFieldShouldBeginEditing' की घटना का उपयोग करें और घटना चयनकर्ता जोड़ने चयन प्रबंधन करने के लिए दिखा रोकने के लिए:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Question selected form the checkbox
[self checkboxSelected];
// Hide both keyboard and blinking cursor.
return NO;
}
ज फ़ाइल में
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
BOOL isChecked;
UIImageView * checkBoxIV;
}
@end
और .m फ़ाइल
- (void)viewDidLoad
{
[super viewDidLoad];
isChecked = NO;
//change this property according to your need
checkBoxIV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 15, 15)];
checkBoxIV.image =[UIImage imageNamed:@"checkbox_unchecked.png"];
checkBoxIV.userInteractionEnabled = YES;
UITapGestureRecognizer *checkBoxIVTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlecheckBoxIVTapGestureTap:)];
checkBoxIVTapGesture.numberOfTapsRequired = 1;
[checkBoxIV addGestureRecognizer:checkBoxIVTapGesture];
}
- (void)handlecheckBoxIVTapGestureTap:(UITapGestureRecognizer *)recognizer {
if (isChecked) {
isChecked = NO;
checkBoxIV.image =[UIImage imageNamed:@"checkbox_unchecked.png"];
}else{
isChecked = YES;
checkBoxIV.image =[UIImage imageNamed:@"checkbox_checked.png"];
}
}
इस चाल करना होगा ...
मैं एड्रियन के विचार छवियों के बजाय वर्णों का उपयोग करना पसंद है। लेकिन मैं बॉक्स पसंद नहीं है, यह केवल (@ "\ u2713") सही का निशान ही आवश्यकता नहीं है। मैं प्रोग्राम के रूप में एक बॉक्स (एक गोल बॉक्स) आकर्षित और जगह एक UILabel इसके अंदर सही का निशान होता है। कार्यान्वयन के इस तरह यह आसान किसी भी निर्भर संसाधन के बारे में देखभाल के बिना कस्टम दृश्य का उपयोग करने के किसी भी आवेदन में बनाता है। आप यह भी सही का निशान का रंग, गोल बॉक्स और आसानी के साथ पृष्ठभूमि अनुकूलित कर सकते हैं। यहाँ पूरा कोड है:
#import <UIKit/UIKit.h>
@class CheckBoxView;
@protocol CheckBoxViewDelegate
- (void) checkBoxValueChanged:(CheckBoxView *) cview;
@end
@interface CheckBoxView : UIView {
UILabel *checkMark;
bool isOn;
UIColor *color;
NSObject<CheckBoxViewDelegate> *delegate;
}
@property(readonly) bool isOn;
@property(assign) NSObject<CheckBoxViewDelegate> *delegate;
- (void) drawRoundedRect:(CGRect) rect inContext:(CGContextRef) context;
@end
#import "CheckBoxView.h"
#define SIZE 30.0
#define STROKE_WIDTH 2.0
#define ALPHA .6
#define RADIUS 5.0
@implementation CheckBoxView
@synthesize isOn, delegate;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, SIZE, SIZE)])) {
// Initialization code
}
//UIColor *color = [UIColor blackColor];
color = [[UIColor alloc] initWithWhite:.0 alpha:ALPHA];
self.backgroundColor = [UIColor clearColor];
checkMark = [[UILabel alloc] initWithFrame:CGRectMake(STROKE_WIDTH, STROKE_WIDTH, SIZE - 2 * STROKE_WIDTH, SIZE - 2*STROKE_WIDTH)];
checkMark.font = [UIFont systemFontOfSize:25.];
checkMark.text = @"\u2713";
checkMark.backgroundColor = [UIColor clearColor];
checkMark.textAlignment = UITextAlignmentCenter;
//checkMark.textColor = [UIColor redColor];
[self addSubview:checkMark];
[checkMark setHidden:TRUE];
isOn = FALSE;
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGRect _rect = CGRectMake(STROKE_WIDTH, STROKE_WIDTH, SIZE - 2 * STROKE_WIDTH, SIZE - 2*STROKE_WIDTH);
[self drawRoundedRect:_rect inContext:UIGraphicsGetCurrentContext()];
[checkMark setHidden:!isOn];
}
- (void)dealloc {
[checkMark release];
[color release];
[super dealloc];
}
- (void) drawRoundedRect:(CGRect) rect inContext:(CGContextRef) context{
CGContextBeginPath(context);
CGContextSetLineWidth(context, STROKE_WIDTH);
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextMoveToPoint(context, CGRectGetMinX(rect) + RADIUS, CGRectGetMinY(rect));
CGContextAddArc(context, CGRectGetMaxX(rect) - RADIUS, CGRectGetMinY(rect) + RADIUS, RADIUS, 3 * M_PI / 2, 0, 0);
CGContextAddArc(context, CGRectGetMaxX(rect) - RADIUS, CGRectGetMaxY(rect) - RADIUS, RADIUS, 0, M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + RADIUS, CGRectGetMaxY(rect) - RADIUS, RADIUS, M_PI / 2, M_PI, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + RADIUS, CGRectGetMinY(rect) + RADIUS, RADIUS, M_PI, 3 * M_PI / 2, 0);
CGContextClosePath(context);
CGContextStrokePath(context);
}
#pragma mark Touch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint loc = [touch locationInView:self];
if(CGRectContainsPoint(self.bounds, loc)){
isOn = !isOn;
//[self setNeedsDisplay];
[checkMark setHidden:!isOn];
if([delegate respondsToSelector:@selector(checkBoxValueChanged:)]){
[delegate checkBoxValueChanged:self];
}
}
}
मैं हाल ही में एक बना दिया। GitHub से प्राप्त करने के लिए नि: शुल्क। अगर देखें इस में मदद मिलेगी। प्रभाव की तरह है
उपयोगकर्ता अरुणा लकमल; FYI करें, जब आप के रूप में आप का वर्णन initWithFrame नहीं बुलाया जाता है आईबी लिए इस कोड को जोड़ने के लिए, initWithCoder है। initWithCoder को लागू करने और के रूप में आप का वर्णन यह काम करेंगे।














