How does this code work to create buttons? - iphone

I am wondering how exactly the following piece of code works (yes, it indeed works as intended, and it gives different values of tag, depending on which button was clicked.)
The point is: Aren't we reusing button 7 times, where are the other 6 buttons left? After executing this code do we really have memory reserved for 7 UIButtons?
Or as a more general question: Is this good programming style? The point is that I need to have a different action depending on which button was clicked, and this approach (with my limited objc skills) looked like the most straightforward.
Thanks in advance,
A beginning iOS developer.
UIButton *button;
for(int k=0;k<7;k++)
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(80, 20+30*k, 30, 30);
button.backgroundColor=[UIColor clearColor];
button.tag=k;
[subview addSubview:button];
}
Where the function aMethod: is defined as:
-(IBAction)aMethod:(id)sender
{
UIButton *clickedButton=(UIButton *) sender;
NSLog(#"Tag is: %d",clickedButton.tag);
}

No, you are not reusing the UIButton seven times: each iteration of the loop creates a new instance of UIButton in a call to the class method buttonWithType:, and assigns it to the variable of the same name:
[UIButton buttonWithType:UIButtonTypeCustom];
Your code would be better off if you declared that variable inside the loop:
for(int k=0;k<7;k++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// ...the rest of the loop
}
This is a very good programming style for situations when your buttons perform very similar actions. For example, calculator buttons differ only in the number that they insert. When the buttons perform actions that differ a lot (e.g. insertion vs. deletion) you are better off creating them separately, not in a loop, and servicing their clicks using separate methods.

In this code you just create 7 buttons with different positions and tags. It is much more better, then duplicate creation code for 7 times. The one thing, that I can offer you - to create some enum for button tags to prevent code like this:
switch([button tag])
{
case 1:
// do something
break;
case 2:
// do something else
break;
case 3:
// exit
break;
.....
default:
assert(NO && #"unhandled button tag");
}
the code with enum values much more easy to read
switch([button tag])
{
case kButtonForDoSomething:
// do something
break;
case kButtonForDoSomethingElse:
// do something else
break;
case kButtonExit:
// exit
break;
.....
default:
assert(NO && #"unhandled button tag");
}

This code does suffice, you're creating 7 different buttons and adding them as subviews. Once you -addSubview:, the object is added to an array, Accessed via the .subviews property of UIView.
Although you should add UIButton *button in your for loop. So your code should look like below..
for(int k=0;k<7;k++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
....
}
If you needed to do something out of the for loop with the button then declaring it outside the loop makes sense, but this is not your case. Hope this helps.

Related

How to hide buttons which I had allocated in for loop?

I have created 3 UIbuttons on the top of the screen. Now after clicking every button I got the 5 buttons for each at below with different background images. Below is the code for my first button(located at top) by which I got 5 images in my view controller.
-(IBAction) btnforimages1click:(id)sender {
for (int a=0; a<5 ; a++) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
buttonsforbundle1 = [[UIButton alloc]initWithFrame:CGRectMake(100*a,300 ,100 ,90 )];
[buttonsforbundle1 addTarget:self action:#selector(btn4images1:) forControlEvents:UIControlEventTouchUpInside];
buttonsforbundle1.tag = a;
[buttonsforbundle1 setBackgroundImage:[UIImage imageNamed:[bundle1 objectAtIndex:a]] forState:UIControlStateNormal];
[self.view addSubview:buttonsforbundle1];
}
}
I do the same for other two buttons also. Now here I want is, when I click any of my top 3 buttons
I just want to display the related buttons (created in for loop)
I want to hide the other buttons related to other Top most buttons.
Please suggest me how to solve this.
You have at least three options:
1) Access UI element by tag:
If you can manage to issue unique tag number in your btnforimages1click, btnforimages2click, btnforimages3click for your other 5 added UIButtons, then you can access them:
UIButton *button = (UIButton *)[self.view viewWithTag:99];
[button removeFromSuperview];
2) Keep reference of the created buttons:
// in view.h
NSMutableArray *my_created_buttons;
...
// in view init
[[my_created_buttons alloc] init];
...
-(IBAction) btnforimages1click:(id)sender {
for (UIButton *b in my_created_buttons {
[b removeFromSuperview];
}
if([my_created_buttons count]) {
[my_created_buttons removeAllObjects];
}
... let buttonsforbundle1 local variable since we store current 5 button set in array
UIButton *buttonsforbundle1;
... in your for loop add this after creating and adding to view
[my_created_buttons addObject:buttonsforbundle1];
}
You can further refactor these code eg. by making mutablearray management to other method.
3) Keep it in Interface Builder
I think it is the easiest option, create 5 UIButtons in Interface Builder and have 5 IBOutlet for them. Then in your btnforimages1click you can eg:
-(IBAction) btnforimages1click:(id)sender {
self.IBbutton1 setBackgroundImage[UIImage imageNamed:[bundle1 objectAtIndex:0]] forState:UIControlStateNormal];
self.IBbutton2 setBackgroundImage[UIImage imageNamed:[bundle1 objectAtIndex:1]] forState:UIControlStateNormal];
self.IBbutton3 setBackgroundImage[UIImage imageNamed:[bundle1 objectAtIndex:2]] forState:UIControlStateNormal];
self.IBbutton4 setBackgroundImage[UIImage imageNamed:[bundle1 objectAtIndex:3]] forState:UIControlStateNormal];
self.IBbutton5 setBackgroundImage[UIImage imageNamed:[bundle1 objectAtIndex:4]] forState:UIControlStateNormal];
}
So you don't hide them, just change the background image. Moreover you don't have to deal with different touchUpInside event, use only one method, and in that one click method, you can distinguish between the different clicked UIButton by checking their tag (plus you have to use a variable to check which of the 3 top UIButton was clicked).

toggle UIButton text programmatically with a conditional statement

Here's my code (it's wrapped in an IBAction that is called when the button is pressed):
if (myButton.currentTitle == #"test") {
[myButton setTitle:#"test2" forState:UIControlStateNormal];
}
if (myButton.currentTitle == #"test2") {
[myButton setTitle:#"test" forState:UIControlStateNormal];
}
I want the UIButton text to toggle when pressed (if text = "test" then change to "test2" and when pressed if text = "test2" change to "test").
I do have an IBOutlet connected for myButton and the the IBAction connected to myButton--so I am pretty sure it isn't a problem with my connections.
For some reason this isn't working, I'm sure I am missing something very simple.
use isEqualToString: instead of ==
This is because you lack a control statement that skips the second if when the first one succeeds. When you come into the block with "test", you switch it to "test2", and then the second condition succeeds immediately, and you turn "test2" back into "#test".
You can an an else to fix this, but you can skip the if altogether by using an NSArray that maps the current state to the new state.
// This should be made static, and initialized only once
NSDictionary *nextTitle = [NSDictionary dictionaryWithObjectsAndKeys:
#"test", #"test2", #"test2", #"test", nil];
// This line does the toggling
[myButton setTitle:[nextTitle valueForKey:myButton.currentTitle] forState:UIControlStateNormal];
if ([myButton.currentTitle isEqualToString:#"test"]) {
[myButton setTitle:#"test2" forState:UIControlStateNormal];
}
if ([myButton.currentTitle isEqualToString:#"test2"]) {
[myButton setTitle:#"test" forState:UIControlStateNormal];
}
Hope, this will help you...
Comparing user-visible strings is generally considered bad practice (and becomes tedious when you need to do i18n), especially with string literals since it's vulnerable to typos.
If you're just going to toggle between two states, the easiest thing to do is to use the UIControl.selected property (corresponding to UIControlStateSelected):
// In init
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitle:#"test2" forState:UIControlStateSelected];
[myButton setTitle:#"test2" forState:UIControlStateSelected|UIControlStateHighlighted];
// Toggle
myButton.selected = !myButton.selected;
It also makes the code a lot cleaner when you when you decide to toggle the button image/background/text colours too.
Note the slight gotcha: If you don't set the title for UIControlStateSelected|UIControlStateHighlighted it will use the title for UIControlStateNormal when the button is both selected and highlighted (touched).
When comparing strings to each other try using if([str1 compare:str2] == NSOrderedSame)

Best radio-button implementation for IOS

I would like to ask if there are examples out there on how to implement radio-button options on an iPhone app.
I find the Picker View quite big for a simple selection feature.
I'm not sure if Apple excluded radio buttons on purpose, and whether if it is better to simply use a Picker View from a usability / user experience point-of-view.
I have some thoughts on how the best radio button implementation should look like. It can be based on UIButton class and use it's 'selected' state to indicate one from the group. The UIButton has native customisation options in IB, so it is convenient to design XIBs.
Also there should be an easy way to group buttons using IB outlet connections:
I have implemented my ideas in this RadioButton class. Works like a charm:
Download the sample project.
Try UISegmentedControl. It behaves similarly to radio buttons -- presents an array of choices and lets the user pick 1.
Just want to sum up, there might be 4 ways.
If you don't have much space, add a click event for text or button, then show UIPickerView:
or open a new table view control with a check mark:
If there is more space, add a table view directly to your main view:
The final solution is using UISegmentedControl:
Hope this helps.
Try DLRadioButton, works for both Swift and ObjC. You can also use images to indicate selection status or customize your own style.
Check it out at GitHub.
**Update: added the option for putting selection indicator on the right side.
**Update: added square button, IBDesignable, improved performance.
**Update: added multiple selection support.
I know its very late to answer this but hope this may help anyone.
you can create button like radio button using IBOutletCollection. create one IBOutletCollection property in our .h file.
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *ButtonArray;
connect all button with this IBOutletCollection and make one IBAction method for all three button.
- (IBAction)btnTapped:(id)sender {
for ( int i=0; i < [self.ButtonArray count]; i++) {
[[self.ButtonArray objectAtIndex:i] setImage:[UIImage
imageNamed:#"radio-off.png"]
forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:#"radio-on.png"]
forState:UIControlStateNormal];
}
For options screens, especially where there are multiple radio groups, I like to use a grouped table view. Each group is a radio group and each cell a choice within the group. It is trivial to use the accessory view of a cell for a check mark indicating which option you want.
If only UIPickerView could be made just a little smaller or their gradients were a bit better suited to tiling two to a page...
I've written a controller for handling the logic behind an array of radio buttons. It's open source and on GitHub, check it out!
https://github.com/goosoftware/GSRadioButtonSetController
The following simple way to create radio button in your iOS app follow two steps.
Step1- Put this code in your in viewDidLoad or any other desired method
[_mrRadio setSelected:YES];
[_mrRadio setTag:1];
[_msRadio setTag:1];
[_mrRadio setBackgroundImage:[UIImage imageNamed:#"radiodselect_white.png"] forState:UIControlStateNormal];
[_mrRadio setBackgroundImage:[UIImage imageNamed:#"radioselect_white.png"] forState:UIControlStateSelected];
[_mrRadio addTarget:self action:#selector(radioButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[_msRadio setBackgroundImage:[UIImage imageNamed:#"radiodselect_white.png"] forState:UIControlStateNormal];
[_msRadio setBackgroundImage:[UIImage imageNamed:#"radioselect_white.png"] forState:UIControlStateSelected];
[_msRadio addTarget:self action:#selector(radioButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
Step2- Put following IBAction method in your class
-(void)radioButtonSelected:(id)sender
{
switch ([sender tag ]) {
case 1:
if ([_mrRadio isSelected]==YES) {
// [_mrRadio setSelected:NO];
// [_msRadio setSelected:YES];
genderType = #"1";
}
else
{
[_mrRadio setSelected:YES];
[_msRadio setSelected:NO];
genderType = #"1";
}
break;
case 2:
if ([_msRadio isSelected]==YES) {
// [_msRadio setSelected:NO];
// [_mrRadio setSelected:YES];
genderType = #"2";
}
else
{
[_msRadio setSelected:YES];
[_mrRadio setSelected:NO];
genderType = #"2";
}
break;
default:
break;
}
}

IBAction used with Dynamically added UIbuttons

Good Evening all!
I have some UIButtons added dynamically into my view and of course I have an IBAction which handles button events.
The problem is: How can I detect which button is pressed if the only thing I know is the (id)sender and the array of buttons?
Buttons are never the same, every button has a different behavior. When I want to use static buttons and connect them through the IB I use something like this :
-(IBAction)doSomething:(id)sender
{
if(sender == button1)
dosomething;
if(sender == button2)
dosomething else;
if(sender == button3)
dosomething3;
}
In my case this does not work because there is no button1, button2, button3 but a MutableArray of buttons which have the same name as they were allocated with it. Button!
I tried using the way above but with no success and i tried also getting the tag of a button but I have nothing to compare it to!
I would really appreciate your help.
sincerely
L_Sonic
PS Dynamicaly means that i am creating the buttons in random time during run time like this
-(void)configActionSheetView
{
buttonView = [[UIView alloc]initWithFrame:CGRectMake(0.0,460, 60, 480)];
[buttonView setBackgroundColor:[UIColor blackColor]];
[buttonView setAlpha:0.6];
for (int i = 0 ;i<[buffButtons count];i++)
{
UIButton *customButton = [buffButtons objectAtIndex:i];
customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//UILabel *customLabel = [[UILabel alloc]init];
//[customButton setTag:(i)+11];
[customButton addTarget:self action:#selector(activateBuffEffect:) forControlEvents:UIControlEventTouchUpInside];
[customButton setAlpha:1.0];
customButton.frame = CGRectMake(8.0, 5+(50*i), 44.0, 44.0);
[customButton setTitle:nil forState:UIControlStateNormal];
buttonView.frame = CGRectMake(0, 460, 60, 50+(44*(i+1)));
[buttonView addSubview:customButton];
}
}
this is inside a functions and gets called during run time. the buffButtons is a mutableArray with buttons that gets populated during runtime.
i need a solution like this i cannot get a different eventhandling method for everybutton.
When you was "added dynamically" I assume you mean that they are created from some piece of code. Since all buttons to different things and you know what a certain button should do, why don't you add different actions to different buttons?
UIButton *myCreatedButton = [[UIButton alloc] init];
[myCreatedButton addTarget:self
action:#selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *myOtherCreatedButton = [[UIButton alloc] init];
[myOtherCreatedButton addTarget:self
action:#selector(doSomethingElse:)
forControlEvents:UIControlEventTouchUpInside];
In the above code the target (set to self) is the class where the method you want to run is found, the action is the method that you want to run and the controlEvent is what should cause the method to run.
If you did it like this you would split the code in different methods like these (you do not need to specify them in the header):
-(void)doSomething:(id)sender {
// do somthing here ...
}
-(void)doSomethingElse:(id)sender {
// do somthing else here ...
}
This way you don't need to know what button was pressed since the correct code would get called anyway. Besides it makes it cleaner if you need to change the code for some of the buttons.
Found it!
-(IBAction)buttonTapped:(id)sender {
UIButton *btn = (UIButton *)sender;
NSLog(#"tapped: %#", btn.titleLabel.text);
[self anotherIBAction:sender];
}
now i can get the tag from the btn :D
thnk you!
Why not add a tag the button and then get the tag number from (id)sender in the selector function?

problem with selector in iphone?

i am having a application where i am generating the uibuttons dynamically want to use same #selector...Now as sooon as event is generated i wanna check for values and pass it to thtroughthat selector how can i implement this code?
can anyone tell me a tutorial sort where buttons are dynamically generated and check for particular button click is depicted?
Plz help...
Not sure what do you call "dynamically"... Objective-c objects are always created dynamically. Perhaps you mean situation where you want to create series of very similar buttons with same code? Yes, it is quite a common task. For example in calculator like app, we need ten buttons with digits, why not create them with single block of code ? So:
- (void)makeButtons{
UIButton * aButton;
for(int i = 0; i < 10; i++){
aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(0, i*45, 60, 40);
[aButton addTarget:self action:#selector(digitClick:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
aButton.tag = i;
[self.view addSubview:aButton];
}
}
- (void)digitClick:(id)sender{
UIButton * aButton =(UIButton *)sender;
NSLog(#"cliced button with title %d",aButton.tag);
}
we use a tag property to find index of clicked button, it is often used way, but there are some other ways. For example store created buttons in array and then check if sender is equal to one of array elements:
...
if ([allButtons objectAtIndex:i] == sender)
...
If you want to pass some data, for example string from each button just create array with these objects and then access it using tag as index.
Try:
[self.button1 addTarget:self action:#selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
[self.button2 addTarget:self action:#selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
- (IBAction)buttonTouchDown:(id)sender
{
if (sender == self.button1) NSLog(#"Button-1");
if (sender == self.button2) NSLog(#"Button-2");
}