簡體   English   中英

通過prepareForSegue傳遞屬性

[英]Pass attribute via prepareForSegue

如何通過prepareForSegue為自定義單元格傳遞屬性(此屬性來自實體)?

因此,我也懷疑是否可以根據用戶單擊的行通過過濾器發送此屬性? 例如,在第一個表中,用戶單擊“比薩店”,然后在第二個屏幕上顯示另一個表,該表僅包含類別為“比薩店”的企業。

到目前為止,這是我的代碼

#pragma mark - Ações da tabela
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //O número de linhas da tabela será o mesmo número de objetos na lista de categorias
    return listaDeCategorias.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CelulaEstabelecimentoTableViewCell *celula = [tableView dequeueReusableCellWithIdentifier:IdentificadorCelula
                                                                                 forIndexPath:indexPath];

    if(!celula) {
        celula = [[CelulaEstabelecimentoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                           reuseIdentifier:IdentificadorCelula];
    }

    categoria = [NSEntityDescription insertNewObjectForEntityForName:@"Categoria" inManagedObjectContext:contexto];
    categoria.nome = [listaDeCategorias objectAtIndex:indexPath.row];

    [celula.textLabel setText:categoria.nome];
    [celula setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return celula;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    categoria.nome = [listaDeCategorias objectAtIndex:indexPath.row];
    //NSString *categoriaClicada = [listaDeCategorias objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:IdentificadorSegue sender:categoria.nome];
}

#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:IdentificadorSegue]) {
        UINavigationController *navigationController = [segue destinationViewController];
        EstabelecimentoViewController *destino = (EstabelecimentoViewController *)([navigationController viewControllers][0]);

    }
}

注意:prepareForSegue方法故意不完整,因為他們不知道如何從那里繼續。

在這里,我希望自定義單元格填充過濾后的數據 在此處輸入圖片說明

映射人: 在此處輸入圖片說明

故事板: 在此處輸入圖片說明

假設您也在使用的ViewController也是EstabelecimentoViewController,它具有一個公共屬性“ nome”,這就是將“ nome”傳遞給EstabelecimentoViewController.nome的方式:

#import 'EstabelecimentoViewController.h'
.
.
.
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:IdentificadorSegue]) {
        EstabelecimentoViewController *establishmentVC = [segue destinationViewController];

        establishmentVC.nome = categoria.nome;
    }
}
/* Try This...*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"mySegue"] && self.demoObject != nil) {
        // This line returns a value...
        NSLog(@"self.demoObject = %@", self.demoObject.itemDescription);
        // ...but it crashes here when it tries to set on the destinationViewController
        if ([[segue destinationViewController] isKindOfClass:[SecondViewController class]]) {
            SecondViewController *destinationViewController = (SecondViewController *)[segue destinationViewController];
            destinationViewController.selectedItemPhoto = self.demoObject.photo;
            destinationViewController.selectedItemTitle = self.demoObject.itemDescription;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM