程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> file-ios文件問題-unrecognized selector

file-ios文件問題-unrecognized selector

編輯:編程綜合問答
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"

最佳回答:


改一下這行代碼:

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

改為:

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

或者這樣也行:

- (void)saveText // removed  " :(id)sender "
 { 
    NSLog(@"Text saved");
 }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved