2 developerwab developerWab 于 2013.02.19 16:15 提问

ios文件问题-unrecognized selector

先看代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    textBox = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 40)];
    [textBox setDelegate:self];
    [self.view addSubview:textBox];
    [textBox release];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(saveText) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Save" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];


    NSFileManager *filemgr;
    NSString *dataFile;
    NSString *docsDir;
    NSArray *dirPaths;

    filemgr = [NSFileManager defaultManager];

    // Identify the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSLog(@"PATH--->  %@",dirPaths);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the data file
    dataFile = [docsDir stringByAppendingPathComponent: @"datafile.dat"];

    // Check if the file already exists
    if ([filemgr fileExistsAtPath: dataFile])
    {
        // Read file contents and display in textBox
        NSData *databuffer;
        databuffer = [filemgr contentsAtPath: dataFile];

        NSString *datastring = [[NSString alloc] initWithData: databuffer encoding:NSASCIIStringEncoding];

        textBox.text = datastring;
        [datastring release];
    }

    [filemgr release];
    [super viewDidLoad];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder]; 
    return YES; 
}

- (void)saveText:(id)sender
{ 
    NSLog(@"Text saved");
}

在每次执行的时候获取错误:

"2013-02-18 12:43:46.194 FileExample[1475:207] -[FileExampleViewController saveText]: unrecognized selector sent to instance 0x682e730"

1个回答

lianlianbushell
lianlianbushell   2013.02.19 16:52
已采纳

改一下这行代码:

[button addTarget:self action:@selector(saveText) forControlEvents:UIControlEventTouchUpInside];

改为:

 [button addTarget:self action:@selector(saveText:) forControlEvents:UIControlEventTouchUpInside];

或者这样也行:

- (void)saveText // removed  " :(id)sender "
 { 
    NSLog(@"Text saved");
 }
Csdn user default icon
上传中...
上传图片
插入图片