2012-05-05 46 views
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){ 

    } 
} 

但它永遠不會ter這個函數。並給我錯誤[事件changeSegment:]:無法識別的選擇器發送到實例0x6833bf0

回答

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){ 

    } 
} 
1

其上u'r attatching事件。這是valueChanged?

我覺得u'r想它touchUpInside

+0

是的,這是一個感謝的問題。 – nabiullinas

+0

歡迎你,親愛的 – Saad