ios - 如何将详细 View Pane 标题设置为当前所选单元格的名称?

标签 ios objective-c xcode ipad master-detail

在我的脑海中是否足够简单? 选择单元格 - 将详细信息面板标题设置为当前所选单元格的名称。

如果您需要任何其他文件,请告诉我。任何帮助将不胜感激!!

我的MasterViewController.h如下

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "TableViewCell.h"

@interface MasterViewController ()

@property NSMutableArray *objects;
@end

@implementation MasterViewController

- (void)awakeFromNib {
    [super awakeFromNib];
    self.clearsSelectionOnViewWillAppear = NO;
    self.preferredContentSize = CGSizeMake(320.0, 600.0);
}

- (IBAction)goHome{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Root View Controller";

    //Add button to NavigationController
    UIBarButtonItem *goHome =
    [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Home",)
                                     style:UIBarButtonItemStyleDone
                                    target:self
                                    action:@selector(goHome)];

    self.navigationItem.leftBarButtonItem = goHome;


    _subjectList = @[@"Art",
                     @"Business",
                     @"Citizenship",
                     @"Computer Science",
                     @"Dance",
                     @"Design Technology",
                     @"Drama",
                     @"English",
                     @"Food Technology",
                     @"Geography",
                     @"History",
                     @"ICT",
                     @"Languages",
                     @"Life Course",
                     @"Maths",
                     @"Media Studies",
                     @"Music",
                     @"PE",
                     @"Religious Studies",
                     @"Science",
                     @"Textiles",];


}



- (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 _subjectList.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"TableViewCell";
    TableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    long row = [indexPath row];
    Cell.SubjectNameLabel.text = _subjectList[row];

    return Cell;


}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = self.objects[indexPath.row];
        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        [controller setDetailItem:object];
        controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
        controller.navigationItem.leftItemsSupplementBackButton = YES;
        //THIS LINE BELOW IS WHERE I PLANNED ON SETTING THE TITLE?
        controller.navigationItem.title = selectedCell;
    }
}



@end

最佳答案

在细节 View Controller 的 .h 文件中创建一个 NSString 属性。然后在 prepareForSegue 中将 string 属性设置为 cell 的名称。您将从 tableViews 数据源中获取单元格标题的名称。

这就是我的意思:

DetailViewController.h 中插入以下内容:

@property (strong, nonatomic) NSString *titleName;

MasterViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    DetailViewController *controller = segue.destinationViewController;
    controller.titleName = [self.subjectList objectAtIndex:indexPath.row];
}

DetailViewController.mviewDidLoad 中插入以下内容:

self.navigationItem.title = self.titleName;

看看这个教程。它应该对你有很大帮助:detail view controller

关于ios - 如何将详细 View Pane 标题设置为当前所选单元格的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061162/

相关文章:

ios - 使用 Xcode 6.4 在 swift 中使用 GMSPlacesClient 时出错

ios - 我现在还需要两个 Storyboard 文件来使用 Universal Storyboard 管理不同的设备吗?

ios - 无效 bundle 。 bundle xxx 不支持最低操作系统版本 (2020)

iphone - 安装 IPA 导致错误 "A signed resource has been added, modified, or deleted"

c# - 等同于 C# 中的协议(protocol)和委托(delegate)

ios - 如何使用 Jenkins 设置我的远程奴隶?

c++ - 计算器程序 Xcode 错误 '@' 标记前的预期表达式

objective-c - UITableViewController 与 TableView

ios - Xcode 8.1 : No options for update frames on changing constraints

ios - 将 NSMutableArray 添加到项目并在另一个类中访问相同的 NSMutableArray