'AppDelegate'没有可见的@interface声明选择器'displayMessage'

时间:2014-02-04 23:13:21

标签: ios objective-c

我正在按照本书的教程进行操作:

http://books.google.co.uk/books?id=6WtVAQAAQBAJ&pg=PA569&lpg=PA569&dq=%5Bself+displayMessage:+kDenied%5D&source=bl&ots=PSj5Nu_QyI&sig=VHOeJtYHARgMOqSEdREyCtcgxc8&hl=en&sa=X&ei=b2rxUv-iN4WQhQfwxIGYDQ&ved=0CC0Q6AEwAA#v=onepage&q=%5Bself%20displayMessage%3A%20kDenied%5D&f=false

但是,当我将此代码放入AppDelegate.m时,我收到错误消息“没有可见@interface for'AppDelegate'声明选择器'displayMessage'”,但是我正在按照代码编写它。

CFErrorRef错误= NULL;

switch (ABAddressBookGetAuthorizationStatus()) {
    case kABAuthorizationStatusAuthorized:{
        addressBook = ABAddressBookCreateWithOptions(NULL, &error);
        [ self useAddressBook:addressBook];
        // Do you work and once your finished..
        if (addressBook != NULL) {
            CFRelease(addressBook);
        }
        break;
    }
    case kABAuthorizationStatusDenied:{
        [self displayMessage: kDenied];
        break;
    }
    case kABAuthorizationStatusNotDetermined:{
        addressBook = ABAddressBookCreateWithOptions(NULL, &error);
        ABAddressBookRequestAccessWithCompletion
        (addressBook, ^(BOOL granted, CFErrorRef error)
        {
            if (granted){
                NSLog(@"Access was granted");
                [self useAddressBook:addressBook];
            } else {
                NSLog(@"Access was not granted");
            }
            if (addressBook != NULL){
                CFRelease(addressBook);
            }
        });
        break;
    }
    case kABAuthorizationStatusRestricted:{
        [self displayMessage: kRestricted];
        break;
    }
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

}

2 个答案:

答案 0 :(得分:2)

您正在调用以下实例方法:

[self displayMessage: kRestricted];

未在UIApplication(AppDelegate)的子类中声明。

要解决此问题,只需实施displayMessage:方法:

- (void)displayMessage:(id)message
{
    // do stuff here
}

答案 1 :(得分:0)

该消息告诉您正在调用方法displayMessage:并且您尚未定义它。

你的书在第13.1节中使用了这种方法,但在该部分中没有对它的定义,但是看起来合适的定义可以在第19.1节中找到。该定义在警报中显示一个字符串。

HTH

<强>附录

本书中所有示例的完整来源均适用于download from GitHub。作者对您遗失方法的定义是:

- (void) displayMessage:(NSString *)paramMessage
{
    [[[UIAlertView alloc] initWithTitle:nil
                                message:paramMessage
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}

您可能希望下载完整存档,因为看起来其他示例也可能不完整。