0

好的,所以我尝试了很多不同的方法......

目前我已经把UINavigationBarIB 放在了一起。然后用IBOutlet.

无论我尝试什么,从那里我似乎都无法更改标题。即使只是在ViewDidLoad.

另外,我不需要分配导航栏的代表吗?

这是我的代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController < UITableViewDataSource, UITableViewDelegate>
{
    NSArray *myArrayOne;
    NSArray *myArrayTwo;
    UITextView *myTextView;
    UINavigationBar *myNavBar;   
}

@property (nonatomic, retain) NSArray *myArrayOne;
@property (nonatomic, retain) NSArray *myArrayTwo;
@property (nonatomic, retain) IBOutlet UITextView *myTextView;
@property (nonatomic, retain) IBOutlet UINavigationBar *myNavBar;


@end

和:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//Synthesising anything with a property call in the .h file.
@synthesize myArrayOne;
@synthesize myArrayTwo;
@synthesize myTextView;
@synthesize myNavBar;

- (void)viewDidLoad
{
    //declaring and instantiating datapaths and also
    // reading into arrays from plist.
    NSString *datapath1 = [[NSBundle mainBundle] pathForResource:@"myListOne" ofType:@"plist"];
    NSString *datapath2 = [[NSBundle mainBundle] pathForResource:@"myListTwo" ofType:@"plist"];

    myArrayOne = [[NSArray alloc] initWithContentsOfFile:datapath1];
    myArrayTwo = [[NSArray alloc] initWithContentsOfFile:datapath2];
    self.navigationItem.title = @"Hi";
    [super viewDidLoad];
}


#pragma mark - TableView Delegate stuff

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //myNavBar.title = [myArrayOne objectAtIndex:indexPath.row];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - TableView Datasource stuff

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //returns amount of items inside myArrayOne as amount of rows
    return [myArrayOne count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *myCell = nil;
    myCell = [tableView dequeueReusableCellWithIdentifier:@"myReuseCell"];

    if (myCell == nil)
    {
        myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myReuseCell"];
    }

    myCell.textLabel.text = [myArrayOne objectAtIndex:indexPath.row];

    return myCell;
}

@end

更改标题的实际代码只是我尝试过的一种方法......

还有什么办法吗?我需要将 IB 中的导航栏与任何东西链接吗?

4

3 回答 3

2

如果您有一个没有导航控制器的导航栏,则必须给它一个堆栈(在您的情况下是一个堆栈)导航项。

viewDidLoad视图控制器中,将视图控制器的导航项添加到堆栈中:

self.myNavBar.items = @[self.navigationItem];

现在您可以像这样设置标题:

self.navigationItem.title = @"Hello"; 

它会出现在您的导航栏中。

于 2012-12-15T07:01:13.353 回答
1

jrturton 上面的解决方案可以正常工作,除非您在 UINavigationBar 中还有一个 UIBarButtonItem(例如“完成”按钮)。在这种情况下,建议的解决方案会导致 UIBarButtonItem 消失。

以下是如何以一种让您更改 UINavigationBar 标题但也保留 UIBarButtonItem 的方式进行操作:

添加IBOutlet UINavigationItem *myNavigationItem;到您的代码中。

在 IB 中,在大纲视图(不是故事板视图)中,右键单击包含 UIViewController 并拖动到由 IB 在 UINavigationBar 内自动创建的 UINavigationItem 并连接到myNavigationItem.

回到您的代码中,添加self.myNavigationItem.title = @"Hello";

于 2015-02-12T20:44:24.973 回答
0

我认为你需要一个 navigationController 而不是你为什么不检查这个链接:

在objective-C中以编程方式为tableView创建导航栏

如果您的应用程序委托中已经有一个导航控制器,那么您应该删除您创建的 IBoutlet 然后使用:

self.navigationItem.title = @"Hi";

因为这将在您的应用程序委托中引用 navigationController。

于 2012-12-15T06:36:09.013 回答