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
You declared
-(void)changeSegment:(id)senderand added the selector@selector(changeSegment:)to your segment control, however, you implemented-(void)changeSegmentor@selector(changeSegment), notice the missing :.Just change this:
To this: