Swift Segmented Control default state - swift

I have a segmented control that works lovely if you tap one of the segments BUT if you don't tap anything and let it sit at the natural resting spot which is the first segment it doesnt work
I tested this by tapping another segment and continuing the app and it worked, and i also tested by tapping another segment and then going back to the first segment and it worked
#IBAction func segmentedControl(sender: UISegmentedControl) {
switch segmenControl.selectedSegmentIndex {
case 0:
numberOfParticipants = "4"
case 1:
numberOfParticipants = "6"
case 2:
numberOfParticipants = "8"
case 3:
numberOfParticipants = "10"
case 4:
numberOfParticipants = "12"
case 5:
numberOfParticipants = "14"
case 6:
numberOfParticipants = "16"
default:
break
}
}
It makes sense since it is wrapped in an IBAction and nothing is being pressed therefore it's not being triggered. How do i fix this issue?
Edit
By doesn't work i mean if the segment that is highlighted says 4 AND the user taps a continue button (different button) then a variable gets updated with a string of "4" and then firebase gets updated to say x = 4
So what is happening is when you land on the page by default the 4 segment is highlighted and if you want 4 to be your option you would leave it alone and hit the continue button...my problem is that if you don't interact with the segment control and actually tap 4 then the code never gets run, but if you tapped 6 then back to 4 it would trigger

Your questions in not very clear in defining what exactly seems to be broken. What doesn't work if you don't tap it? In general there are 2 steps involved in using UISegmentedControls. In step 1 you set it in your initialization - i.e. viewDidLoad - via:
segmenControl.selectedSegmentIndex = your_initial_value
Of course your initial value cannot be numberOfParticipants because the control only has 6 values..
Then step 2 is something like your IBAction which looks fine. Your use of magic numbers is of course a bit problematic.

This is how I understand the question:
You have a "Continue" button and if you hit it, you want it to reflect the state of the segmented control. Correct?
If that's the case, you don't need the IBAction, just read segmentControl.selectedSegmentIndex directly in IBAction of the Continue button.

I understand your question and had the same problem myself.
I experimented as I did not want any Segment in my Segmented Control to be highlighted or selected at all as this would cause confusion to my users and if the highlighted start index was actually what the User required,then when pressed did not perform the Action within the Switch Case area.
So... I made the Index Value -1 and this does not highlight any part of the Segment at all, it works a treat for my requirements and makes the user select the desired segment.
I set this up within my ViewDidLoad()
mySegmentedControlOutlet.selectedSegmentIndex = -1

Related

Moving a label inside another label and detect it using UIPanGestureRecognizer

Let say I want to move a label inside another empty label or empty box, can I use an empty UILabel? How can I detect that my label is 'over' my empty label?
My app will have let say 5 different numbers, 1 being the right answer. I want to be able to put these numbers in an empty box and pop up a message saying a good or bad message.
At this point my label is moving but I don't detect it when it comes over my empty label.
try
if label == "" {
your action
} else {
another action or nothing
}

Is there a statement that says "if this button has been pushed (not necessarily when it has been pushed, execute.."?

I'm not sure if I could do this with a do-while statement or what; I have a color-changing game that I am working on and I need a statement that will basically say:
If a button (one of the color buttons) HAS BEEN pushed (as in in the immediate past), then when the user goes to play the game by touching one of the other buttons, supply in this image. If not, continue to supply in this image.
I am a beginner so please explain all suggestions thoroughly. Thank you!
You could have a variable that is turned to True when the button is pressed and False when the other button is pressed. You could set the image to play only when the variable is True.
So if Button x sets the first image and Button y starts the game/other image it could be something like this:
Button x == Pressed:
xpressed = True
if xpressed == True:
DisplayImageHere
Button y == Pressed:
ypressed = True
xpressed = False
if ypressed == True:
DisplayImageHere/Start Game
If you run something similar in a loop it could work.

uisegmented control manually setting selected index not working

Ok so I have a segmentedControl which I need to dynamically remove and add segments to. This piece of code will remove the first segment but then doesnt set the correct segment to be selected. The strange thing is when I print it out all the numbers are correct it is just the highlighted segement on the screen which is always out by -1.
if ([outControl numberOfSegments]==4) {
int previous = [outControl selectedSegmentIndex];
if (previous>0) {
previous--;
}
[outControl removeSegmentAtIndex:0 animated:YES];
NSLog(#"setting to %d with %d segments", previous ,[outControl numberOfSegments]);
[outControl setSelectedSegmentIndex:previous];
}
Here is the log setting to 1 with 3 segments. With this log segment 0 is actually selected in the control and I cannot press segment 1 as the control thinks it is selected?
I can press segment 0 though even though that is the one being displayed as selected.
Here is a screen grab to try and clarify my issue.
image
The text in the cell is correct as cell 1 is actually selected as you can see by the log. But the highlighted cell is 0!? Its driving me crazy. It all works when adding segments its only when removing I get the issue.
Can anyone see where its going wrong??
Many thanks
Jules
I have noticed that even if i do not set the new selected segment I get the same results ie the wrong segment is selected.....?? Also this is all coded with cellForRowAtIndexPath if that helps anyone?
The code looks right yet behaves (wrongly) just as you described. It seems that the control gets in an awkward state when changing the selection in the same turn of the run loop as the segment deletion. But this works:
-(void)setSegmentedSelection:(NSNumber *)indexNum {
NSInteger index = [indexNum intValue];
[outControl setSelectedSegmentIndex:index];
}
// then instead of setting the selected index after the segment is removed,
// let the run loop return...
[self performSelector:#selector(setSegmentedSelection:) withObject:[NSNumber numberWithInt:previous] afterDelay:0.0];
I'm surprised that it doesn't work as you coded, but at least here's a workaround until we figure it out.

Scala: Button in Table Cell does not "fire" Action

I've got a little Problem: I want to have Buttons in some of the cells of my Table. As rendering component I return a Button with the following code: (theres application specific and debugging code in this example but you'll get the picture)
class LivingTreeButton(lt:LivingTree[_], client:TableBehaviourClient) extends Button(Action("xx") {
println("fire!")
lt.expanded = !lt.expanded
client.refresh
}){
println("I print therefore I am")
}
now when I scroll to one of the Buttons in the Table I see the "I print therefore I am" printouts and I do see the buttons with the "xx" text. But when I press one of the buttons nothing happens and I don't even see the "fire!" printouts.
It doesn't work neither, when I define the Action in the Buttons body instead of the constructor.
As further background info:
I'm not blocking the tables Events or anything. I only have a to listeners set up in JTable
peer.getColumnModel().addColumnModelListener(behaviourWorker)
peer.getTableHeader().addMouseListener(behaviourWorker)
and only temporarily block one of my own events in the Tables subclass:
listenTo(this.selection)
reactions += {
case e#TableRowsSelected(_,_,true) => if(!blockSelectionEvents) publish(PimpedTableSelectionEvent(this))
}
has any of you ever struggled with the same problem or has any idea what might be going wrong. After 2 hours od resultless debugging I would be thankfull for ANY hint.
Guess I got it. Didn't know that I have to add a TableCellEditor to catch the events. Doesn't really work yet but I'm sure that's it.

UISegmented Contol set to Momentary Handler

This is probably a really simple question but I can't seem to find anything in the APIs or across any search engine.
I have a Segmented control that i have set to momentary as a user will select a couple of makes of a car that they want to search for. The issue that I'm running into is that I can't seem to figure out how to recognize which segment was selected. In regular mode its a simple SelectedSegment = index but with momentary its my understanding that the selected segment is always -1 as none are ever "selected"
I have a handler for ValueChanged but I can't figure out what I'm checking for or what I should be sending to determine which segment was selected. Any help would be greatly appreciated. I'm using monotouch but Obj-C would be fine as well.
Thanks!
In your handler, you should check the selectedSegmentIndex to determine which segment was selected:
- (void)valueChanged:(UISegmentedControl *) control {
switch([control selectedSegmentIndex]) {
case 0:
//...
break;
case 1:
//...
break;
}
}