如何将loginViewFetchedUserInfo:中的信息一次传递给多个视图

时间:2014-07-17 11:39:23

标签: ios facebook login

您好我正在尝试将login.FetchedUserInfo中的user.id和user.name传递给我的menuViewController,profileViewController和settingViewController到目前为止我已将信息发送到profileViewController:

// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
//    self.profilePictureView.profileID = user.id;
//self.FBProfilePicture.profileID = user.id;

_profilePicture = user;
_FBNameString = user.name;

NSLog(@"%@, name from Login", _FBNameString);


[self pushViewController:user.name andProfilePicture:_profilePicture];
}


- (void)pushViewController:(NSString *)userName andProfilePicture:(id<FBGraphUser>)profilePicture
{

//    MenuViewController *menu = [self.storyboard instantiateViewControllerWithIdentifier:@"MenuViewController"];
//    [menu setFBName:userName];
//    [menu setFBProfilePic:profilePicture];
//
//    SettingViewController *setting = [self.storyboard instantiateViewControllerWithIdentifier:@"SettingViewController"];
//    [setting setFBName:userName];
//    [setting setFBProfilePic:profilePicture];

//    NSLog(@"%@",profilePicture);


//    [self.navigationController pushViewController:controller animated:NO];



}

我只能在profileViewController中接收信息,而不是其他我已经使用协议放置了setter和getter但是我无法将它转到另一个viewController

2 个答案:

答案 0 :(得分:0)

您可以使用NSNotification。 例 有一个名为myTestNotificationReceived的方法,它在myClassA中实现。现在我想从另一个类myClassB调用此方法。下面是我如何使用NSNotificationCenter执行此操作的代码。

    @implementation myClassA
- (void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (id) init
{
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(myTestNotificationReceived:) 
    name:@"myTestNotification"
    object:nil];
return self;
}
- (void) myTestNotificationReceived:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"myTestNotification"])
    NSLog (@"Notification is successfully received!");
}
@end

现在我想从myClassB调用myTestNotification方法。

@implementation myClassB
- (void) dealloc{
[super dealloc];
}
- (id) init
{
    self = [super init];
    if (!self) return nil;
    return self;
}
- (void) myMethod
{
// All instances of myClassA will be notified
[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"myTestNotification" 
    object:self];
}
@end

现在,您可以在不同的类中添加尽可能多的观察者。

答案 1 :(得分:0)

一般来说,你的方法应该是这样的。(你可能需要根据要求进行调整)

// this method will be called when the user information has been fetched
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{

//    self.profilePictureView.profileID = user.id;
//self.FBProfilePicture.profileID = user.id;

_profilePicture = user.picture;//assuming you may be having picture property for object user
_FBNameString = user.name;

NSLog(@"%@, name from Login", _FBNameString);

//POST NOTIFICATION with desire object here its "user"
[[NSNotificationCenter defaultCenter] postNotificationName:@"POSTLOGININFO" object:user];

//uncomment if you want this, ...depends on you 
//[self pushViewController:user.name andProfilePicture:_profilePicture];

}

现在更新menuViewController.m,profileViewController.m和settingViewController.m类的init方法,如下所示

-(id) init
{
  self = [super init];
  if (!self) 
   return nil;

  //Your custom code 

 //get registered for POSTLOGININFO notification so that selector method get called when you post notification with name POSTLOGININFO
  [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(gotLoginInfo:) 
          name:@"POSTLOGININFO"
              object:nil];
   return self;
}

将新方法gotLoginInfo:添加到每个menuViewController.m,profileViewController.m和settingViewController.m

-(void)gotLoginInfo:(NSNotification *)notification{

    //Assuming FBGraphUser class have all required properties
    FBGraphUser *user = (FBGraphUser *)[notification object];

   // save user.id and user.name to your class local variable
    NSLog(@"UserID::%@ and username::%@",user.id,user.name);

}

 -(void)dealloc{

      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"POSTLOGININFO" object:nil];


 }

使用完成后删除通知观察者 &#34; [[NSNotificationCenter defaultCenter] removeObserver:self name:@&#34; POSTLOGININFO&#34;对象:无];&#34 ;.在loginViewFetchedUserInfo:get called之前,还要确保你的类menuViewController,profileViewController和settingViewController的有效对象