Facebook教程添加注销按钮但空白屏幕

时间:2012-06-15 01:04:52

标签: ios logout facebook-ios-sdk

我对目标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];
}
}

1 个答案:

答案 0 :(得分:0)

如果您在logoutButton上使用IB,请检查是否已在界面构建器中将其链接到对象(xib)。如果错误仍然存​​在,请告诉我。

[编辑]

 [self.viewController.view addSubview:logoutButton];

不起作用。你正在使用故事板,对吧?生成的初始值通常不包含属性中的“viewController”。你只能看到“窗口”。没有viewController的实例可能是导致错误的原因。

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

顺便说一句,请显示该Facebook教程的链接,以便我向您解释如何实施。