2013-03-16 33 views
0

我有一個ViewController提示FBFriendPickerViewController,我在選擇時返回包含選擇的NSArray。現在我想用這個選擇信息提示並顯示一個新的ViewController。我是Objective C的新手,但我想這個解決方案非常簡單。這裏是我的建議:Obj-C全局使用NSArray

ViewController2.h

- (id)initWithStyle:(UITableViewStyle)style andSelection:(NSArray *)selection; 
@property (strong, nonatomic) NSArray *selectedParticipants; 

ViewController2.m

- (id)initWithStyle:(UITableViewStyle)style andSelection:(NSArray *)selection { 
    self = [super initWithStyle:style]; 
    if (self) { 
     self.title = NSLocalizedString(@"Split Bill", nil); 
     self.tableView.backgroundColor = [UIColor wuffBackgroundColor]; 
     self.selectedParticipants = selection; 
    } 
    return self; 
} 

- (void)setSelectedParticipants:(NSArray *)selectedParticipants { 
    NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]); 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%d rowsInSection", [self.selectedParticipants count]); 
    return [self.selectedParticipants count]; 
} 

ViewController1.m然而

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 2) { 
     [[self friendPickerController] presentModallyFromViewController:self animated:YES handler:^(FBViewController *sender, BOOL donePressed) { 
      if (donePressed) { 
       ViewController2 *vc = [[ViewController2 alloc] initWithStyle:UITableViewStyleGrouped 
                       andSelection:[self.friendPickerController selection]]; 
       [self.navigationController pushViewController:vc animated:YES]; 
      } 
      //[[self friendPickerController] clearSelection]; 
      } 
     ]; 
    } 
} 

看來,第一setSelectedParticipants日誌返回正確數量的選定好友,但numberOfRowsInSection-log返回0。

這是爲什麼?

在此先感謝!

+1

你從來沒有真正設置過任何東西! – 2013-03-16 14:35:25

回答

0

從您的代碼

- (void)setSelectedParticipants:(NSArray *)selectedParticipants { 
    NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]); 
} 

您已經在init方法設置selectedParticipants刪除此功能

+0

沒有說他應該「刪除」它 - 它只是需要被固定來實際設置基礎變量。 – 2013-03-16 14:36:10

+0

我建議一個簡單的修復。正如我在文章中提到的,「他已經在init函數中設置了數組」。 – 2013-03-16 14:48:40

2

這裏的問題是在你的二傳手:

- (void)setSelectedParticipants:(NSArray *)selectedParticipants { 
    NSLog(@"setSelectedParticipants (%d)", [selectedParticipants count]); 
} 

你會發現,你從來沒有真正設置支持屬性的實例變量的值,在這種情況下,缺省值爲_selectedParticipants。所以,要解決問題,只需將以下行添加到您的設置器:

_selectedParticipants = selectedParticipants; 

而且你應該很好去。

+0

這很有道理。起初我嘗試了(在setSelectedParticipants內部)做一個'self.selectedParticipants = selectedParticipants',這導致了一個循環,所以我想它是一種回調函數。但是因爲我已經將我的財產合成爲_selectedParticipants,所以它是有道理的。從PHP到Obj-C的過渡可能需要一些時間:)謝謝! – 2013-03-16 14:39:42

+0

@CasparAleksanderBangJespers沒問題,高興幫忙:) – 2013-03-16 14:40:10