gpt4 book ai didi

ios - 触摸 UITableView 中的行时更改 UINavigationBar 中的标题

转载 作者:行者123 更新时间:2023-11-28 20:26:10 26 4
gpt4 key购买 nike

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

目前我已将 UINavigationBar 与 IB 一起放置。然后用 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 中的导航栏链接到任何内容?

最佳答案

如果你有一个没有导航 Controller 的导航栏,你必须给它一个堆栈(在你的例子中,一堆)导航项。

在 View Controller 的 viewDidLoad 中,将 View Controller 的导航项添加到堆栈中:

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

现在你可以这样设置标题了:

self.navigationItem.title = @"Hello"; 

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

关于ios - 触摸 UITableView 中的行时更改 UINavigationBar 中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13889818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com