我找遍了,但我似乎找不到。我相信很多人都会有我的链接,我很可能已经看过了。但如果有人能给我看一下做以下事情的代码:
我希望在我的UINavigationBar中有一个左箭头作为"Back“UINavigationItem。我该怎么做呢?下面是我的UIViewController中的当前代码
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:@selector(backButtonSelected:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:nil action:@selector(optionsButtonSelected:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;
[self.view addSubview:theBar];发布于 2011-01-05 01:24:29
为了在屏幕上弹出/推送UIViewControllers,您应该使用UINavigationController。导航控制器会自动将UINavigationBar添加到您的UIViewControllers中,因此您不需要像以前那样创建它们。
这是一个示例。我没有寻找内存泄漏。在应用程序委托中,您可以找到此方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
MainVC *mainVC = [[MainVC alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}MainVC是表示层次结构的级别1的UIViewController。我在里面有
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor redColor];
self.title = @"MainVC";
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"SecondLevel" style:UIBarButtonItemStyleBordered target:self action:@selector(secondLevelSelected:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
- (void) secondLevelSelected:(id)sender
{
SecondVC *secondVC = [[SecondVC alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}SecondVC是另一个表示层次结构第二级的UIViewController。在这里你会找到你想要的后退按钮。
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
self.view.backgroundColor = [UIColor greenColor];
self.title = @"SecondVC";
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"FirstLevel" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBtnSelected:)];
self.navigationItem.leftBarButtonItem = leftBtn;
}
- (void) leftBtnSelected:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}发布于 2012-07-11 07:29:31
我想这可能就是你要找的。
// in .h file
@property (nonatomic, retain) UINavigationBar *navBar;
// in .m file just below @implementation
@synthesize navBar;
// within .m method
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
navBar.barStyle = UIBarStyleBlack;
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Nav Bar Title"];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStylePlain
target:nil
action:@selector(backButtonSelected:)];
title.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:nil
action:@selector(showActionSheet:)];
title.rightBarButtonItem = rightButton;
[navBar pushNavigationItem:title animated:YES];
[self.view addSubview:navBar];https://stackoverflow.com/questions/4596234
复制相似问题