ios next and previous buttons - ios

I have a next button that I want to disappear when I get to the last result of an array and I want my previous button to disappear when I am at the first result of the array. I have figured out how to make them disappear when only one result is found.
Contacts is the array.
next Button:
//button look
if (contacts != nil) {
if ([contacts count] > 1 ) [self.view addSubview:nextButton];
Previous button:
//button look
if (contacts != nil) {
if ([contacts count] > 1 ) [self.view addSubview:previousButton];
I have tried this
if ([contacts count] < (index-1)) [self.view addSubview:nextButton];
I get this error "Ordered comparison between pointer and integer ('NSUInterger'(aka 'unsigned int') and 'char * (*)(const char *,int)')" The button is there it just doesn't disappear when it gets to the last result.
Any help would be greatly appreciated.

It sounds like index is a NSUInterger. count is a int and NSUInteger is typedef to unsigned integer. try changing index where ever your setting it to a int or typecasting it to a int where your using it for comparison.
For the part about the button, if your logic is right and you are trying to remove "nextButton" from view you can either hide or remove it from the view like so:
// To remove button
if ([contacts count] < (index-1)) [nextButton removeFromSuperview];
or
//To hide button
if ([contacts count] < (index-1)) nextButton.hidden = YES;

You never remove it from the superView, so it will never go away. You should use the hidden property of the button i.e.
[nextButton setHidden:NO];
or YES if you want to hide it.
The error is probably from the index entity, did you typecast that as a NSUInterger? change that to a uint or an int. (just posted a bit late, but this has been mentioned in other answer :D)

if ([contacts objectAtIndex:[contacts count]-1])
{
[self.view addSubview:nextButton];
}
if([contacts objectAtIndex:0)
{
[self.view addSubview:previousButton];
}
Hope this helps...nil just tells it there are no more elements to be included in the array. U can't use it to check..

Related

Calling an IBAction from another method

I am new to Objective-C, Xcode and all of the good stuff. I am self teaching.
I am trying to call an IBAction from another method.
- (IBAction)strAdj:(UIStepper *)strStepper
{
// Converts stepper to integer
NSUInteger strvalue = strStepper.value;
// Changes the the text to the value of the stepper
strStat.text = [NSString stringWithFormat:#"%2d", (unsigned)strvalue];
_strMod.text = [NSString stringWithFormat:#"%2d", (unsigned)stepperAdj(strvalue)];
// Based on the value it change the strMod to a specific value
}
I am only posting a portion of the next code. It is a simple switch statement. I basically want to call the IBAction above in the Void below.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (row) {
case 0:
break;
// Core races never change
// defines dwarf stats
case 1:
// sets all the increases
tempCon = 2;
tempWis = 2;
tempChr = -2;
// resets all other stats
tempStr = 0;
tempDex = 0;
tempInt = 0;
// I want to call the IBAction here...
break;
This IBAction will need to occur ultimately 15 times. The above switch statement occurs when the picker is changed. Where as the IBAction happens every time the stepper occurs.
I hope that I am not too far from what I want to do. Again I have only been working with this for the last several days and I have not been able to find what I am looking for or if I did then I wasn't sure what to do.
It kinda looks like you have a dropdown for the player to select a race and each attribute (str, con, wis, etc.) has a stepper to bump the respective values up or down.
I don't know enough to speculate, but I suspect there is a better approach overall. That said, I think a brute force solution to what you are trying to do is to make sure each of your steppers has a referencing outlet: stepperWis, stepperCon, stepperInt, etc. Then in your switch you can do something like so (for each attribute):
stepperWis.value = (whatever value you want);
[self strAdj:self.stepperWis];
You can call UIButton TouchUpInside event programatically like :
[YourButtonObject sendActionsForControlEvents: UIControlEventTouchUpInside];

Changing the text of a label on button press with dynamic integer

I'm following a tutorial to create a simple game where you tap a button and the the game counts how many times you have pressed the button as you go along. The score is displayed on screen in the form of a label.
I can make it so that when you press the button the label text changes to say 'Pressed!'. But then i can not get the label to change when i try to add the changing score with a format specifier.
-(IBAction)buttonPressed{
count ++;
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
Any help would be greatly appreciated. Thanks!
You may not have your label set up for multiple lines of text. Try getting rid of the "\n" in your format string. Other than that, you need to tell us what happens. Have you put a breakpoint to make sure your button IBAction is being called? Have you checked to make sure "scoreLable" (sic) is not nil? The devil is in the details.
-(IBAction)buttonPressed : (id) sender{
UIButton * btnPressed = (UIButton *)sender;
int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];
NSLog(#"Button value = %d", buttonValue);
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
First You Can Set static int count = 0;

iPhone - how to select from a collection of UILabels?

I have a few UILabels, any one of which will update according to the index of an NSArray index they represent. I thought of selecting them by their tag
self.displayLabel.tag = myArray[index];
but that changes the tag value to whatever my array is holding at the moment
Using a dictionary for whatever tricks it offers instead of an NSArray doesn't help because i still have to select the correct matching label. This is the effect i want to achieve.
self.|mySelectedLabel|.text = myArray[index];
what should i put in |mySelectedLabel| to get the one i'm looking for?
I'm almost ashamed to ask at my reputation level, but this is stymie-ing me
every search only turns up how to set Labels and change, not the process of selecting
Assuming you have set the tags to the appropriate index to match your
array indices you can use [self.view viewWithTag:index];
Why are you not setting the tag with:
self.displayLabel.tag = index;
Also, you could just loop though an array of labels and find the right one:
for (UILabel *label : labelArray) {
if (label.tag == index) {
label.text = #"I found you!";
}
}
Rather than using tags you can refer to your specific textfields by reference:
// Create an array to hold your textfields
NSMutableArray *textFields = [NSMutableArray array]
// Create your textfields and add them to the array
UITextField *textField;
for (NSUInteger idx = 0: idx++; idx < numberOfTextFieldsYouWant) {
textField = [UITextField alloc] initWithFrame:<whateverYouWant>];
[textFields addObject:textField];
}
Since you are adding the objects to an array, rather than using the tag value 0, 1, 2... you can just access it by it's index in the array
So, for what you want to do you can just do:
textfields[index].text = myArray[index];
It's a lot cleaner, doesn't rely on magic tags, and you have an array of all your dynamic textfields that you can remove, or change in one place.
I think tags are vastly overused, and they aren't necessary in most cases.
Just letting you know I reframed the problem and this eventually worked for me without having to use an array
( with endless experimenting, I sort of bumped into it so I don't know if it constitutes good technique )
the desired label corresponding to the bag weight ( one of a number possible ) displays the right update
- (IBAction)acceptWeight:(UIButton *)sender {
int tempValue = (int) currentWeight;
// current weight comes from a UISegementedController
for (UILabel *labels in self.view.subviews)
{
if (labels.tag == currentWeight)
{
bags[tempValue]++;
labels.text = [NSString stringWithFormat:#"%i",bags[tempValue]];
}
}
totalKilo = totalKilo + (int)currentWeight;
self.totalKilo.text = [NSString stringWithFormat:#"%d",totalKilo];
}

iOS: Moving a uibutton by its tag outside of where it was generated

Is it possible? I have an array of buttons created in an void and I want to move one when a neighbouring one is called in the buttonTapped void. I have no trouble moving the button thats pressed because it is the sender, but I also need to move the one next to it and can't seem to get it to move. Each button in the array has a tag value so they're unique.
Thanks
Reasonable intuitive assumption: you generated the buttons so that they are ordered in the array...
- (void)moveButton:(UIButton *)sender // whatever
{
NSUInteger idx = [buttonArray indexOfObject:sender] + 1;
UIButton *nextButton = idx < buttonArray.count ? [buttonArray objectAtIndex:idx] : nil;
// do something with `nextButton`
}

UISlider value changed update result

I have three labels receiving output from three UISliders. A result is calculated and placed in a fourth label when a segmented control is touched.
How do I make the result update if one of the slider values is changed without touching the segmented control again?
First, add an event for your slider changing values:
[mySlider addTarget:self
action:#selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
You will want to do this for each slider.
In your sliderValueChanged: function:
- (void)sliderValueChanged:(id)sender {
[self calculateResult];
}
In calculateResult you can do your calculations and set your result box.
If you need to make sure your segmented control is in some state first, just add an if clause to sliderValueChanged:
- (void)sliderValueChanged:(id)sender {
if ([mySegmentedControl selectedSegmentIndex] == 1) {
[self calculateResult];
}
}
I came up with something that worked using mopsled's answer:
if (UIControlEventValueChanged && mySeg.selectedSegmentIndex == 0) {
float floatResult = [f3.text floatValue]-[f2.text floatValue];
float coldFactor = [fcold.text floatValue]*floatResult;
float result = [f1.text floatValue]+coldFactor;
NSString *tpResult = [[NSString alloc] initWithFormat:#"%.0f",result];
f4Result.text = tpResult;
}

Resources