-1

我对目标 C 很陌生。我遵循了将 Facebook 集成到 iOS 应用程序的教程,没有做任何其他事情。在我添加了注销按钮的代码后,我运行了它,它给了我一个错误: [self.viewController.view addSubview:logoutButton]; 错误是“无法识别的选择器发送到实例 0x6b6c550”。我知道这可能是一个愚蠢的错误,但如果有人能指出我错在哪里,我将不胜感激!

static NSString* kAppId = @"340105106048288";


// Method that gets called when the sign out button is clicked
- (void)logoutButtonClicked {
    [facebook logout];
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
   (NSDictionary *)launchOptions
{

UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logoutButton.frame = CGRectMake(40, 40, 200, 40);
[logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(logoutButtonClicked)
       forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:logoutButton];


facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults
    objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
// This part, the authorize method will bring you to the authorization page
if (![facebook isSessionValid])
    [facebook authorize:nil];


    return YES;
}



// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url];
}

// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application 
        openURL:(NSURL *)url 
  sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation {
    return [facebook handleOpenURL:url];
}

// Save the user credential, specifically the access token and the expiration date to the user defaults
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}


- (void) fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]) {
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}
}
4

1 回答 1

0

如果您在 logoutButton 上使用 IB,请检查您是否已将其链接到界面构建器中的对象 (xib)。让我知道错误是否仍然发生。

[编辑]

 [self.viewController.view addSubview:logoutButton];

不会工作。您正在使用情节提要,对吗?生成的初始值通常不包括属性中的“viewController”。您只能看到“窗口”。没有可能导致错误的 viewController 实例。

一条建议,你为什么不在 ViewController 中实现整个事情并仅在必要时使用委托呢。

顺便说一下,请显示那个 Facebook 教程的链接,这样我就可以向你解释它应该如何实现。

于 2012-06-15T02:22:31.913 回答