-3

When I click on the UIButton nothing happened

checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Code cracker
  • 3,105
  • 6
  • 37
  • 67
raghunadh
  • 1
  • 2

4 Answers4

2

You have to create button like below.

UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];

You have to create the button first using button property and then give its frame.

If still its not working then self.background addSubview should be self.view addSubview.

What is self.background?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

Check userInteractionEnabled property of your background property.

But i see why you have no feedback on button if rest is fine - you use UIControlStateSelected for setting image when button has touch, but you must use UIControlStateHighlighted

Nikita Ivaniushchenko
  • 1,425
  • 11
  • 11
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/72849/discussion-on-answer-by-nikita-ivaniushchenko-uibutton-programatically-is-not-wo). – Taryn Mar 12 '15 at 14:56
  • Exactly, but i still see that initial issue is not solved - so still don't know what was the cause. And everybody was making assumptions about its reasons. – Nikita Ivaniushchenko Mar 12 '15 at 14:58
0

Your code is perfectly fine. You need to check the following .

  • check whether self.background is placed properly in your view heirarchy, I tried your code with self.view instead of self.background like below and it works perfectly.
self.checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 50, 50)];
[self.checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox1.png"] forState:UIControlStateNormal];
[self.checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox2.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.checkbox];
  • From the error message that you have given "

    Warning: Attempt to present <enter_the_den: 0x7fbf1bd4c4e0> on <ViewController: 0x7fbf1bc771c0> whose view is not in the window hierarchy!

I suspect the issue is in your code inside the method autologin, just comment all the code in your method and just try a log , it should work.

refer this : whose view is not in the window hierarchy

Community
  • 1
  • 1
karthik
  • 1,271
  • 1
  • 14
  • 27
  • Their code is not perfectly fine as they need to be using `[UIButton buttonWIthType:]` instead of `[[UIButton alloc] initWithFrame:]` as this will not do anything. So your answer is incorrect – Popeye Mar 12 '15 at 12:59
  • @Popeye, Can u pls justify your comment , I tested this code and it is working fine for me. – karthik Mar 12 '15 at 13:10
  • Read the apple documentation around `UIButton` and creating buttons and you will understand why you should be using `buttonWIthType` and not `initWithFrame:`. – Popeye Mar 12 '15 at 13:12
  • @NikitaIvaniushchenko just because your answer is incorrect and you can't accept it is no reason to start insulting other users, that isn't welcome on here. Flagged for moderator attention – Popeye Mar 12 '15 at 14:03
  • @Popeye, i really have some doubts in your ObjC skills, how can i report that to moderation team? – Nikita Ivaniushchenko Mar 12 '15 at 14:05
  • @NikitaIvaniushchenko So you going to doubt `FahimParkar`s objC skills as well who has said the same thing or maybe you want to take everyone on this question on http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically and tell them their skills aren't good either OR maybe this tutorial http://www.tutorialspoint.com/ios/ios_ui_elements_buttons.htm Like I say insults aren't welcome here and if that's how you're going to be then I might as well just stop because clearly you don't want to accept what everyone else says. – Popeye Mar 12 '15 at 14:09
  • 1
    There is no mentioning in that question that only that method must be used for button creation. Also, they use predefined type of button - UIButtonTypeRoundedRect, so initWithFrame does not fit. – Nikita Ivaniushchenko Mar 12 '15 at 14:12
  • There's just no talking sense to you, go use google and answer you're own question of why you're answer is incorrect. – Popeye Mar 12 '15 at 14:16
  • @Popeye,I find there is no issue with the code I pasted above , this is comparison http://stackoverflow.com/questions/6245882/initwithframeframe-vs-uibutton-buttonwithtype, and also I request you to justify your comment . I answered this question only after testing in my machine , if you think it is incorrect please clarify it instead of telling read it in apple doc. – karthik Mar 12 '15 at 14:29
  • Normally I would but thanks to `Nikita Ivaniushchenko` I honestly can't be bothered so you can thank him for that. And saying read the documentation is a valid comment. – Popeye Mar 12 '15 at 14:34
  • karthik, your answer is correct and that answer about comparison of there two methods is written by really high-rated user, so if that's argument for @Popeye, he must follow him. Otherwise, he must tell mr. MattDiPasquale that he is wrong. – Nikita Ivaniushchenko Mar 12 '15 at 14:47
  • @NikitaIvaniushchenko So you're answer is incorrect is that an admission? Also read the comments of that answer and note that I also did tell him he was wrong. – Popeye Mar 12 '15 at 14:48
  • Read this once more [http://stackoverflow.com/questions/6245882/initwithframeframe-vs-uibutton-buttonwithtype] – Nikita Ivaniushchenko Mar 12 '15 at 14:49
  • @NikitaIvaniushchenko Like I just said read the comments to that answer and you'll find I did raise it with that user back in 2013 and my comment still remains for that answer too and notice the other comments on it that raise the exact same things I have raised with you. So you going to go tell them since I've told the high rated user you have mentioned? – Popeye Mar 12 '15 at 14:53
  • @Popeye, I asked you just to know wat is wrong in that , so that I can learn from it , one issue you pointed out is we cannot set buttontype , but this is not the concern in the question.If you can clarify it will be helpful to all. – karthik Mar 12 '15 at 15:03
  • Basically `UIButton` doesn't override `initWithFrame:` so it explicitly sets the `buttonType` apparently to `0` but this will make your code unsafe. The reason being is that what happens if Apple decide one day that they are going override that method and set the default value to `3` your app all of a sudden doesn't work as expect. So this is the reason why you should use `buttonWithType:` because the `UIButtonType Enums` have been documented so will not change whereas `initWithFrame:` is undocumented for `UIButton` so it can be changed at will by Apple. Also this method didn't work for me. – Popeye Mar 12 '15 at 15:09
0

If you read the Apple Documentation for UIButton and look under the heading marked as Creating Button you'll find that the way to create a UIButton is to use buttonWithType: and not initWithFrame:.

Return Value

A newly created button.

Discussion

This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.

When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.

The only time when you should use alloc/init with a UIButton is when you have subclassed UIButton however your init method should then implement the buttonWithType: and pass in UIButtonTypeCustom.

So change:

checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];

To

// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:@selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Popeye
  • 11,839
  • 9
  • 58
  • 91