在我的应用程序中,我试图使用导航栏后退按钮从第二个 ViewController 导航到第一个 ViewController,但是在我的应用程序的 XIB 中将导航栏拖放到 ViewController 上时,我只能获得带有标题的栏。
我究竟做错了什么?我正在使用 Xcode 4.6。和 .xib 文件来设计视图。这是我现在得到的:
这就是我要找的东西:
在我的应用程序中,我试图使用导航栏后退按钮从第二个 ViewController 导航到第一个 ViewController,但是在我的应用程序的 XIB 中将导航栏拖放到 ViewController 上时,我只能获得带有标题的栏。
我究竟做错了什么?我正在使用 Xcode 4.6。和 .xib 文件来设计视图。这是我现在得到的:
这就是我要找的东西:
导航控制器提供了一个默认的导航栏和一个后退按钮。但是后退按钮标题的改变是一件有点乱的事情
以编程方式
UIStoryboard *astoryBoard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController2 *vc=[astoryBoard instantiateViewControllerWithIdentifier:@"ViewController2"];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Master" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[self.navigationController pushViewController:vc animated:YES];
如果您使用segue,请将这些行包含到父 VC
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Master" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
只为回来
代码明智
UIStoryboard *astoryBoard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController2 *vc=[astoryBoard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self.navigationController pushViewController:vc animated:YES];
不是故事板
创建第二个 VC 的实例,然后使用此代码
YourViewController *VC=[[YourViewController alloc]initWithNibName:@"yournibname" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:VC animated:YES];
在您的 BUS2AppDelegate.h 文件中添加此代码
@interface BUS2AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navController; //you have added this code?
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
并在 BUS2AppDelegate.m 文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[BUS2ViewController alloc] initWithNibName:@"BUS2ViewController" bundle:nil];
// View Controller with each Navigational stack support.
navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
在您的第一个视图控制器中添加此代码
#import "AAlesund.h"
- (IBAction)secodVCBtnClicked:(id)sender {
AAlesund *aAlesund = [[AAlesund alloc] initWithNibName:@"AAlesund" bundle:nil];
self.navigationItem.title = @"Master";
[self.navigationController pushViewController:aAlesund animated:NO];
}
要在第二个视图控制器的导航栏中添加标题,请添加此代码
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *nav_title1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 220, 25)];
nav_title1.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
nav_title1.textColor = [UIColor whiteColor];
nav_title1.adjustsFontSizeToFitWidth = NO;
nav_title1.textAlignment = UITextAlignmentCenter;
nav_title1.text = @"Detail";
nav_title1.backgroundColor = [UIColor clearColor];
self.navigationItem.titleView = nav_title1;
}
这是输出。