ios - 当我点击 UIPopoverController 中的一个单元格时推送一个新的 UIViewController

标签 ios objective-c uitableview uiviewcontroller uipopovercontroller

当我单击 UIPopoverController 单元格时,我需要打开一个 ViewControllerSeccionesViewController 是我放入 UIPopoverController 中的 ViewController,它是一个表格。不起作用的函数是来自 TableViewdidSelectRowAtIndexPath

感谢您的提前。

我显示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];

    }


}

最佳答案

你有这个代码:

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

这让您看起来走在正确的轨道上。但是随后您重新加载 Storyboard,实例化一个 Controller 并将其推送到一个不存在的导航 Controller 中(因此,没有崩溃,但也没有结果)。

当您应该做的是使用该委托(delegate)关系将 selectedItem 传递回源 View Controller 。一旦到达那里,该 View Controller 就可以访问导航 Controller ,因此它可以决定如何使用 selectedItem 来创建 Controller 并推送该 Controller 以进行显示。

您可以将 tareas/eventos 传递回委托(delegate),而不是 selectedItem。谁应该拥有需要哪个 View Controller 的知识以及它应该如何配置是您根据您的应用程序结构来决定的。

关于ios - 当我点击 UIPopoverController 中的一个单元格时推送一个新的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22093882/

相关文章:

ios - 在 scrollViewDidScroll 中以编程方式更新 NSLayoutConstraint Swift 4

ios - 提高 objective-c 中的搜索性能

objective-c - 将 HTML 加载到子 UIWebView 时,我可以保持平滑滚动吗?

objective-c - 如何: Sort TableView per column using NSSortDescriptor when column names are unknown?

ios - 未从其他 View Controller 保存属性更新

ios - 在 UITableViewController 底部锚定 View

ios - 使用数组快速修改 Tableview

ios - 如何在导航 Controller 工具栏的栏按钮项上添加标题和图像

iphone - MPMoviePlayerViewController,删除 quicktime 符号/添加背景图像?

ios - 如何让我的 CollectionView 单元格从我的 TableView 中过滤数据?