除非点击了UIButton

时间:2015-06-22 02:54:05

标签: objective-c facebook-graph-api uibutton segue grand-central-dispatch

这可能是一个奇怪的案例,所以如果我的问题或解决方案不清楚,我会提前道歉:

我有一个 LoginViewController ,用于获取用户的Facebook个人资料图片,用户名,电子邮件等。然后我将它转换为 HomeViewController ,显示一些对象,其他项目,以及显示用户个人资料图片的UIView。

奇怪的部分是当我使用UIButton时,我创建的UIImage仅被转移。我似乎无法将图像以任何其他方式发送到 HomeViewController 。我甚至设置了一个GCP,让它试着等待Facebook提供信息 - 仍然没有。下面是我对 LoginViewController.m 的代码。如果有人知道为什么会发生这种情况,我将非常感激。谢谢!

#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>


@interface LoginViewController ()

- (void)toggleHiddenState:(BOOL)shouldHide;

@end

@implementation LoginViewController

@synthesize profPicture;


- (void)viewDidLoad {
    [super viewDidLoad];

    [self toggleHiddenState:YES];
    self.lblLoginStatus.text = @"";

    self.loginButton.readPermissions = @[@"public_profile", @"email"];
    self.loginButton.delegate = self;

    // Do any additional setup after loading the view.
}

-(void)toggleHiddenState:(BOOL)shouldHide{
    self.lblUsername.hidden = shouldHide;
    self.lblEmail.hidden = shouldHide;
    self.profilePicture.hidden = shouldHide;
    self.loggedinwallpaper.hidden = shouldHide;
    self.FBlogout.hidden = shouldHide;
}


-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
    self.lblLoginStatus.text = @"";

    [self toggleHiddenState:NO];
}

-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
    self.lblLoginStatus.text = @"";

    [self toggleHiddenState:YES];
}

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
    NSLog(@"%@", user);
    self.profilePicture.profileID = user.objectID;
    self.lblUsername.text = user.name;
    self.lblEmail.text = [user objectForKey:@"email"];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
        // Create Facebook Profile Picture from User ID url
        NSString *pic_link = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?width=300&height=300", user.objectID];
        NSURL *pic_url = [NSURL URLWithString:pic_link];
        profPicture = [UIImage imageWithData: [NSData dataWithContentsOfURL:pic_url]];

        dispatch_async(dispatch_get_main_queue(), ^(void){
            //Main Thread : UI Updates
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
            [self performSelector:@selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];
            [self presentViewController:homeViewController animated:YES completion:nil];


        });
    });
}

-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
    NSLog(@"%@", [error localizedDescription]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    HomeViewController *homeviewController = segue.destinationViewController;
    homeviewController.homepic = profPicture;
}


@end

2 个答案:

答案 0 :(得分:1)

从您分享的代码中我假设您提到的按钮有效,因为您在故事板中用segue连接了它,对吗? (将箭头拖到下一个VC?)

您的代码存在的问题是

[self performSelector:@selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];

你在没有必要参数的情况下自己触发prepareForSegue:sender: - 你没有准备好准备,因为你甚至没有转向其他视图控制器。因此,执行HomeViewController *homeviewController = segue.destinationViewController;segue为零,因此无法设置图像。

你应该能够通过用以下代码替换在主线程上运行的代码来解决这个问题:

dispatch_async(dispatch_get_main_queue(), ^(void){
    // Main Thread : UI Updates
    [self performSegueWithIdentifier:@"<insert segue identifier here>" sender:self];
});

确保在Storyboard中为视图控制器之间的segue设置名称,并在此处使用该标识符。 (见Apple Docs

答案 1 :(得分:0)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    HomeViewController *homeviewController = segue.destinationViewController;
    homeviewController.homepic = [UIImage alloc] init];
    homeviewController.homepic = profPicture;
}