我对通常的FB SDK和iOS开发颇为陌生。我试图将FBGraphUser对象传递给应用程序委托,然后在另一个视图中使用它。我的应用程序正确连接到FB,然后在logginview中将FBGraphUser对象分配给appDelegate。现在的问题是,当我去看另一个视图时,它再次检索它。如何将FBGraphUser对象传递给appDelegate
有谁知道为什么不工作?
我在loginViewController.h代码
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
// here we use helper properties of FBGraphUser to dot-through to first_name and
// id properties of the json response from the server; alternatively we could use
// NSDictionary methods such as objectForKey to get values from the my json object
;
// setting the profileID property of the FBProfilePictureView instance
// causes the control to fetch and display the profile picture for the user
_loggedInUser = user;
[self saveFBuserToDB];
_appDelegate.loggedInUser = (id<FBGraphUser>) user;
// NSLog(@"%@",_appDelegate.loggedInUser);
}
直到这里就像一个魅力。现在连接到FB,并从应用程序的委托获取对象时,我做的NSLog,因为它应该....
...当我去到另一个查看用户未设置了
createExercisePopupViewController .H
#import <UIKit/UIKit.h>
#import "myGymAppDelegate.h"
@interface createExercisePopupViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray* routineName;
}
@property (retain,nonatomic) NSArray* routineName;
@property (strong,nonatomic) myGymAppDelegate *appDelegate;
和createExercisePopupViewController.h
#import "createExercisePopupViewController.h"
@interface createExercisePopupViewController()
{
NSManagedObjectContext *context;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_appDelegate=(myGymAppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(@"%@",_appDelegate.loggedInUser);
}
SOLUTION:
虽然@Borinschi伊万给出的答案是有效
我发现在其周围不再使用的应用程序委托retriving的fbgraph用户和使用我的所有其他视图这种方法的另一个工作。这将在彼此视图设置FB用户图表包括[自fbConnect]在viewDidLoad中
- (void) fbConnect {
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
_loggedInUser = user;
}
}];
}
}
有更多的问题与你的方法(appDelegate持有数据...)但首先:是否有可能你的viewDidLoad在用户登录到FB之前被调用 - 因此你的NSLog写入null? – 2013-04-28 19:12:32
只有在用户登录时才会调用createExercisePopupViewController.h中的viewDidLoad,并且在故事板的另一个视图中。我的问题有哪些问题? – 2013-04-28 19:17:50