【问题标题】:Facebook tutorial add logout button but blank screenFacebook教程添加注销按钮但空白屏幕
【发布时间】:2012-06-18 02:15:48
【问题描述】:

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

【问题讨论】:

    标签: ios logout facebook-ios-sdk


    【解决方案1】:

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

    [编辑]

     [self.viewController.view addSubview:logoutButton];
    

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

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

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

    【讨论】:

    • 我刚刚使用故事板创建了一个新项目。我没有更改 viewcontroller.h 和 .m 文件中的代码。我需要在那里做任何事情吗?或者我应该在 appDelegate 文件中添加更多内容吗?非常感谢您的帮助!
    • 您是否在新项目中集成了 Facebook api?在包含 Facebook API 之前和之后运行您的项目,并且没有注销按钮。无法识别的选择器通常是由以下原因引起的: 1. 错误的对象实例(例如控制器) 2. 未调用您的方法。可能是您正在使用 IBAction 并且无法将其链接到您的对象。也可能是您正在调用未声明的方法或对象。
    • 我已经包含了 api 并且在注销按钮步骤之前一切正常。我也可以看到授权页面。我只是按照教程中的步骤创建项目。在退出按钮步骤之前我还需要做其他事情吗?
    • 我只是添加了我在 appDelegate 文件中添加的所有代码。我认为它是动态创建的。我没有在情节提要中添加任何按钮。
    • 这是教程。我把所有东西都移到了控制器文件中。授权部分没问题。但是注销按钮仍然没有出现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2012-03-04
    • 2012-05-22
    • 2012-05-04
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多