我對通常的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