How to save a reference to a control added programmatically? - iphone

I added 3 buttons programmatically to my view, then I added the buttons to an array so that I can access them at a later time:
for (i = 0; i < 3; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[view addSubview:button];
[_buttons addObject:button];
}
If I reference the button in my array and change the image of the button, it does not change the button on screen.
UIButton* button = [_buttons objectAtIndex: 0];
[button setImage:thumb forState:UIControlStateNormal];
I've found a way to change the image of the button by looping through all the subviews in my view, but is there a better way?
for (UIView* subView in ((UIView*)[self.view.subviews objectAtIndex:0]).subviews){
if ([subView isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)subView;
if (button.tag == self.selected){
[button setImage:thumb forState:UIControlStateNormal];
}
}
}

A common reason for this is that your array has not been initialized. When this happens, Objective C does not complain or throw excetions: instead, it behaves as if the calls to add elements never happened. It also returns nil when you try getting items back.
Add this line to your viewDidLoad method:
_buttons = [NSMutableArray array];
This should solve the problem.

Possible this could help on a click event:
-(void)clickEvent:(id)sender
{
[sender setImage:thumb forState:UIControlStateNormal];
}
If this is how you are picking up the event.

When you do addSubview, view retains subview you are adding. Also addObject retains it. Hence both are different ojbects. Changing properties of object in array will not affect object retained by view.
You can avoid loop by using tags. While adding buttons on view set unique tags to them. And when you want to access them, get it using tag directly.
//set tags for buttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:999];
[view addSubview:button];
//access using tag
UIButton *button = (UIButton*)[view viewWithTag:999];
[button setImage:thumb forState:UIControlStateNormal];

Add a tag to the button after creating it. And later use that tag to get that button.
for (i = 0; i < 3; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i+1;
[view addSubview:button];
[_buttons addObject:button];
}
Then you can access it like:
UIButton *button1 = (UIButton *)[self.view viewWithTag:1];
UIButton *button2 = (UIButton *)[self.view viewWithTag:2];
UIButton *button3 = (UIButton *)[self.view viewWithTag:3];

While adding UIControl programatically to use reference later add tag to it which should be different like:
yourBtn.tag = 111;
Now get reference of UIButton like this:
UIButton *button = (UIButton*)[yourViewWhereYouAdded viewWithTag:111];

Related

Programmatically added button getting crash in storyboard

I am adding a button in UIScrollView in StoryBoard
Below is the code i am using.
-(void)addScrollView
{
for(int i=0;i<[array count];i++)
{
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, SUBSCROLLVIEW_WIDTH, SUBSCROLLVIEW_HEIGHT)];
UITextView *txtVwDetail = [[UITextView alloc] initWithFrame:CGRectMake(342, 0, TEXTVIEW_WIDTH, TEXTVIEW_HEIGHT)];
txtVwDetail.text = SAMPLE_STRING;
[self addSubScrollView:subScrollView];
[self.view addSubview:subScrollView];
[self.view addSubview:txtVwDetail];
}
}
-(void)addSubScrollView:(UIScrollView *)aSubScrollView
{
for(int i=0;i<[array count];i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(intBtnX, (aSubScrollView.frame.size.height - 80)/2, 50, 80);
[aButton setImage:[UIImage imageNamed:[self.items objectAtIndex:i]] forState:UIControlStateNormal];
**[aButton addTarget:self action:#selector(btnSubImageClicked) forControlEvents:UIControlEventTouchUpInside];**
aSubScrollView.contentSize = CGSizeMake(intScrollViewWidth, aSubScrollView.frame.size.height);
[aSubScrollView addSubview:aButton];
}
}
In addSubScrollView method I have added Button and its click event which is getting crashed.
-(void)btnSubImageClicked
{
NSLog(#"btnSubImageClicked");
}
I am having scenario as
MyMainViewController is the sourceViewController for my created customSegue which is the UIStoryboardSegue class
MyMainViewController having a UIView as PlaceHolderView in which I am adding MydestinationViewContoller's View which is this Scrollview
-(void)perform
{
BrowseScreenVC *srcObj = (BrowseScreenVC *)[self sourceViewController];
UIViewController *dstObj = (UIViewController *)[self destinationViewController];
for(UIView *vw in srcObj.viewPlaceHolder.subviews)
{
[vw removeFromSuperview];
}
NSLog(#"dest : %#",dstObj.view);
NSLog(#"destobj :%#",dstObj);
srcObj.currentViewController = dstObj;
[srcObj.viewPlaceHolder addSubview:[dstObj.view retain]];
}
UPDATE
With answer I also have to change the line
srcObj.currentViewController = dstObj;
to
srcObj.addChildViewController = dstObj;
to make it working
you have to add target as follow:
[aButton addTarget:self action:#selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
#selector(), within these braces you have to provide the method that you want to perform. the ":" represents that the method has some argument. In this case the argument would be the button itself that is calling the method.
You have to implement your method then its signature would look like this
- (void)btnSubImageClicked:(id)sender{
// sender would be the button that is calling the method. you can use this object according to your requirements if you want.
// your code
}
if you want to call this method from somewhere else as well you can call it by passing sender argument nil. e.g [self btnSubImageClicked:nil];
Your action needs to accept an (id) sender argument, even if you are not using it:
-(void)btnSubImageClicked:(id) sender
{
NSLog(#"btnSubImageClicked");
}
In the addSubScrollView:
[aButton addTarget:self action:#selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
Button target should like
-(void) btnSubImageClicked:(id)sender{}
try this.
Updated:-
Please correct your code,change this line
[aButton addTarget:self action:#selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
Now working i checked.
Instead of
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
write this,
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and for better practice always write this,
-(void) btnSubImageClicked:(id)sender{}

Creating UIButtons dynamically - Logic Error

I have a NSMutableArray, and i have saving some string values in it. The size of the NSMutableArray could vary from 2-5 (Meaning it might have 2 -5 string values stored in it).
Depending on the count of the NSMutableArray i need to initialize UIBUttons and then set the value of the String stored init to the buttons title.
int numberOfButtonsToBeInitialize= [mutableArr count];// we are finding the number of buttons to be initialize and we assign it to a variable.
Now i need to create buttons (what ever the number returned by numberOfButtonsToBeInitialize)
How can i do this ?
for(int i=0;i<numberOfButtonsToBeInitialize;i++){
//init the button
UIButton *bout = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the title
[bout setTitle:[mutableArr objectAtIndex:i] forState:UIControlStateNormal];
[bout addTarget:self action:#selector(event:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:bout ];
//then you can set the position of your button
[bout setFrame:CGRectMake(70,3, 40,40)];}
Try this:
NSMutableArray *myButtonsArray = [[NSMutableArray alloc] initWithCapacity:0];
UIButton *tmpButton;
for (NSString *bTitle in mutableArr)
{
tmpButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[tmpButton setTitle:bTitle forState:UIControlStateNormal];
// Any additional setup goes here
[myButtonsArray addObject:tmpButton];
}
Now you have an array with all the buttons. You can just iterate this array and add any button as a subview in your main view.
You need a for loop. For example:
float buttonWidth=50;
float margin=5;
for (int index=0; index<numberOfButtonsToBeInitialize;index++)
{
UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* buttonTitle=[mutablearr objectAtIndex:index];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setFrame:CGRectMake(0+(buttonWidth+margin)*index, 0, buttonWidth, 30)];
[button setTag:100+index];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
In this case we layout buttons in a row(horizontally). Adjust to your own preferences

How to change the image of button with the help of tag?

How i can change the image on button when i have only tag of that button. for instance I have 20 buttons and tag is set 0 to 19. now in a response of some action i have the tag 5 of that button and now i want to change the image of that button. how could it possible? Please help
I think you can use it:
for (UIButton *button in self.view.subviews) {
if (button.tag == 5) {
[button setBackgroundImage:[UIImage imageNamed:#"new-imagepng"] forState:UIControlStateNormal];
}
}
But I didn't check it.
The following code gives you the button you are after (superView refers to whatever view is holding your button):
UIButton *button5 = [superView viewWithTag:5];
Then you can amend the properties of the button as you see fit.
UIButton *button = (UIButton *)[myView viewWithTag:myTag];
[button setImage:... forState:...];
The myView variable is superview of the 20buttons. You can get UIButton object from the tag and can set image for your state.
Make the actionof all buttons the same, say #(selector)buttonPressed:. Then in your handler do this:
-(void)buttonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
int tag = button.tag;
// respond as you wish using tag... for example:
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"img%02d.png", tag]];
[button setImage:image forState:UIControlStateNormal];
}
One more warning: better use integers 1 through 20 rather than 0 through 19. This way you avoid mistakes that might arise out of the fact that the default tagof every UIView is set to 0.

Add a multiple buttons to a view programmatically, call the same method, determine which button it was

I want to programmatically add multiple UIButtons to a view - the number of buttons is unknown at compile time.
I can make one or more UIButton's like so (in a loop, but shorted for simplicity):
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button x" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);
[view addSubview:button];
Copied/Edited from this link:
How do I create a basic UIButton programmatically?
But how do I determine in buttonClicked: which button was clicked? I'd like to pass tag data if possible to identify the button.
You could either keep a reference to the actual button object somewhere that mattered (like an array) or set the button's tag to something useful (like an offset in some other data array). For example (using the tag, since this is generally must useful):
for( int i = 0; i < 5; i++ ) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}
// then ...
- (void)buttonClicked:(UIButton*)button
{
NSLog(#"Button %ld clicked.", (long int)[button tag]);
}
You can assign a tag to the button.
button.tag = i;
Then in -buttonClicked:, check the tag of the sender:
-(void)buttonClicked:(UIButton*)sender {
int tag = sender.tag;
...
}
I think this would be the best answer:-
http://conecode.com/news/2012/05/ios-how-to-create-a-grid-of-uibuttons/
For that give different tag to each button & use code like this:
[btn1 addTarget:self action:#selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:#selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside];
& in method
-(void)pressbtn:(id)sender {
UIButton *button=sender;
if (button.tag==1)
{
NSLog(#"Press button 1");
}
if (button.tag==2)
{
NSLog(#"Press button 2");
}
and so on to check which button is called
If you want to add buttons at run time then there will be 10 20 50 or more than that. Then you should to use ui scroll view in this condition.
When the buttons will be generate then your scroll view size should be increased accordingly.
And you can write the code like this
scrollview = [[UIScrollView alloc] init];
scrollview.contentSize = CGSizeMake(INVOICE_ADDITEM_SCROLLVIEW_CONTENT_WIDTH, INVOICE_ADDITEM_SCROLLVIEW_CONTENT_HEIGHT);
scrollview.frame = CGRectMake(0,50, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT);
// scrollview.backgroundColor = [UIColor whiteColor];
scrollview.scrollsToTop = NO;
scrollview.delegate = self;
[self.view addSubview:scrollview];
for (int pos = 0; pos < 2; pos++) {
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:#"checkbox_active.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(TXT_FLD_X_CORD+90, 295, 20, 20)];
// [but setCenter:CGPointMake( 50, i*40+20 )];
but.tag = pos;
/*if(pos==0)
{
[but setImage:[UIImage imageNamed:#"checkbox_active.png"] forState:UIControlStateNormal];
// [but setImage:[UIImage imageNamed:#"checkbox_active.png"] forState:UIControlStateSelected];
}*/
[but setCenter:CGPointMake(pos*90+125 ,310)];
[but addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:but];
}
UIButton has a tag property. Use that and in your buttonClicked method, you can check the button that was clicked based on it's tag. Might want to keep constants around for what button is what.
For each of your buttons set an appropriate tag, and then refer to the tag in your action. i.e.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
...
button.tag = 1
[view addSubview:button];
You can easily set the tag based on the index of your iteration, if you're creating buttons in a loop. And then in your action:
- (void)aButtonWasTapped:(UIButton *)source {
if( source.tag == 1 ) {
// etc
}
}
Somebody might have this problem:
Jason Coco's answer worked fine for me, until I wanted to use the tag to access properties from an NSArray that was defined as a property.
Turns out the property had to be defined as "retain" instead of "weak".
The Swift version (with Labels):
for index in 1...5 {
var label = UILabel()
label.tag = index
labelsDictionary["Label\(index)"] = label
self.addSubview(label)
}
Call using using self.viewWithTag(i) as UILabel:
(cell.viewWithTag(5) as UILabel).text

change button state in cellForRowAtIndexPath

this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray, I populate it.
I set up in reuseTableViewCellWithIdentifier with the following
cellRectangle = CGRectMake((ARROW_OFFSET + 5), (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, ARROW_WIDTH, LABEL_HEIGHT);
UIButton *tmpButton = [[UIButton alloc] initWithFrame:cellRectangle];
[tmpButton initWithFrame:cellRectangle];
[tmpButton setImage:[UIImage imageNamed:#"icon_edit.png"] forState:UIControlStateNormal];
[tmpButton setImage:[UIImage imageNamed:#"icon_no.png"] forState:UIControlStateDisabled];
[tmpButton addTarget:self action:#selector(editSelectedRow:) forControlEvents:UIControlEventTouchUpInside];
tmpButton.tag = ARROW_TAG;
[cell.contentView addSubview: tmpButton];
[tmpButton release];
then in cellForRowAtIndexPath I have the following lines of code
UIButton *button = (UIButton *)[cell viewWithTag:ARROW_TAG];
[button setTag:indexPath.row];
if (counterHasStarted == 1) {
NSLog(#"yes");
button.enabled = NO;
} else {
button.enabled = YES;
}
the button shows nicely, but for some reason when the counterHasStarted variable (which is an int is set, it doesn't change! I can change UILabels based on the above code (checking if counterHasChanged is 1 or 0).
Any ideas what's going on??
-cellForRowAtIndexPath: will only get called when the table view needs a new UITableViewCell because the user scrolled.
I guess you're changing counterHasStarted and expect the button enabled state to change? You could reload the data when you change counterHasStarted ([yourTableView reloadData]). Then the table view will call -cellForRowAtIndexPath: for all the cells that are currently visible, and you can enable or disable the buttons as needed.