gpt4 book ai didi

ios - 以编程方式将按钮添加到 UINavigationController

转载 作者:行者123 更新时间:2023-12-01 17:40:34 31 4
gpt4 key购买 nike

我认为这应该是一个简单的,但我只是摸不着头脑。我有一个在 Storyboard中设置了多个 ViewController 的应用程序。我希望它们中的每一个在每个 View 的顶部都有相同的导航栏,但是这个导航 Controller 在所有 View 上都有相同的按钮。导航 Controller 是在 AppDelegate 中设置的,并且按钮被添加到每个 ViewController 的 ViewDidLoad 中(根据我在这里收到的另一个问题的答案),但目前,按钮(和标题)没有显示。

UINavigationController 的设置位置 - AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

[self.window setRootViewController:navVC];

[_window makeKeyAndVisible];

return YES;
}

观点之一:
- (void)viewDidLoad
{

[self setTitle:@"Congress app"]; // < This does not set the title of the NavController

UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];

//self.navigationItem.leftBarButtonItem = showMenuButton;
//self.navigationItem.rightBarButtonItem = showSettingsButton;
self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;

UINavigationController *currentNav;
UIViewController *VCname;
NSLog(@"First left button in navigation controller - %@", [self.navigationController.navigationItem.leftBarButtonItems objectAtIndex:0]);
NSLog(@"First right button in navigation controller - %@", [self.navigationController.navigationItem.rightBarButtonItems objectAtIndex:0]);
[super viewDidLoad];
}

我有几个 NSLogs 显示导航 Controller 中的内容(如果有的话),它显示:以及哪些暗示正在添加按钮但未显示?

编辑:

我刚刚记得在上面的代码中,有一个 ViewController (InitViewController.m) 在 VC 之前被触发。 NavigationController 分配在这里:
- (void)viewDidLoad
{
[super viewDidLoad];

self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
}

这很可能是问题的原因。

编辑2:

感谢 Saurav Mac,我已经将问题追踪到我试图在“错误” View Controller 上添加按钮。我在 AppDelegate.m 中设置了导航 Controller ,第一个 View Controller 是 InitViewController,然后调用 MainViewController 作为“顶 View Controller ”。不敢相信我错过了,谢谢你的帮助。

最佳答案

您可以在公共(public)类中创建此按钮,即 NSObject 的子类,然后可以在要添加按钮的每个类中调用此方法,或者您可以将此代码添加到每个类的 ViewDidLoad 方法中 -

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];

UIImage *img1=[UIImage imageNamed:@"image.png"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:@selector(BackButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.leftBarButtonItem=barButton;

关于ios - 以编程方式将按钮添加到 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21229789/

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