using segmented controller

Discussion in 'iOS Programming' started by pamelaraj, May 27, 2009.

  1. pamelaraj macrumors newbie

    Joined:
    Apr 8, 2009
    #1
    I have added a segment controller in the navigation bar. The segment controller has 3 segments. The first one is "My Schedule", "Others Schedule", "Options".
    So when the app is loaded the "My Schedule" is selected by default. It shows a tableview with 10 cells. When any one of the cell is clicked, it navigates to a second page with the detailed information of the schedule.

    Similarly, when the second segment is clicked, it should show the "Others schedule" with a similar navigation view.

    Could anyone help me understand or provide an example to connect the segmented controller to a table view..?
     
  2. johnnyjibbs macrumors 68030

    johnnyjibbs

    Joined:
    Sep 18, 2003
    Location:
    London, UK
    #2
    It sounds like you want tab bar functionality. However, you can create the same functionality with UISegmentedControl. Like all the other UIControl elements (like UIButton or UISwitch), it uses the target: action: forEvent: method of UIControl to respond to user actions.

    Therefore when you set up your segmented control, you will set up a method that is called every time the user selects one:

    Code:
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] init];
    // other configuration of control
    
    [segment addTarget:self action:@selector(segmentedControlWasTapped:) forEvent:UIControlEventValueChanged];
    
    Code:
    - (void)segmentedControlWasTapped:(id)sender {
       // then use a switch statement or series of if statements to determine which selectedSegmentIndex was touched to control the views
       if (selectedSegmentIndex == 0) {
          // display view controller 1, etc
       }
    }
    
     
  3. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #3
    I think u must have written action method to be called on taping segment.In that method u can just check the selected segmen Index.For "My shedule" it will be 0,"Others Schedule" it will be 1,"Options" it will be 2.
    So in action method you Can just call reloadData method on the tableView you are using.
     
  4. pamelaraj thread starter macrumors newbie

    Joined:
    Apr 8, 2009
    #4
    Hi Johnny, Sujit,

    Thanks for explaining me the details about the segmented control. Thanks for the piece of code Johnny.

    I will also try to use the tab bar controller functionality. Initially, I thought that the tab bar functionality was only for the options positioned on the bottom part of view. But if I can use it to create options on the top part of the view; its great to know that.

    Tc,
    -P
     
  5. johnnyjibbs macrumors 68030

    johnnyjibbs

    Joined:
    Sep 18, 2003
    Location:
    London, UK
    #5
    Well, technically, you can position a tab bar at any position on the screen. But I have only ever seen at the bottom, and I would image that this is where Apple would expect it to be. But I could be wrong.

    I was suggesting that if you use a tab bar you should have it at the bottom of the screen. But if that interferes with a toolbar that you might have, then the segmented control at the top would be fine instead.
     

Share This Page