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 6953727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:30:59+00:00 2026-05-27T14:30:59+00:00

I have implemented several of UISegmentedControl objects before but for one or other reason

  • 0

I have implemented several of UISegmentedControl objects before but for one or other reason I am unable to implement it within a tableview cell. I want the user to choose his gender. if the value of the segmentedControl changed a method gets called to save it for further processing.

self.seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Man",@"Female", nil]];
[self.seg addTarget:self action:@selector(toggleGender) forControlEvents:UIControlEventValueChanged];

I created the method:

-(void)toggleGender{
    NSLog(@"%d",self.seg.selectedSegmentIndex)
    if (self.seg.selectedSegmentIndex == 0)
    {
         NSLog(@"User is a man");
    } else if (self.seg.selectedSegmentIndex == 1)
    {    NSLog(@"User is a female");
    }
}

The first NSLog prints -1 as value if the value has changed.

I declared seg in the header file, and created a property and synthesized it!
I am currently NOT dealllocing this object. (I know this is a memory leak)

Why am I unable to read-out the selected segment, and why does it give back -1 as selected item?

  • 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-05-27T14:30:59+00:00Added an answer on May 27, 2026 at 2:30 pm

    Try this :

    self.seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Man",@"Female", nil]];
    [self.seg addTarget:self action:@selector(toggleGender:) forControlEvents:UIControlEventValueChanged];
    

    (NOTE THE ‘:’ in the @selector..). And change your function by this :

    -(void)toggleGender:(id)sender
    {
        if ([sender isKindOfClass:[UISegmentedControl class]]) 
        {
            NSLog(@"%d",[(UISegmentedControl *)sender selectedSegmentIndex]);
            if ([(UISegmentedControl *)sender selectedSegmentIndex] == 0)
            {
                NSLog(@"User is a man");
            }
            else if ([(UISegmentedControl *)sender selectedSegmentIndex] == 1)
            {
                NSLog(@"User is a female");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented authentication systems for webapps several times over the years, but before
I have a view controller class that has to implement several protocols. Too keep
I have several server controls that implement the IValidator interface. As such, they have
I have an HTML table with several columns and I need to implement a
If I have interface IFoo, and have several classes that implement it, what is
I have implemented drag and drop in my application, but am having some difficulty
I'm attempting to implement this paper . I've have most of it down, but
I have multiple custom UIWebView's as subviews within a UIScrollView. I have implemented the
I have implemented a user control that I reuse on several pages. However, I
Writing simple parsers are trivial and I have implemented several over the years. In

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.