美好的一天。我尝试将事件添加到段控件,但它总是带我一个例外。我在类
中创建了一个属性#import <UIKit/UIKit.h>
@interface Events : UINavigationController{
UISegmentedControl *segmentedControl;
}
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
-(void)changeSegment:(id)sender;
@end
在.m文件中我添加了一个子视图
- (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;
}
然后在ViewDidLoad中我尝试添加事件
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.segmentedControl addTarget:self action:@selector(changeSegment:)
forControlEvents:UIControlEventValueChanged];
}
事件描述
-(void)changeSegment{
if(segmentedControl.selectedSegmentIndex == 0){
}
if(segmentedControl.selectedSegmentIndex == 1){
}
}
但它永远不会进入这个功能。并给我错误[事件changeSegment:]:无法识别的选择器发送到实例0x6833bf0
答案 0 :(得分:1)
你在哪个地方举办活动。是值改变吗?
我想你会在touchUpInside上尝试它
答案 1 :(得分:1)
您宣布-(void)changeSegment:(id)sender
并将选择器@selector(changeSegment:)
添加到您的细分受众群控件中,但是,您实施了-(void)changeSegment
或@selector(changeSegment)
,请注意缺失:。
只需改变一下:
-(void)changeSegment{
if(segmentedControl.selectedSegmentIndex == 0){
}
if(segmentedControl.selectedSegmentIndex == 1){
}
}
对此:
-(void)changeSegment:(id)sender{
if(segmentedControl.selectedSegmentIndex == 0){
}
if(segmentedControl.selectedSegmentIndex == 1){
}
}