【问题标题】:How to change NavigationBar title in ios如何在ios中更改导航栏标题
【发布时间】:2013-12-28 03:53:13
【问题描述】:

嘿,所以我有点卡在这个问题上 - 我环顾四周,找不到任何特定于我的问题的东西,所以请随时重定向我:

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

对于 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;


}

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

【问题讨论】:

    标签: ios uitableview uinavigationbar


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用

      self.navigationItem.title = @"the title";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-15
        • 1970-01-01
        • 2015-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        相关资源
        最近更新 更多