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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:01:29+00:00 2026-06-03T08:01:29+00:00

I have a segment on my tableview controller. segmentIndex=0 -> tableView getting loaded ->

  • 0

I have a segment on my tableview controller.

segmentIndex=0 -> tableView getting loaded -> tap. navigating to another tableViewController.
segmentIndex=1 -> tableView getting loaded -> tap. navigating to another tableViewController.

Switching tableviews happens fine. The problem is that after navigating back I need to remember the previous index value. How to implement it? My current code:

- viewWillAppear
self.segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Nominals",@"Coinage",nil]];
[self.segment setFrame:CGRectMake(80, 0, 200, 30)];
[self.segment setSegmentedControlStyle:UISegmentedControlStyleBar];
//self.segment.selectedSegmentIndex=0;
[self.segment addTarget:self action:@selector(segmentValueChaged:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:self.segment];


-(IBAction)segmentValueChaged:(id)sender
{
switch (self.segment.selectedSegmentIndex) 
{
    case 0:
    {
        DBAccess *access=[[DBAccess alloc]init];
        self.items=[access returnNominals:self.subCountryID nk:fromPeriod];
        [access closeDataBase];
        self.tableView.hidden=NO;
        [self.tableView reloadData];
        self.segmentIndex=0;
         break;
    }
    case 1:
    {
        DBAccess *access=[[DBAccess alloc]init];
        self.coinageArr=[access returnCoinage:fromPeriod period:subCountryID];
        self.tableView.hidden=NO;
        [self.tableView reloadData];
        self.segmentIndex=1;
        break;
    }
}
}

Thank you in advance guys.

EDIT:

self.segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Nominals",@"Coinage",nil]];
[self.segment setFrame:CGRectMake(80, 0, 200, 30)];
[self.segment setSegmentedControlStyle:UISegmentedControlStyleBar];
if(!self.segmentIndex)
{
    self.segment.selectedSegmentIndex=-1;
}
else 
{
    self.segment.selectedSegmentIndex=self.segmentIndex;
}

//self.segment.selectedSegmentIndex=self.segmentIndex;
[self.segment addTarget:self action:@selector(segmentValueChaged:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:self.segment];


-(void)viewWillDisappear:(BOOL)animated
{
switch (self.segment.selectedSegmentIndex) 
{
    case 0:
    {
        self.segmentIndex=0;
        break;
    }
    case 1:
    {
        self.segmentIndex=1;
        break;
    }
}

}
  • 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-03T08:01:30+00:00Added an answer on June 3, 2026 at 8:01 am

    You store your value in viewWillDisappear and when you come back to your controller viewWillAppearyou set back the value you stored. So what you could also do is, start by setting the value to… let’s say -1. if in your viewWillAppearvalue is -1, then you set your default value, else, you use that value.

    - (void)viewWillAppear:(BOOL)animated {
        self.segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Nominals",@"Coinage",nil]];
        [self.segment setFrame:CGRectMake(80, 0, 200, 30)];
        [self.segment setSegmentedControlStyle:UISegmentedControlStyleBar];
    
        if (self->_currentIndex == -1) {
            NSInteger myDefaultValue = 0;
            self.segment.selectedSegmentIndex=myDefaultValue;
        } else {
            self.segment.selectedSegmentIndex=self->_currentIndex;
        }
    
       [self.segment addTarget:self action:@selector(segmentValueChaged:) forControlEvents:UIControlEventValueChanged];
       [[self navigationItem] setTitleView:self.segment];
    }
    

    You also have to store the value when the controller is about to disappear.

    - (void)viewWillDisappear:(BOOL)animated {
        self->_currentIndex = self.segment.selectedSegmentIndex;
    }
    

    Where you defined NSInteger _currentIndex; in your interface

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

Sidebar

Related Questions

I have a controller class as in the following code segment: @Controller public class
In my Map application I have segment controller on main screen and using that
I have a tableview with a segmentedControl in the header, i want the segment
I have a segment controller on one of my views and now on the
I have a segment control added to the navigation bar. It works fine. When
i have Segment class who contain 2 sections , each section contain 2 point
i have used segment control for distance measuring in km or Miles so if
I have a code segment that generates a dynamic 3D array of random numbers
I have a Path and when user click on a segment I have to
I have two array and three button in segment control i want that when

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.