Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:30:08+00:00 2026-06-02T07:30:08+00:00

Ok so I have a segmentedControl which I need to dynamically remove and add

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:30:09+00:00Added an answer on June 2, 2026 at 7:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Core Data app that works to add or remove one of
I'd like to have a UIViewController which has an Add and a Trash button
I have a tableview with a segmentedControl in the header, i want the segment
hii every one i have created a data entry screen like this in which
I have a sorting comparator which I need to use in a few different
I am trying to develop an iPad app in which I need to have
I have a UITableView with several datasources. This is because, I need to switch
I know this is a very stupid question to ask but i have a
I have an application in which I have a segmented control with seven segments
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.