再会。我尝试将事件添加到段控制,但它总是让我感到厌烦。我在课堂上创建了一个属性
#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){
}
}
但它从来没有进入这个功能。并给我错误 [Events changeSegment:]: unrecognized selector sent to instance 0x6833bf0