0

當我點擊一個UIPopoverController單元時,我需要打開一個ViewControllerSeccionesViewControllerViewController,我在UIPopoverController,這是一個表。不起作用的功能是TableViewdidSelectRowAtIndexPath當我點擊UIPopoverController中的一個單元時推送一個新的UIViewController

謝謝您的提前。

我的代碼顯示UIPopoverController是:

-(IBAction)seccionesButtonTapped:(id)sender 
{ 

     if (_itemPicker == nil) { 
      //Create the ColorPickerViewController. 
      _itemPicker = [[SeccionesViewController alloc] initWithStyle:UITableViewStylePlain]; 

      //Set this VC as the delegate. 
      _itemPicker.delegate = self; 
     } 

     if (_itemPickerPopover == nil) { 
      //The color picker popover is not showing. Show it. 
      _itemPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_itemPicker]; 
      //[_itemPickerPopover presentPopoverFromBarButtonItem:(UIBarButtonItem *) sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
      [_itemPickerPopover presentPopoverFromRect:CGRectMake(350, 902, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; 
     } else { 
      //The color picker popover is showing. Hide it. 
      [_itemPickerPopover dismissPopoverAnimated:YES]; 
     } 
} 

SeccionesViewController代碼:

-(id)initWithStyle:(UITableViewStyle)style 
{ 
    if ([super initWithStyle:style] != nil) { 

     //Initialize the array 
     _itemNames = [NSMutableArray array]; 

     //Set up the array of colors. 
     [_itemNames addObject:AMLocalizedString(@"listaTareas", @"")]; 
     [_itemNames addObject:AMLocalizedString(@"EventosTab", @"")]; 

     //Make row selections persist. 
     self.clearsSelectionOnViewWillAppear = NO; 

     //Calculate how tall the view should be by multiplying the individual row height 
     //by the total number of rows. 
     NSInteger rowsCount = [_itemNames count]; 
     NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
     NSInteger totalRowsHeight = rowsCount * singleRowHeight; 

     //Calculate how wide the view should be by finding how wide each string is expected to be 
     CGFloat largestLabelWidth = 0; 
     for (NSString *itemName in _itemNames) { 
      //Checks size of text using the default font for UITableViewCell's textLabel. 
      CGSize labelSize = [itemName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]]; 
      if (labelSize.width > largestLabelWidth) { 
       largestLabelWidth = labelSize.width; 
      } 
     } 

     //Add a little padding to the width 
     CGFloat popoverWidth = largestLabelWidth + 100; 

     //Set the property to tell the popover container how big this view will be. 
     self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight); 
    } 

    return self; 
} 

#pragma mark - View Lifecycle 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [_itemNames count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [_itemNames objectAtIndex:indexPath.row]; 

    return cell; 
} 

#pragma mark - Table view delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *selectedItem = [_itemNames objectAtIndex:indexPath.row]; 

    NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults]; 
    NSString *dis = [dispositivo stringForKey:@"dispositivo"]; 

    if ([selectedItem isEqualToString:AMLocalizedString(@"listaTareas", @"")]) { 

     UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:dis bundle:nil]; 
     ListaTareasViewController *tareas= [storyBoard instantiateViewControllerWithIdentifier:@"ListaTareasView"]; 
     tareas.communitykey = communitykey; 
     tareas.tituloAsig = tituloAsig; 
     tareas.infoH = infoH; 
     if ((vengoDe != 1) && (vengoDe != 2) && (vengoDe != 3)) { 
      tareas.vengoDe = 1; 
     }else{ 
      tareas.vengoDe = vengoDe; 
     } 
     tareas.infoAsig = infoAsig; 
     [self.navigationController pushViewController:tareas animated:YES]; 

    }else if ([selectedItem isEqualToString:AMLocalizedString(@"EventosTab", @"")]){ 

     UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:dis bundle:nil]; 
     ListaEventosViewController *eventos= [storyBoard instantiateViewControllerWithIdentifier:@"listaEventosView"]; 
     eventos.communitykey = communitykey; 
     eventos.nomAsig = tituloAsig; 
     eventos.infoH = infoH; 
     if ((vengoDe != 1) && (vengoDe != 2) && (vengoDe != 3)) { 
      eventos.vengoDe = 1; 
     }else{ 
      eventos.vengoDe = vengoDe; 
     } 
     eventos.infoAsig = infoAsig; 
     [self.navigationController pushViewController:eventos animated:YES]; 

    } 


} 

回答

1

你有這樣的代碼:

 //Set this VC as the delegate. 
     _itemPicker.delegate = self; 

這使得它看起來像你在正確的軌道上。但是,然後你重新加載故事板,實例化一個控制器並將其推送到一個不存在的導航控制器中(所以,沒有崩潰,但沒有結果)。

當您應該做的是使用該代理關係將selectedItem傳遞迴源視圖控制器。一旦出現,該視圖控制器就可以訪問導航控制器,因此它可以決定如何使用selectedItem來創建控制器並推送該控制器以進行顯示。

有可能您可以將tareas/eventos返回給代表而不是selectedItem。誰應該掌握哪些視圖控制器是必需的知識以及應該如何配置這些知識是您根據您的應用程序結構決定的。

+0

就像這樣:'self.navigationController.delegate = self;'?? – amurcia

+0

對不起,但我很迷茫... – amurcia

+0

不,傳回給委託就像'[self.delegate seccionesViewController:self didSelectItem:## XXX ##];'其中## XXX ##是視圖控制器或選定的項目。 – Wain

相關問題