2011-03-28 93 views
0

在实现文件中出现错误(“switchChanged”未声明),但找不到问题。你可以帮我吗?需要帮助调试switchChanged方法

TIA

ViewController.m

#import "Control_FunViewController.h" 



@implementation Control_FunViewController 

@synthesize nameField; 
@synthesize numberField; 
@synthesize sliderLabel; 
@synthesize leftSwitch; 
@synthesize rightSwitch; 
@synthesize doSomethingButton; 

-(IBAction)sliderChanged:(id)sender 
{ 
    UISlider *slider = (UISlider *)sender; 
    int progressAsInt = (int)(slider.value + 0.5f); 
    NSString *newText = [[NSString alloc] initWithFormat:@"%d",progressAsInt]; 
    sliderLabel.text = newText; 
    [newText release]; 
} 

-(IBAction)textFieldDoneEditing:(id)sender 
{ 
    [sender resignFirstResponder]; 
} 

-(IBAction)backgroundTap:(id)sender 
{ 
    [nameField resignFirstResponder]; 
    [numberField resignFirstResponder]; 
} 

-(IBAction)toggleControls:(id)sender 
{ 
    if ([sender selectedSegmentIndex] == kSwitchesSegmentIndex) 
    { 
     leftSwitch.hidden = NO; 
     rightSwitch.hidden = NO; 
     doSomethingButton.hidden = YES; 
    } 
    else { 
     leftSwitch.hidden =YES; 
     rightSwitch.hidden =YES; 
     doSomethingButton.hidden = NO; 
    } 
-(IBAction)switchChanged:(id)sender 
    { 
     UISwitch *whichSwitch = (UISwitch *)sender; 
     BOOL setting = whichSwitch.isOn; 
     [leftSwitch setOn:setting animated:YES]; 
     [rightSwitch setOn:setting animated:YES]; 
    } 
-(IBAction)buttonPressed 
    { 
     //TODO: Implement Action Sheet and Alert 
    } 
} 





/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 


/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 


/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [nameField release]; 
    [numberField release]; 
    [sliderLabel release]; 
    [leftSwitch release]; 
    [rightSwitch release]; 
    [doSomethingButton release]; 
    [super dealloc]; 
} 

@end 

ViewController.h

#import <UIKit/UIKit.h> 

#define kSwitchesSegmentIndex 0 

@interface Control_FunViewController : UIViewController { 
    UITextField *nameField; 
    UITextField *numberField; 
    UILabel *sliderLabel; 
    UISwitch *leftSwitch; 
    UISwitch *rightSwitch; 
    UIButton *doSomethingButton; 
} 

@property(nonatomic,retain)IBOutlet UITextField *nameField; 
@property(nonatomic,retain)IBOutlet UITextField *numberField; 
@property(nonatomic,retain)IBOutlet UILabel *sliderLabel; 
@property(nonatomic,retain)IBOutlet UISwitch *leftSwitch; 
@property(nonatomic,retain)IBOutlet UISwitch *rightSwitch; 
@property(nonatomic,retain)IBOutlet UIButton *doSomethingButton; 

-(IBAction)textFieldDoneEditing:(id)sender; 
-(IBAction)backgroundTap:(id)sender; 
-(IBAction)sliderChanged:(id)sender; 
-(IBAction)toggleControls:(id)sender; 
-(IBAction)switchChanged:(id)sender; 
-(IBAction)buttonPressed; 


@end 

回答

0

冒号(:)是方法的名称的一部分,但是你有没有包括它在错误信息中。这可能是你忘记了,但是如果你从某处调用了-switchChanged(无冒号),或者如果你使用动作-switchChanged(无冒号)连接了一个控件,那就是问题所在。也许你稍后添加冒号和发送者参数?

+0

冒号不在错误信息中;那不是我的错误,而是把它留下。这是Xcode返回给我的。据我所知,我已将所有冒号包含在switchChanged:方法中,并且我也将它包含在头文件中,所以我找不出错误在哪里。 – pdenlinger 2011-03-28 21:51:37

+0

这是一个编译时错误,对吗?如果它在运行时发生,您会收到关于无法识别的选择器的错误。如果是这种情况,编译器是否会指出问题发生的地方? – Caleb 2011-03-29 01:55:52

+0

发现问题。上面的方法(toggleControls)缺少一个右括号,导致了问题。一旦解决了,所有编译好的。 – pdenlinger 2011-03-30 18:07:18