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

时间:2013-12-28 03:53:13

标签: ios uitableview uinavigationbar

嘿所以我有点卡在这个 - 我已经环顾四周,找不到任何具体问题,所以请随意重定向我:

我已经设置了一个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;


}

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

使用

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