0

再会。我尝试将事件添加到段控制,但它总是让我感到厌烦。我在课堂上创建了一个属性

#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

4

2 回答 2

1

您声明-(void)changeSegment:(id)sender了选择器并将其添加@selector(changeSegment:)到您的段控件中,但是,您实现了-(void)changeSegmentor @selector(changeSegment),请注意缺少 :。

只需更改此:

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

    }
    if(segmentedControl.selectedSegmentIndex == 1){

    }
}

对此:

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

    }
    if(segmentedControl.selectedSegmentIndex == 1){

    }
}
于 2012-05-05T15:38:23.373 回答
1

您正在参加活动。是价值改变了吗?

我想你在 touchUpInside 上试试

于 2012-05-05T15:28:49.417 回答