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

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

Related

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.

How to modify UISwitchController so it ended up looking like this:

Basically I want to customize the background and the button. I want the button to be square. Things like that.
My designers already provide me with this pictures:
A thing I am looking into is to just use UIButton. However, how do I make UIButton to be on switch mode? What should I do so that if button is clicked then it becomes "selected"
The result that I want is this:
This code may help Your query , you have to modify it ..
https://github.com/domesticcatsoftware/DCRoundSwitch
Thanks.
UIButton *switchButton = [[UIButton alloc] init];
[switchButton setBackgroundImage:offImage forState:UIControlStateNormal];
[switchButton setBackgroundImage:onImage forState:UIControlStateSelected];
When you want the button to look "on", just do: switchButton.selected = YES; after it has been tapped or whenever else you want to set it to that state.

iOS how to identify which image (button) is pressed?

I've three randomly generated images( actually UIButton) and when application runs it asks the user to choose one random image among the three(say a.png).after user select the image app will do some thing based on correct image was selected or not.
Now the question is that how can i identify that user select right image? I'm trying to get name of image that user select and then check it, but really no idea how to do that.
I've searched google for this but can't find something useful.
Can anyone help?
Thanks
When you really want to store the name of the image into the button, instead of using the tagproperty, you could set it to the title and hide the titleLabel (apple reference says: Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance of the button label.):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
button.titleLabel.hidden = true;
[button setTitle:#"a.png" forState:UIControlStateNormal];
then you can access to the imagename with button.currentTitle.
While generating your UIButtons you can fill the tag property.
Then you can check if your sender.tag equals the value you filled in.
When you only want to check if the correct image is selected, you can set only this one to 1 and the other buttons to 0.
To know which button clicked u need to add tag say from 0, 1, 2
[yourButton setTag:0]; //set tag 1 and 2 also to other button
Now add same selector or method to all buttons:
[yourButton setTarget:#selector(buttonClicked:) forState:UIControlTouchUPInside];
// setTarget to other buttons too
method would be this:
-(void)buttonClicked:(id)sender
{
if([sender tag] == 0)
{
//button 1 clicked
}
else if([sender tag] == 1)
{
//button 2 clicked
}
else if([sender tag] == 2)
{
//button 3 clicked
}
}
Well if you are using UIButtons for this purpose, you could set Target Selector on each button and determine the UIButton using their tag values.
There's also one more method to help. But only of you use 'backgroundImage'.This action is should be linked with your buttons:
-(IBAction)photoIconClicked:(id)sender{
if ([sender isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)sender;
_currentImage = button.currentBackgroundImage;
}
}

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

UIButton's target is fired before animation

I have programmed a Quiz in Xcode where I've created UIButtons programmatically displaying the answer to a question. When the user clicks the button the next question is loaded and the buttons position i shuffled to not make the right answer in the same place for each question (the right answer is however under the same UIButton for every question.)
This is how I create my buttons:
if (self.buttonRightAnswer == nil)
{
self.buttonRightAnswer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.buttonRightAnswer addTarget:self action:#selector(rightAnswer) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.buttonRightAnswer];
}
self.buttonRightAnswer.frame = CGRectMake(20.0, [[randomPositionArray objectAtIndex:0] floatValue], 280.0, 40.0);
[self.buttonRightAnswer setTitle:[[self.quiz.arrayWithQuestions objectAtIndex:questionNr - 1]valueForKey:#"RightAnswer"] forState:UIControlStateNormal];
When the user clicks the right an answer the next question loads. Here it's a problem. The next question loads with the same method only shuffled positions and the animation of the press-down of the button is drawn after the buttons been moved resulting in the user seeing where the right answer is now.
How can I wait for the animation before moving the buttons?
I could solve this by having the buttons stay in one place, shuffling out the questions over the buttons and comparing the users answer with the right answer.. but this requires a hell of a lot more code.
You can use some of shit-code and insert some kind of 'sleep' before next shuffling buttons
Before you create the buttons againg (after a question was answered), you can add something like (if you use differen images for button states)
-(void)waitUntilButtonDeselected
{
bool selected = YES;
while (selected)
{
bool selected = (self.buttonRightAnswer.currentBackgroundImage == UIImageOfNotSelectedButton);
}
}
The idea here is to check if the image of the button corresponds to not selected button.
Typically when using a UIButton, events are triggered on UIControlEventTouchUpInside, or at the end of the touch.
You're triggering it on the touch down which is causing issues with the animation you want to happen.
It might be simpler for you to switch to using UIControlEventTouchUpInside instead of UIControlEventTouchDown since this should solve your problem and be more consistent with other iPhone apps.