ios - 从表格 View 设置动态 View 的标题

标签 ios xcode uiviewcontroller uinavigationcontroller segue

我正在使用 SWRevealViewController、ContentViewController 和 NavigationViewController(导航)的组合。

Storyboard如下:

current status of the storyboard

在 TableViewCell 上单击我可以获得导航文本。我希望能够做的是将这些信息传递回自身。将 ContentViewController 的标题设置为所选单元格并加载其他所需数据。

我怎样才能实现这个功能?

这是我的 NavigationViewController.m 文件中的最新代码:

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

    BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];


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

    if (isChild) {
        NSString *cellText = selectedCell.detailTextLabel.text;
        NSLog(@"%@ tapped",cellText);
        return;
    }
    else{
        NSString *cellText = selectedCell.textLabel.text;
        NSLog(@"%@ tapped",cellText);

        if([cellText isEqualToString:@"Sign Out"]){
            [self performSegueWithIdentifier:@"goToHome" sender:self];
        }
        else if ([cellText isEqualToString:@"Sign In"]){
            [self performSegueWithIdentifier:@"goToHome" sender:self];
        }
        else if ([cellText isEqualToString:@"Festival Map"]){
            [self performSegueWithIdentifier:@"festivalMap" sender:self];
        }
    }

    [self.tableView beginUpdates];

    if (currentExpandedIndex == indexPath.row) {
        [self collapseSubItemsAtIndex:currentExpandedIndex];
        currentExpandedIndex = -1;
    }
    else {

        BOOL shouldCollapse = currentExpandedIndex > -1;

        if (shouldCollapse) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
        }

        currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

        [self expandItemAtIndex:currentExpandedIndex];
    }

    [self.tableView endUpdates];

}



- (void)expandItemAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    NSArray *currentSubItems = [subItems objectAtIndex:index];
    int insertPos = index + 1;
    for (int i = 0; i < [currentSubItems count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
    }
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

- (void)collapseSubItemsAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"festivalMap"]){
        NSLog(@"TEST");

        NSString * title = @"CHANGE";

        ViewController * view = [segue destinationViewController];
        view.navigationItem.title = title;
    }
}

这是我的 ContentViewController.m 文件的代码:

#import "ContentViewController.h"
#import "SWRevealViewController.h"
#import "Function.h"

@interface ContentViewController ()

@end

@implementation ContentViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _barButton.target = self.revealViewController;
    _barButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

@end

谢谢

最佳答案

你可以试试这个:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if([segue.identifier isEqualToString:@"yoursegue"]){  
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
    ContentViewController*contentView = (ContentViewController*)segue.destinationViewController;
    contentView.cellText = selectedCell.textLabel.text; 
    NSLog(@"cellText  = %@",selectedCell.textLabel.text;);
  }
}

但是,从用户界面访问值或围绕它构建逻辑并不是好的做法,您应该使用数据源。这违反了 MVC 原则。

关于ios - 从表格 View 设置动态 View 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849444/

相关文章:

objective-c - 如何从我的 appDelegate 访问我的 viewController? ( swift )

mysql - iOS 上的回合制多人游戏

ios - serialQueue 中的 performSelectorWithDelay

ios - 如何向函数添加计时器 - IOS Swift

ios - MagicalRecord saveWithBlock 用法现在无法在 XCode 7 beta 5 下编译

xcode - 准备将按钮命令发送到另一个 ViewController Swift

iOS 7 容器 View Controller 显示的详细 View Controller 比应有的要小

ios - 在 swift 4 中自动关闭 View Controller

xcode - 标识符为 'com.Company.ProductName' 的应用 ID 不可用,请输入其他字符串

ios - 在 iOS5.1/6 中,presentViewController 和 viewDidAppear 没有被调用