Get UIButton from view programmatically - objective-c

Im creating and adding a grid of buttons to my custom view keyboardView as follows:
int offset = 0;
for (int row = 0; row<4; row++){
for (int col = 0; col<13;col++) {
offset +=1;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(5+col*65+offset,5+row*65, 60, 60);
[aButton setTitle:myarray[row][col] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[keyboardView addSubview: aButton];
}
}
I need certain buttons to be of different sizes, like the return key or space bar. How can i get a reference to a particular button programmatically, later on in the same method? Is there an easier way than setting the tag and then calling [keyboardView viewWithTag:t]? Becauseint's are going to get confusing.
Thanks.

You could make instance variables like UIButton *spaceBar. if you reach the Button in the two for-Iretations which is thought to be the spacebar just do spacebar = aButton.
So you can later in the method just use this instance Variable which refers to the specified button. ;-)
I hope it's more or less understandable. ^^

You can either do it with UIView tags (which don't have to get confusing, just create an enum), or if you have only a few "special" UIButtons, you can create ivars to keep references to them.

Related

How to move an image with a button?

I have an image(clubcow.png) that is passed from an nsarray string to make a picture. Then facedowncow represents the picture. I was wondering how to make a button that will move facedowncow to position (100,100) when button is tapped. Any tips will be appreciated. Also, there is more to this code, I just posted the important parts to give an idea on what is going on.
cardKeys = [[NSArray alloc] initWithObjects:#"clubcow", nil];
currentName = [NSMutableString stringWithFormat:#"%#.png", [cowsShuffled objectAtIndex:currentcow]];
faceDowncow = (UIImageView *)[self.view viewWithTag:1];
faceDowncow.userInteractionEnabled = YES;
I would start by creating a UIButton and adding it to your view controller's view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 20);
[button setTitle:#"Tap Me" forState:UIControlStateNormal];
[button addTarget:self action:#selector(animateImage:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then, link this button to a function that will animate your faceDowncow object. You could add your faceDowncow as a property of the view controller so the following function can easily reference it:
- (void)animateImage:(UIButton *)sender {
[UIView animateWithDuration:0.2
animations:^{
// change origin of frame
faceDowncow.frame = CGRectMake(100, 100, faceDowncow.frame.size.width, faceDowncow.frame.size.height);
} completion:^(BOOL finished){
// do something after animation
}];
}
First of all, it looks like this code is from a view controller subclass, and the cow is a subview. In that case, you should probably have a property for it rather than obtaining it by its tag all the time. If it's instantiated in a storyboard scene/nib then you can hook up an outlet to a property/ivar in your subclass fairly easily.
The easiest way to do what you want is to create the button and use target action so that when it is tapped, it calls a method in your view controller. In the method body, obtain a reference to your cow and set it's frame property, like so:
[faceDowncow setFrame: CGRectMake(100,100,faceDowncow.bounds.size.width,faceDowncow.bounds.size.height)];
If you don't know how target action works, I suggest reading Apple's documentation on the matter. It's as simple as getting a button, calling one method to tell it what events should make a certain method get called, and then implementing that method.

Creating/Displaying Interface Objects with Code for the iPhone [Objective-C]

I'm currently designing an app that calls for dynamically creating objects and then displaying them in a ViewController. My problem is wading through the documentation to try and create the objects I need. I've done some searching and even tried to just sit down and figure it out, but alas, I've got nothing to show for it. I already know how to dynamically create the ViewController but I can't seem to get any objects in it.
Does anyone know how I could go about creating objects (Say a button and a slider) dynamically with Objective-C in XCode?
Creating a button in code is simple
- (void)addButton {
// Create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set the frame (location and size)
[button setFrame:CGRectMake(0, 0, 40, 30)];
// Set what happens when you touch the button
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// Set button text
[button.titleLabel setText:#"Button"];
// Add button to view
[self.view addSubview:button];
}
A lot of interface elements follow a similar patter- alloc and init the object, set the frame and add it to a view.

Best way to create dynamic list of links/buttons in iOS5 view controller

I want to create a dynamic set of links/buttons in an iOS5 view controller and am trying to figure out what might be the best way to do this.
For eg:
Item 1
Item 2
Item 3
:
:
Item N
Each of the Items is a link/button that is clickable and will do some action, like load another screen etc based on the link.
I don't know ahead of time how many items there might be so if all the items don't fit on the screen, I need to be able to scroll to view.
My question:
1. What is a better way of doing this? I could just create the labels and buttons dynamically but this seems rather cumbersome and I'm not entirely sure how I would differentiate between the different buttons (essentially I'd need some index to find out which Item was clicked).
2. Alternatively, I was wondering if I can just render this page as HTML and just have links? I've never done this and not sure how I'd associate a button with a link.
Any suggestions?
AK
You can try to use the tag property to store the index value you need when you create the button. Then evaluate it in the button tap handler by accessing using button.tag.
Maybe you can try Cordova for an HTML based approach. I'm not too familiar with it though, so I can't say for sure.
Hope it helps.
(1) You can assign UIButton tag property based on the button index. If any events were to trigger, you could recognize which button the event belongs to by checking the tag.
Sample :
// Initializing some buttons
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 1;
[button1 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag = 2;
[button2 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
Selector for button events :
- (void)buttonPushed:(id)sender {
...
if ([sender tag] == 1) {
// do something after button1 event
} else if () {
// do something after button2 event
}
...
}
(2) If you choose to do it in HTML, you could check out CMHTMLView

Loop across all outlets

I want to do a loop across every nine outlets (UIButton's, called btn1, btn2, btn3... btn9) that I have, like:
for(int i = 0; i < 9; i++) {
[[btn(%#), i] setImage:someOne forState:UIControlStateNormal]; // I know that this is ridiculous, but it's just a way to demonstrate what I'm saying. :-)
}
Any tip?
Thanks a lot!
You may want to check out IBOutletCollection (apple doc here) which allows you to connect multiple buttons to the same outlet and access them as you would a regular NSArray.
Have all the outlets you want to loop to loop through on a separate view.
for(int subviewIter=0;subviewIter<[view.subviews count];subviewIter++)
{
UIbutton *button = (UIbutton*)[view.subviews objectAtIndex:subviewIter];
// Do something with button.
}
While creating UIButton, you can set tag property of button. Now there can be several ways of accessing that button, like one is -
NSArray *subViews = self.view.subviews;
for (int index = 0; index < [subViews count]; index++) {
if ([subViews objectAtIndex:index] isKindOfClass:[UIButton Class]) {
//Button is accessible now, Check for tag and set image accordingly.
}
}
If you would like to do that you should think what congregates all the UIView instances or in your case: buttons.
I would suggest you to add all the buttons to an array or any other kind of data format that helps you manage your objects.
Should you like to do that without using an external object for that purpose, I would suggest you to add all the buttons to a superview and then, you will be able to iterate over the subviews of the superview using: mySuperview.subviews property.
You could also give a unique ID number to each button (tag) right after when you initialize it, and then you can access tha button usIng the given tag:
myButton.tag = 1;
//Access the button using:
UIButton *b = (UIButton *) [superview viewWithTag:1];

objective-c "add buttons automatically"

i need any kind of idea.
this is the problem. i am parsing a xml file which contains the url of an image, name of buttons, url for link to another UIwebview or view, etc... those are the important. what i need is; if a read a name of the button, on the screen must to add a new button automatically with its own image and link. i mean if in the xml i have 6 tags with the information mentioned previously, in the screen must to have 6 buttons with image and link. if in the xml exists more they must exist in the screen or uiwebview too.
i appreciate your help or ideas!!
Well, you can begin by using the NSXMLParser to parse the XML and obtain the properties for your buttons.
NSXMLParser Class Reference
Once you know how many tags you need, you can iterate:
for (int i = 0; i < numTags; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// The tag is so when clicked, you can determine which one was pressed
// It would help to have your links stored in an NSArray, so you can pull
// them out by index.
button.tag = i;
[button setTitle:buttonName forState:UIControlStateNormal];
[button addTarget:self action:#selector(openButtonLink:) forControlEvents:UIControlEventTouchDown];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = ...; // How do you want your buttons laid out?
[someView addSubview:button];
}
I would also recommend checking out the class reference for UIButton:
UIButton Class Reference