Search code examples
objective-cuisegmentedcontroluicontainerviewchildviewcontroller

How can I send selectedSegmentIndex to containerView in Objective-c?


I have a containerView into my viewController. My viewController has a UISegmentedControl.

How can I send this selectedSegmentIndex selected at this moment and everytime when I change this?

I', trying with a property in the next class with prepareForSegue but this only send on first load.. Thanks!

Then Edit I'm getting: -[ViewController containerViewDidChangeSegmentIndex:]: unrecognized selector sent to instance 0x7f8db16286d0

FirstViewController (This contains A select and a containerView)

ViewController.h

    @interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UISegmentedControl *select;
@property (weak, nonatomic) IBOutlet UIView *container;

@property(nonatomic, assign) ContainerViewController * classLevelReference;

@end

ViewController.m

    @interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     [_select addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
}

- (void)segmentChanged:(UISegmentedControl *)segment{
    //since you've reference to your container view here, you can directly call its method here:
    [self.classLevelReference containerViewDidChangeSegmentIndex:segment.selectedSegmentIndex];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    //don't forget to check the segue id, if you've multiple segues
    ContainerViewController *containerView = [segue destinationViewController];
    self.classLevelReference = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ContainerViewController.h

@interface ContainerViewController : UIViewController

- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex;

@end

ContainerView.m

@implementation ContainerViewController

- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex{
    //Do whatever you want with your updated index
    NSLog(@"changing");
}

@end

Solution

  • You might want to make a method in your ContainerViewController.h like this:

    @interface ContainerViewController:UIViewController
    //.....other implementation here.
    - (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex;
    @end
    

    Now implement this method in your ContainerViewController.m like this:

    - (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex{
        //Do whatever you want with your updated index
    }
    

    Now in your prepare for segue, save the reference to your ContainerViewController in a class-level variable :

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        //don't forget to check the segue id, if you've multiple segues
        ContainerViewController *containerView = [segue destinationViewController];
        self.classLevelReference = containerView;
    }
    

    Lastly in the FirstViewController.m, tell the container view when segment index changes.

    - (void)viewDidLoad{
       //.....your other implementation here.....
    
       //add a listener to your segment's value changed action
       [youregements addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];  
    }
    
    - (void)segmentChanged:(UISegmentedControl *)segment{
       //since you've reference to your container view here, you can directly call its method here:
       [self.classLevelReference containerViewDidChangeSegmentIndex:segment.selectedSegmentIndex];
    }
    

    Important: you might've different names for the ContainerViewController and FirstViewController in your case, just apply the changes carefully.

    Happy coding!