2013-04-10 55 views
0

我在我的應用中有幾個UIPopover s,其中包含UITableView s。所有人在被解僱時都會發送消息popoverControllerDidDismissPopover:。當某個特定的popover被解散時,我想要將所有用戶的選擇都移動到UITextView確定哪個UIPopover控制器已被解僱

我不能這樣做,除非我知道哪個popover正在被解僱。任何想法我可以做到這一點?

UIViewController* popoverContent = [[UIViewController alloc] init]; 

    UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 416)]; // was 216 
    popoverView.backgroundColor = [UIColor redColor]; 
    popoverContent.contentSizeForViewInPopover = CGSizeMake(300.0, 416.0); 

    // define UITableView 
    tvServices = [[UITableView alloc] init]; 
    tvServices.frame = CGRectMake(0, 0, 300, 416); 
    tvServices.tag = 1201; 

    tvServices.delegate = self; 
    tvServices.dataSource = self; 

    // add it to the popover 
    [popoverView addSubview:tvServices]; 
    popoverContent.view = popoverView; 
    popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; 
    popoverController.delegate = (id)self; 
    [popoverController setPopoverContentSize:CGSizeMake(300, 416) animated:NO]; 

    // show it next to services textbox 
    [popoverController presentPopoverFromRect:soServices.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; 
} 
+1

您可以在代理的方法'勾選駁回UIPopover做到這一點 - (無效)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController'那麼你可以通過isKindOfClass檢查:或等同標籤。 – 2013-04-10 21:51:24

回答

2

popoverControllerDidDismissPopover:委託方法告訴你哪個彈出被解除。從那裏你可以根據需要查看popover的contentViewController

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { 
    UITableViewController *controller = (UITableViewController *)popoverController.contentViewController; 

    UITableView *tableView = controller.tableView; 
} 

這應該指出你在正確的目錄。但是你不應該需要訪問表視圖。您正在訪問視圖控制器中的數據,而不是表格視圖。

+0

嗨馬迪...我沒有看到任何可以識別控制器時使用** contentViewController ** ...你有什麼想法? – SpokaneDude 2013-04-10 21:56:38

+1

您可以檢查內容視圖控制器的類,也可以在視圖控制器的視圖上設置標籤。如果你有共同的處理,那麼更好的方法就是定義一個協議,以使所有這些視圖控制器都符合。然後你可以使用協議方法來詢問視圖控制器。 – rmaddy 2013-04-10 22:06:15

+0

我需要進入控制器內部以查看哪個UITableView在那裏......但當然,沒有任何我知道的事情會告訴我UITableView何時在UIPopover中消失。 – SpokaneDude 2013-04-10 22:12:20

1

你應該做它周圍的其他方法:

的控制器,你現在應該使用代表團通知開始演示控制器。

在您的彈出窗口控制器中,向委託人(呈現控制器)發送消息以通知其選擇/取消選擇。


#import "ViewController.h" 
#import "QuestionGroupViewController.h" 

@interface ViewController() <QuestionGroupViewControllerDelegate> 
@end 

@implementation ViewController 

- (IBAction)presentQuestionnaire:(id)sender { 

    NSArray *q = @[@{@"question" : @"what cities would you like to visit?" , @"answers": @[@"Paris", @"barcelona", @"Istanbul"]}]; 

     QuestionGroupViewController *qvc = [QuestionGroupViewController new]; 
     qvc.delegate = self; 
     qvc.questions = q; 
     [self presentViewController:qvc animated:YES completion:NULL]; 
    } 
} 

-(void)selectedAnswer:(NSString *)answer forQuestion:(NSString *)question 
{ 
    NSLog(@"selectedt\t\t%@: %@", question, answer); 
} 
-(void)deSelectedAnswer:(NSString *)answer forQuestion:(NSString *)question 
{ 
    NSLog(@"deselectedt\t\t%@: %@", question, answer); 
} 
@end 

#import <Foundation/Foundation.h>  
@protocol QuestionGroupViewControllerDelegate<NSObject> 
-(void) selectedAnswer:(NSString *)answer forQuestion:(NSString *) question; 
-(void) deSelectedAnswer:(NSString *)answer forQuestion:(NSString *) question; 

@end 

@interface QuestionGroupViewController : UITableViewController 

@property (nonatomic, strong) NSArray *questions; 
@property (nonatomic, weak) id<QuestionGroupViewControllerDelegate> delegate; 
@end 

#import "QuestionGroupViewController.h" 


@interface QuestionGroupViewController() 
@property (nonatomic, strong) NSMutableArray *selectedCells; 

@end 


@implementation QuestionGroupViewController { 

} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.selectedCells = [NSMutableArray array]; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [self.questions count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [self.questions[section][@"answers"] count] +1 ; 

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return self.questions[section][@"question"]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier [email protected]"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if(!cell){ 
     cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:identifier]; 

    } 
    if (indexPath.row == [self.questions[indexPath.section][@"answers"] count]) { 
     cell.textLabel.text = @"done"; 
    } else { 
     cell.textLabel.text = [[self.questions objectAtIndex:indexPath.section][@"answers"] objectAtIndex:indexPath.row]; 
    } 


    return cell; 
} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.row == [self.questions[indexPath.section][@"answers"] count]) { 
     [self dismissViewControllerAnimated:YES completion:NULL]; 
     return; 
    } 

    NSString *answer = self.questions[indexPath.section][@"answers"][indexPath.row]; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){ 
     [self.selectedCells removeObject:indexPath]; 

     cell.accessoryType = UITableViewCellAccessoryNone; 
     [self.delegate deSelectedAnswer: answer forQuestion:self.questions[indexPath.section][@"question"]]; 

    } else { 
     [self.selectedCells addObject:indexPath]; 

     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [self.delegate selectedAnswer: answer forQuestion:self.questions[indexPath.section][@"question"]]; 

    } 

} 

-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath 
{ 
    return ([self.selectedCells containsObject:indexPath]) ? YES : NO; 
} 

@end 
0

如果你是從你的代碼呈現popovers,那麼你應該給他們一個參考。收到彈出式代理呼叫時,您只需檢查哪個實例正被解散。聽起來很簡單,除非我錯過了一些東西。

1

你應該popoverControllerDidDismissPopover:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { 
    UITableView *tableView = (UITableView*)[popoverController.contentViewController.view viewWithTag:1201]; 
    if (tableView != nil) { 
     // here you know that this UIPopover has tvServices as you set its tag to 1201 
    } 
}