2013-03-10 31 views
2

我正在選擇一個Facebook朋友。我想在啓動facebook朋友選擇器的文本框中顯示Facebook朋友的名字。傳遞Facebook朋友名稱到文本字段中的問題

謝謝!

另外,是否有區別抓住用戶的字符串名稱和選擇朋友發送對象?因爲這就是用戶將要做的事情。所以,我需要顯示用戶名的字符串值,但我也必須將一個對象與該用戶關聯起來。

-(IBAction)cancelList; 
-(IBAction)submitList; 
-(IBAction)datePicker; 

@property (strong, nonatomic) IBOutlet UITextField *listFieldText; 
@property (strong, nonatomic) IBOutlet UITextField *dateFieldText; 
@property (strong, nonatomic) IBOutlet UITextField *wallPostText; 
@property (strong, nonatomic) IBOutlet UITextField *friendsName; 
@property (retain, nonatomic) UIDatePicker *pick; 
@property (strong, nonatomic) UIImage *profilePic; 

@property (retain, nonatomic) PF_FBFriendPickerViewController *friendPickerController; 

實現:

- (void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    sender.delegate = self; 
    if([sender isEqual:dateFieldText]) 
    { 
     UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
             initWithTitle:@"Save Date" 
             style:UIBarButtonItemStyleDone 
             target:self 
             action:@selector(datePicker)]; 
     self.navigationItem.rightBarButtonItem = doneButton; 
     pick = [[UIDatePicker alloc] init]; 
     [pick setFrame:CGRectMake(0,200,320,120)]; 
     //[pick addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged]; 
     dateFieldText.delegate = self; 
     dateFieldText.inputView = pick; 
    } 
    else if ([sender isEqual:friendsName]) 
    { 
     NSLog(@"Pick a friend!"); 

     if (self.friendPickerController == nil) { 
      // Create friend picker, and get data loaded into it. 
      self.friendPickerController = [[PF_FBFriendPickerViewController alloc] init]; 
      self.friendPickerController.title = @"Select Friends"; 
      self.friendPickerController.delegate = self; 
      self.friendPickerController.allowsMultipleSelection = NO; 
     } 
     [self.friendPickerController loadData]; 
     [self.friendPickerController clearSelection]; 
     [self presentModalViewController:self.friendPickerController animated:YES]; 

    } 
    else{ 
     UIBarButtonItem *submitButton = [[UIBarButtonItem alloc] 
             initWithTitle:@"Done" 
             style:UIBarButtonItemStyleDone 
             target:self 
             action:@selector(submitList)]; //change this 


      self.navigationItem.rightBarButtonItem = submitButton; 

     } 
    } 

-(void)updateFriendTextField:(NSString*)subtitle 
{ 
    friendsName.text = subtitle; 
} 

/* 
-(void)updateSelections 
{ 
    NSString* friendsSubtitle = @"Selected friends"; 
    id<PF_FBGraphUser> friend = [self.selectedFriends objectAtIndex:0]; 
    friendsSubtitle = friend.name; 
    [self updateFriendTextField:friendsSubtitle]; 

    //[self handlePickerDone]; 
} 
*/ 

-(void)friendPickerViewControllerDataDidChange:(PF_FBFriendPickerViewController *)friendPicker { 
    NSLog(@"Current friend selections: %@", friendPicker.selection); 

} 

- (void)dealloc 
{ 
    friendPickerController.delegate = nil; 
} 

- (void)facebookViewControllerCancelWasPressed:(id)sender 
{ 
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 
    NSLog(@"Friend selection cancelled."); 
    //[self handlePickerDone]; 
} 

- (void)facebookViewControllerDoneWasPressed:(id)sender 
{ 

    NSLog(@"Done was pressed."); 
    //[self updateSelections]; 
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

可以簡化你的問題?它不清楚你的要求是什麼。 – Vijay 2013-03-13 06:52:08

+0

基本上我可以啓動Facebook的朋友選擇器,並選擇朋友,但我不知道如何將數據[如用戶名等]傳遞到另一個視圖。朋友選取器從桌面視圖模式呈現。 – STANGMMX 2013-03-13 11:18:19

+0

對於與IDE無關的問題,請勿使用[tag:xcode]標記。 – Undo 2013-03-14 00:28:09

回答

0

您可以將其從friendPicker.selection保存在您的某個代理方法(friendPickerViewControllerSelectionDidChangefriendPickerViewControllerDataDidChange)中。

數組中的項目是PF_FBGraphUser對象,所以無論你從PF_FBGraphUser需要你可以將它們保存和使用:

@property (retain, nonatomic) NSString *id; 
@property (retain, nonatomic) NSString *name; 
@property (retain, nonatomic) NSString *first_name; 
@property (retain, nonatomic) NSString *middle_name; 
@property (retain, nonatomic) NSString *last_name; 
@property (retain, nonatomic) NSString *link; 
@property (retain, nonatomic) NSString *username; 
@property (retain, nonatomic) NSString *birthday; 
@property (retain, nonatomic) id<PF_FBGraphPlace> location; 
0

對於這個答案多種解決方案。

就我個人而言,我喜歡使用全局標題,因爲它從我的角度來看是最高效的。

我將創建一個頭文件,並將其命名爲「global.h」

在該頭文件,我將創建以下文件:

NSString* friendsName;
NSString* wallPostText;
NSString* dateFieldText;
NSString* listFieldText;

接下來,我將在初始化這些對象應用程序的委託時,應用程序首先通過簡單地調用friendsName = @"friend";

後來,當你真正從選擇收到朋友的名字,保存名字到是全局變量,frien剛剛推出DSNAME!

記得#import「global.h」到需要訪問這些變量的所有類中!

第二種方法是在你希望訪問這些變量的類中創建一個局部變量。

@property(nonatomic) NSString *friendsName;

當你正在推動新的UIViewController,可以通過正確的初始化後通過它像這樣:

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.friendsName = "Mark";
[self pushViewController:viewControllerB animated:YES];

希望這有助於!

+0

我不確定我完全理解。選擇是作爲一個陣列出來的。另外,請記住我提供了一個模型視圖控制器,用於將項目添加到列表中,並且此模型視圖控制器具有幾個不同的文本框。其中一個有啓動朋友選擇器的friendsName文本框。我做了一個選擇,點擊完成,它會取消朋友選取器和原始模型視圖控制器,然後轉到起始表視圖。我需要朋友選擇器以朋友的名字填充第一個模式視圖控制器的文本框。 – STANGMMX 2013-03-15 01:46:23