ios - 如何在 ios 中更改导航栏标题

标签 ios uitableview uinavigationbar

嘿,所以我有点卡在这个问题上了——我环顾四周,找不到任何与我的问题相关的内容,所以请随时重定向我:

我已经设置了一个 MapViewController 导航栏来根据单击的表格单元格更改标题,但是此 MapViewController 也可用作选项卡栏项目,它将显示更大的 map 但是当没有表格单元格被单击时它不会显示任何标题。

对于 TableViewController.m:

@interface FirstTableViewController () {

    NSArray *locations;
}

@end

@implementation FirstTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    locations = [NSArray arrayWithObjects:@"Bondi", nil];

}

- (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 [locations count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    //Uncomment to have array auto-name table as oposed to custom designs
    //cell.textLabel.text = [locations objectAtIndex:indexPath.row];
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"mapViewer"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        MapViewController *destViewController = segue.destinationViewController;
        destViewController.resultsName = [locations objectAtIndex:indexPath.row];
    }
}

对于 MapViewController.h 声明的属性:

@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) NSString *resultsName;
@property (strong, nonatomic) IBOutlet UINavigationItem *locationName;
@property (strong, nonatomic) IBOutlet UILabel *resultsLabel;

最后是 MapViewController.m:

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize resultsName;
@synthesize resultsLabel;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    resultsLabel.text = [NSString stringWithFormat:@"Showing %@", resultsName];
    _locationName.title = resultsName;


}

任何提示/技巧都将不胜感激 - 谢谢。

最佳答案

尝试在 MapViewController 上设置 title 属性。因此,在 viewDidLoad 中,您可以执行类似 self.title = self.resultsName; 的操作,或者直接在您的 prepareForSegue 方法中进行设置。

关于ios - 如何在 ios 中更改导航栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20810817/

相关文章:

ios - 当我使用 SKPhysicsBody(纹理 :size:) to create physics body I got an issue

ios - 带有自定义动画的屏幕弹出 Controller 时出现黑色效果?

ios - 顶点和法线的单独数组

ios - UISearchDisplayController 结果后可见的额外单元格

ios - UITableView:如何仅针对单元格将 clipsToBounds 设置为 Yes?

swift - 点击 UIImageView 时隐藏的导航栏和工具栏

ios - UITableViewController 和 UIViewController 的父类(super class)

ios - 如何设置tableView中第一个单元格的rowHeight

ios - UISearchbar添加navigationController?.navigationBar,如何更改 "Cancel"颜色

swift - 将 UI BarButtonSystemItem 图标添加到 Nav-Toolbar (SWIFT)