文件ios问题 - 无法识别的选择器

时间:2013-02-18 07:15:50

标签: iphone ios file 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]:无法识别的选择器发送到实例0x682e730”

我是iOS的新手,无法识别或理解或推断此错误。

1 个答案:

答案 0 :(得分:0)

更改此行

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

用这个

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

OR

这样做

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