1

I am trying to make a menu using checkboxes, which i have created programmatically. Heres how i made my checkBox:

UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(60,60,300, 44)]; 
[checkBox setImage:[UIImage imageNamed:@"unmarked.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
[checkBox setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[self.view addSubview:checkBox];

And the Target :

- (void)toggleButton: (id) sender
{
    checkboxSelected = !checkboxSelected;
    UIButton* check = (UIButton*) sender;
    if (checkboxSelected == NO)
        [check setImage:[UIImage imageNamed:@"unmarked.png"] forState:UIControlStateNormal];
    else
        [check setImage:[UIImage imageNamed:@"marked.png"] forState:UIControlStateNormal];

}

Now I am making a simple custom UIButton, I want to check if the checkbox is checked or not using this custom UIButton and NSLog the same i.e If the checkBox is checked then on button click it should NSLog that the checkBox is checked..!

Can anyone help me with this..?

Any help will be truly appreciated.

Thanks for your time

Shailesh

Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49
Shailesh
  • 3,072
  • 3
  • 24
  • 33

1 Answers1

0

You should consider to use UISwitch, because checkboxes are not that nice on apple mobile devices. You can refer to a tutorial, e.g. here: http://www.xprogress.com/post-30-uiswitch-tutorial-example-how-to-use-the-switch-in-xcode-for-iphone-sample-included/

or here for doing it programatically: Creating a UISwitch Programmatically

Nevertheless, if you insist to use checkboxes look here: How to create a simple checkbox in iOS?

Community
  • 1
  • 1
Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49
  • Hey Thanks for fast response.. i went through the links u provided. But i have already created checkboxes which are working fine. I just wanted to verify that the checkbox is checked or unchecked on a simple custom UIButton click. – Shailesh Apr 25 '12 at 13:50
  • I am not sure what you need help with. So it is more about logging? If so, add `NSLog(@"Is checked: %@", checkboxSelected ? @"YES" : @"NO");` to `- (void)toggleButton: (id) sender`. This will write you down the state of the button. Is this what you need? If no please try to explain little bit more ;) – Ondrej Peterka Apr 25 '12 at 14:04
  • @ Ondra Peterka: I can use NSLOG in toggleButton function to output which checkBox has been checked. But Iam creating a menu which has 4 checkboxes and one simple UIButton "Submit". Suppose user checks the 3 out of 4 checkboxes and hits submit button then it should NSLOG that which 3 checkBoxes had been checked. – Shailesh Apr 25 '12 at 14:45
  • And Iam having an idea of using BOOL variable to do so.But not sure if it will work or what..! :-) – Shailesh Apr 25 '12 at 14:45
  • I see, so you can use tags. Look for example here http://stackoverflow.com/questions/7282809/how-to-get-button-tag-value-in-iphone . You can assign each button some tag (some number) like this: `checkbutton1.tag =1;`, `checkbutton2.tag = 2;` Than in some `submitButtonClicked` method (which is run when submit button is clicked) check for that value in tag to distinguish buttons (by reading the `button.tag` value). Is this sufficient? – Ondrej Peterka Apr 25 '12 at 15:40
  • @Shailesh_ios Were the tags working for you? Similar discussion went also here btw. https://discussions.apple.com/thread/1718213?start=0&tstart=0 – Ondrej Peterka Apr 26 '12 at 05:17
  • @ Ondra Peterka: Hey it Worked.. Using Tags .. Its working Very well.. actually it was giving trouble with UIButton so just used CCSprites with cocos2D. Thankyou VeryMuch Ondra..! – Shailesh Apr 26 '12 at 11:19
  • @Shailesh_ios Glad to hear that ;), if you are satisfied with the answer, you might want to consider to accept it. – Ondrej Peterka Apr 26 '12 at 11:22
  • I did..:-)) Iam New to Stack Overflow.. This was my First Question..:-D So.. Pardon if i take time to getting used to. Thanks Again..! :-) – Shailesh Apr 26 '12 at 11:25
  • @Shailesh_ios No worries, I know you are new. You migth want to read http://stackoverflow.com/faq . Truth to tell I did not do it at the very begginig and I run into few unnecessary complications. Anyway, good luck with your projects ;) – Ondrej Peterka Apr 26 '12 at 11:28