Differentiating between dynamically generated buttons - iphone

I have ten options on a view controller, each of which 'pushes' to the same new view controller displaying a specific amount of buttons (for each option a different amount of buttons may be available, ranging from 3 buttons through to 15). Currently, my code is performing similarily to the answer posted on this question.
All these buttons are created dynamically (the amount obviously depending on the length of the array) for each option using a for-loop:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if(option 1) {
Array contains different values...
}
if(option 2) {
Array contains different values...
}
etc...
for(int i = 0; i < xCoordinatePosition.count; i++) {
UIButton *imageOne = [UIButton buttonWithType:UIButtonTypeCustom];
[imageOne setTag:i];
[imageOne addTarget:self action:#selector(webViewChanged:) forControlEvents:UIControlEventTouchUpInside];
}
}
As you can see I've set a tag for each button to differentiate between the dynamically created buttons, as I want each one to display a different UIWebView when selected.
When I've selected an option and NSLog the resulting tags of each button on a results page, I get the reply I'm after: 1, 2, 3 etc.
- (IBAction)webViewChanged:(UIButton*)sender
{
NSLog(#"%d", sender.tag);
}
The problem is, I want a unique number for each button in regard to ALL option buttons - currently each of my ten options returns with buttons that have the tags 1, 2, 3 etc. up to ten, whereas I need the first option return with 1-10, the second option return with 11-20 etc, as each individual button will be returning something unique.
For example:
Clicking the button with the tag of 7 on one option will bring up a completely different web-view to clicking on a button with the tag of 7 on another, therefore I need to make a distinction between every button.
Does anyone know how I could set the tag so that is unique for each option (instead of just setting each button of the currently chosen option to be unique like it does currently)?

Just use another integerVariable which have the values multiple of 10.
int factor;
if(option 1)
{
factor = 0;
}
if(option 2)
{
factor = 10;
}
etc..
Then put tag as,
[imageOne setTag:i+factor];

If I understood your question correctly, one way to solve the problem can be to set a base tag for each option like 100, 200, ...
So, in your prepareForSegue:
int baseTag;
if(option 1) {
// Array contains different values...
baseTag = 100;
}
if(option 2) {
// Array contains different values...
baseTag = 200;
}
// etc...
And in the for loop, simply:
[imageOne setTag: baseTag + i];
For option 1, your image tags will start from 101, and for option 2, from 201, etc.

Related

How to make jbutton sum up the values of my jradiobuttons

I'm making a project in jFrame. It has 15 jRadiobuttons. What I want is a jButton to add the selected jRadioButtons and display the sum in a jTextField. Can anyone help me? I've been doing this for 3 days and cant get it right.
So you have numbers as values of the radiobuttons?
Then try it like this:
When the button is clicked, go through a foreach loop of all radiobuttons.
in the loop, check if they are selected. If they are, you add their value to the Sum.
Is that what you were asking for?
EDIT
You create A List containing all radiobuttons.
In the Listener of the Button you write:
int sum = 0;
//foreach loop
for (jRadiobutton rbutton : rbuttons){
//Check if the button is selected
if (rbutton.isSelected){
sum += rbutton.Value;
}
}
I hope this is good enough explained.
Maybe the Syntax isn't correct, i haven't used java for a while now...

Image sorting bug in Scrollview - iPad application

We are building a catalog app that has 2 rows of sorted images with varying widths but same height. We draw the artwork on scrollview in sorted order (from A to Z) As per attached image.
We search using a alphabetically ordered bar on top that has letters (A to Z) If i touch on letter J the i want the artwork which starts from j comes first. The code we are using is not working well. E.g. clicking on J takes us to A. Will greatly appreciate your help or advice.
Code is as follows:
NSString *newStr = [currentArtworkTitle substringWithRange:NSMakeRange(0,1)];
if([newStr isEqualToString:self.searchString])
if (scrollViewTopRowWidth > scrollViewBottomRowWidth) {
xCordForSortedView = scrollViewTopRowWidth - c - imgForButton.size.width;//self.touchLengthCount;//50 ;// -10//c scrollViewBottomRowWidth
}
else {
xCordForSortedView = scrollViewBottomRowWidth - c - imgForButton.size.width;// self.touchLengthCount;//40;//scrollViewTopRowWidth
}
// For shifting the screen
if(scrollViewTopRowWidth > scrollViewBottomRowWidth){
int tmpMargin = scrollViewTopRowWidth - xCordForSortedView;
if(tmpMargin < 1024)
scrollView.contentSize = CGSizeMake(scrollViewTopRowWidth+1024,scrollView.bounds.size.height);
}
else{
int tmpMargin = scrollViewBottomRowWidth - xCordForSortedView;
if(tmpMargin < 1024)
scrollView.contentSize = CGSizeMake(scrollViewBottomRowWidth+1024,scrollView.bounds.size.height);
}
[scrollView scrollRectToVisible:CGRectMake(xCordForSortedView, 0,scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
I won't edit your code but I can give you a hint.
Look, when you populate the scrollwiew, by adding an imageview, add also an entry into a NSMutableDictionary (instance variable), so the key will be the A-Z letter, and the value will be the x position of the image. When you finish populating the scrollview, you will have also the dictionary containing the position of all your images (and you need only the x) and the corresponding letter. Now you tap "J" and hit "Search" - you parse your dictionary to fing the object with the key "J" and read it's value. Use this (x) value to set your srollview's contentOffset. Tada!

UISlider and continuous property

I am changing a value with UISlider and this value is the number of my PDF pages , so when users sliding they can navigate through the pages , because of tiles rendering I need set continuous property to NO, also I need a live page counter that uses with the same slider, so the property should be YES :, how can I set the property yes or no for specific codes with the same slider ?
this part , the property should be NO
navSlider.continuous = NO;
pageInt = CGPDFDocumentGetNumberOfPages(pdfscroll.pdf);
prgress = (int)(navSlider.value + 0.5);
navSlider.maximumValue = pageInt;
navSlider.minimumValue = 1;
[bookController moveToPage:prgress];
and here yes :
navSlider.continuous = YES;
NSString *numberOfPage = [NSString stringWithFormat:#"%d of %d",prgress,pageInt];
pageCounter.text = numberOfPage;
You clearly do need to know about every slider change, so it must be continuous. However, you should not call [bookController moveToPage:prgress]; after every single slider movement. The solution is to update the page number for every slider movement, but only call [bookController moveToPage:prgress]; when the slider is released.

If using Xcode to make an iOS calculator, how would I add a decimal button?

How to add a decimal button so I don't have to do whole numbers? If i wanted any decimal number like 1.2 or 100.4 or 3.0 or anything like that, how would I add it to the calculator I'm making?
You're not giving me much information in your question. How do the other buttons work?
If I was making a calculator I would have a label at the top showing the current reading. On press of a number button I would update the label with the number at the end.
For a decimal button you just add a . to the end of the label. You might want to have a global variable BOOL hasDecimalPlace and set it to true so you know if there is already a decimal place. Just remember to set it to false again when you clear the view or do a calculation or similar.
- (IBAction)Decimal:(id)sender{
NSString *currentText = Text.text;
if ([currentText rangeOfString:#"." options:NSBackwardsSearch].length == 0) {
Text.text = [Text.text stringByAppendingString:#"."];
}
}

How to change a button name through an array?

How do I fetch the value from API to an array and put that value into a button as its title from starting index.
For example, button 1 is named by array, index 0; button 2 is named by index 1 and so on.
[urButton setTitle:[urArray objectAtIndex:index] forState:UIControlStateNormal];