我最近开始学习 Obj-C,据我的朋友(对 Obj-C 有经验)说,如果我学会了如何以编程方式创建视图而不依赖 StoryBoards,那就更好了。
所以...我一直在尝试创建一个带有左键的 NavigationBar。但是,当我运行它时,我得到了实际的 NavigationBar,但没有得到按钮。这是我的代码:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
firstController = [[MainViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
这是 MainViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *navbarColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = navbarColor;
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:NULL];
self.navigationController.navigationItem.leftBarButtonItem = button;
}
任何帮助,将不胜感激。我对 Objective-C 还很陌生,但我理解代码概念,所以你在解释时不需要“婴儿”谈论它^.^