gpt4 book ai didi

iphone - 无法将事件附加到段控件

转载 作者:行者123 更新时间:2023-11-28 22:57:53 25 4
gpt4 key购买 nike

美好的一天。我尝试将事件添加到段控制,但它总是让我感到异常。我在类中创建一个属性

#import <UIKit/UIKit.h>

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

然后在 .m 文件中添加一个 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;
}

然后在 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

最佳答案

您声明了 -(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){

}
}

关于iphone - 无法将事件附加到段控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10463272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com