1

So I have a button that is attached to an action called writeToFile:

The action opens an NSPanel and allows the user to enter the name of the file they want to write to their desktop.

The problem I am having is I can't update the user and let them know that the write was successful before the NSPanel closes.

So I put a label or NSTextField because I am talking about a mac app inside the NSPanel and the NSTextField updates and tells the user that the write was a success.

I change the button title that is attached to the action writeToFile: to "close" and I am trying to change the method the button actually calls by using performSelector:

However, I keep getting an "Unrecognized selector"

Here is some code, any and all help is greatly appreciated:

- (void) closePanel {
       [theSheet close];
}

- (IBAction) writeToFile: (id)sender
{

    if ([_nameOfFile.stringValue length] > 0) {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
        NSString *path = [[paths objectAtIndex:0]stringByAppendingPathComponent:_nameOfFile.stringValue];
        NSString *string = _inputTextView.string;
        BOOL OK = [string writeToFile:path
                       atomically:YES
                         encoding:NSUTF8StringEncoding
                            error:NULL];

        if (OK) {

            _statusField.stringValue = @"File written to desktop";
            _writeButton.title = @"close";
            [_writeButton performSelector:@selector(closePanel)];

        } else {

            _statusField.stringValue = @"Sorry, something went wrong :(";

        }

    } else {

        _statusField.stringValue = @"Please name the file first!";

    }
}
4

3 回答 3

1

那是因为_writeButton没有实现-closePanel. 你的课有。将其更改为:

[ self closePanel ];
于 2012-10-19T20:31:24.190 回答
0

你设置代理了吗?

[_writeButton setTarget:self];
[_writeButton setAction:@selector(writeToFile:)];
于 2012-10-26T09:36:58.550 回答
0

换行:

[_writeButton performSelector:@selector(closePanel)];

为了:

[_writeButton addTarget:self action:@selector(closePanel)forControlEvents:UIControlEventTouchUpInside];
于 2012-10-19T20:37:11.170 回答