Objective-C 程序化标题未显示
Objective-C Programmatic title not showing
我已经在 ViewController.m 中设置了我的 self.navigationItem.title = @"Hello";
但它没有显示:
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions API_AVAILABLE(ios(13.0)) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
ViewController.m
#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Hello";
self.view.backgroundColor = [UIColor systemBlueColor];
}
@end
提前致谢!
我认为您没有使用故事板。
在您的情况下,您必须以编程方式执行所有操作,包括分配导航栏。
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Hello"];
self.view.backgroundColor = [UIColor systemBlueColor];
[navbar setItems:@[title]];
[self.view addSubview:navbar];
}
如果您使用的是故事板,这个简单的命令就可以工作,而不需要 appDelegate 中的任何内容。
self.navigationItem.title = @"Hello";
足以说明:viewController.title = @“Hello”;
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621364-title
我已经在 ViewController.m 中设置了我的 self.navigationItem.title = @"Hello";
但它没有显示:
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions API_AVAILABLE(ios(13.0)) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
ViewController.m
#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Hello";
self.view.backgroundColor = [UIColor systemBlueColor];
}
@end
提前致谢!
我认为您没有使用故事板。 在您的情况下,您必须以编程方式执行所有操作,包括分配导航栏。
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Hello"];
self.view.backgroundColor = [UIColor systemBlueColor];
[navbar setItems:@[title]];
[self.view addSubview:navbar];
}
如果您使用的是故事板,这个简单的命令就可以工作,而不需要 appDelegate 中的任何内容。
self.navigationItem.title = @"Hello";
足以说明:viewController.title = @“Hello”;
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621364-title