nabiullinas
nabiullinas

Reputation: 1245

Can not attach event to segment control

Good day. I try to add event to segment control, but it always take me an exeption. I create a property in class

#import <UIKit/UIKit.h>

@interface Events : UINavigationController{
    UISegmentedControl *segmentedControl;
}
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
-(void)changeSegment:(id)sender;
@end

And in .m file I add a subview

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
         self.segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                 [NSString stringWithString:NSLocalizedString(@"Трансляции", @"")],
                                                 [NSString stringWithString:NSLocalizedString(@"Мафия", @"")],

                                                 nil]];
        segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl setSelectedSegmentIndex:0];



        //[segmentedControl setFrame:[self.navigationBar bounds]];
        CGRect rect = [self.navigationBar bounds];
        rect.size.width = rect.size.width - 40;
        rect.size.height = rect.size.height - 15;
        rect.origin.y =  rect.origin.y + 7;
        rect.origin.x =  rect.origin.x + 17;
        [segmentedControl setFrame:rect];

        [self.navigationBar addSubview:segmentedControl];

    }
    return self;
}

Then In ViewDidLoad I try to add event

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.segmentedControl addTarget:self action:@selector(changeSegment:) 
               forControlEvents:UIControlEventValueChanged];

}

And the description of events

-(void)changeSegment{
    if(segmentedControl.selectedSegmentIndex == 0){

    }
    if(segmentedControl.selectedSegmentIndex == 1){

    }
}

But it never enter to this function. And give me error [Events changeSegment:]: unrecognized selector sent to instance 0x6833bf0

Upvotes: 0

Views: 443

Answers (2)

EmilioPelaez
EmilioPelaez

Reputation: 19882

You declared -(void)changeSegment:(id)sender and added the selector @selector(changeSegment:) to your segment control, however, you implemented -(void)changeSegment or @selector(changeSegment), notice the missing :.

Just change this:

-(void)changeSegment{
    if(segmentedControl.selectedSegmentIndex == 0){

    }
    if(segmentedControl.selectedSegmentIndex == 1){

    }
}

To this:

-(void)changeSegment:(id)sender{
    if(segmentedControl.selectedSegmentIndex == 0){

    }
    if(segmentedControl.selectedSegmentIndex == 1){

    }
}

Upvotes: 1

Saad
Saad

Reputation: 8947

on which u'r attatching the event. is it valueChanged??

i think u'r trying it on touchUpInside

Upvotes: 1

Related Questions