我一直在网上看看我是否需要将委托放在profileViewController(这是信息的视图)或者我需要登录/注册viewControllers中的委托我不知道如何通过信息profilePicture,名称,登录状态请帮忙,
EDIT ::
我能通过我的协议传递user.name但我无法通过FBProfilePictureView
ViewController.h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
@protocol passNames <NSObject>
- (void)setFBName: (NSString *)FBName;
- (void)setFBProfilePicture: (id<FBGraphUser>)FBPicture;
@end
@interface ViewController : UIViewController <FBLoginViewDelegate>
@property (weak) id <passNames> delegate;
@property (strong, nonatomic) NSString *FBNamePass;
@property (strong, nonatomic) FBProfilePictureView *FBProfilePicture;
@end
ViewController.m
#import "ViewController.h"
#import "NeXtViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePictureView;
@end
@implementation ViewController
@synthesize delegate, FBNamePass, FBProfilePicture;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a FBLoginView to log the user in with basic, email and likes permissions
// you should always ask for basic permissions when loggin the user in
FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions:@[@"basic_info",@"email",@"user_likes"]];
// set this loginUIViewCOntroller to be the loginView button's delegate
loginView.delegate = self;
// align the button in the center horizontally
loginView.frame = CGRectMake(25, 299, 271, 50);
// add the button to the view
[self.view addSubview:loginView];
//[self pushViewController];
}
-(void)viewDidAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
self.nameLabel.text = user.name;
//self.profilePictureView.profileID = user.id;
// FBProfilePicture.profileID = user.id;
// NSLog(@"%@ FB", FBProfilePicture.class);
// FBNamePass = user.name;
// NSLog(@"%@ user", FBNamePass);
[self pushViewController:user.name andProfilePicture:user];
}
- (BOOL)isUser:(id<FBGraphUser>)firstUser equalToUser:(id<FBGraphUser>)secondUser {
return
[firstUser.id isEqual:secondUser.id] &&
[firstUser.name isEqual:secondUser.name] &&
[firstUser.first_name isEqual:secondUser.first_name] &&
[firstUser.middle_name isEqual:secondUser.middle_name] &&
[firstUser.last_name isEqual:secondUser.last_name];
}
// implement the loginViewShowingLoggedInUser: delegate method to modify your app's UI for a logged-in user experoience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
self.statusLabel.text = @"Logged in";
if ([self.statusLabel.text isEqualToString:@"Logged in"]) {
//NeXtViewController *n = [self.storyboard instantiateViewControllerWithIdentifier:@"NeXt"];
//[self.navigationController pushViewController:n animated:NO];
}
}
// implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experoience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
self.profilePictureView.profileID = nil;
self.nameLabel.text = @"";
self.statusLabel.text = @"You're not logged in";
}
// You need to override loginView:handleError in order to handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
{
NSString *alertMessage, *alertTitle;
// If the user should perform an action outside of you app to recover,
// the SDK will provide a message for the user, you just need to surface it.
// This conveniently handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertMessage = [FBErrorUtility userMessageForError:error];
alertTitle = @"Facebook Error";
// This code will handle session closures since that happen outside of the app.
// You can take a look at our error handling guide to know more about it
// https://developers.facebook.com/docs/ios/errors
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = @"Session Error";
alertMessage = @"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login");
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
// https://developers.facebook.com/docs/ios/errors
} else {
alertTitle = @"Something went wrong";
alertMessage = @"Please try again later";
NSLog(@"Unexpected error:%@",error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
}
}
- (void)pushViewController:(NSString *)user andProfilePicture:(id<FBGraphUser>)profilePicture
{
NeXtViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"NeXt"];
[controller setFBNameString:user];
//controller.profilePicture = profilePicture;
[controller setFBProfilePicture:profilePicture];
NSLog(@"%@",profilePicture);
[self.navigationController pushViewController:controller animated:NO];
}
@end
NeXtViewController.h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "ViewController.h"
@interface NeXtViewController : UIViewController <passNames>
{
ViewController *view;
}
@property (strong,nonatomic) NSString *FBNameString;
@property (strong, nonatomic) FBProfilePictureView *profilePicture;
@property (weak, nonatomic) IBOutlet FBProfilePictureView *FBProfilePictureView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
- (IBAction)FBLogOut:(id)sender;
@end
NeXtViewController.m
#import "NeXtViewController.h"
@interface NeXtViewController ()
@end
@implementation NeXtViewController
@synthesize FBNameString = _FBNameString, profilePicture = _profilePicture;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
view = [[ViewController alloc] init];
[view setDelegate:self];
self.nameLabel.text = _FBNameString;
//self.FBProfilePictureView.profileID = _profilePicture.profileID;
NSLog(@"%@ %@", self.FBProfilePictureView.class, self.FBNameString.class);
self.navigationItem.hidesBackButton = YES;
}
-(void)viewDidAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setFBName:(NSString *)FBName
{
_FBNameString = FBName;
}
- (void)setFBProfilePicture:(id<FBGraphUser>)FBPicture
{
self.FBProfilePictureView.profileID = FBPicture.id;
}
- (IBAction)FBLogOut:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];
if (FBSessionDidBecomeClosedActiveSessionNotification) {
ViewController *views = [self.storyboard instantiateViewControllerWithIdentifier:@"View"];
[self.navigationController pushViewController:views animated:NO];
views.navigationItem.hidesBackButton = YES;
}
}
@end
答案 0 :(得分:1)
在我看来,你不需要代表(我从你的问题收集的内容)。您可以做的是从“登录/注册viewControllers”获取对“profileViewController”的引用,并将一些信息传递给它们。让我们假设你正在使用故事板和segues。在profileViewController.h中,您将创建一些属性,如:
@interface profileViewController
@property(nonatomic) UIImage* profilePicture;
@property(nonatomic) NSString* name;
@property(nonatomic) NSString* loginStatus;
//all your methods and properties...
@end
然后在您的登录/注册视图控制器中,找到查找profileViewController的方法:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the profile view controller from segue and cast it
profileViewController* profileVC = (profileViewController*)[segue destinationViewController];
// Set the properties in the profile VC Class
[profileVC setProfilePicture:myImage];
[profileVC setName:myName];
[profileVC setLoginStatus:myLoginStatus];
}
然后只要在profileViewController中调用'viewDidLoad'或'init',就应该设置属性并准备使用!它不一定是你正在使用的segue,只是在你使用它之前访问profileViewController的任何方式。
答案 1 :(得分:0)
尝试将数据设置为NSUserDefault,它易于使用,并且使用NSUserDefault很容易在多个类之间共享数据。
<强> AppDelegate.m 强>
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
保存
AppDelegate * degate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
delegate.prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[delegate.prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
// saving an NSInteger
[delegate.prefs setInteger:42 forKey:@"integerKey"];
[delegate.prefs synchronize];
检索
delegate.prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [delegate.prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [delegate.prefs integerForKey:@"integerKey"];
在AppDelegate中定义你的NSUSserDefault,并使用AppDelageate对象以及多个类中的数据进行共享。
试过这个