首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向左箭头- UINavigationItem

向左箭头- UINavigationItem
EN

Stack Overflow用户
提问于 2011-01-05 00:58:33
回答 2查看 7.5K关注 0票数 2

我找遍了,但我似乎找不到。我相信很多人都会有我的链接,我很可能已经看过了。但如果有人能给我看一下做以下事情的代码:

我希望在我的UINavigationBar中有一个左箭头作为"Back“UINavigationItem。我该怎么做呢?下面是我的UIViewController中的当前代码

代码语言:javascript
复制
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];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-05 01:24:29

为了在屏幕上弹出/推送UIViewControllers,您应该使用UINavigationController。导航控制器会自动将UINavigationBar添加到您的UIViewControllers中,因此您不需要像以前那样创建它们。

这是一个示例。我没有寻找内存泄漏。在应用程序委托中,您可以找到此方法:

代码语言:javascript
复制
- (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。我在里面有

代码语言:javascript
复制
- (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。在这里你会找到你想要的后退按钮。

代码语言:javascript
复制
- (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];
}
票数 1
EN

Stack Overflow用户

发布于 2012-07-11 07:29:31

我想这可能就是你要找的。

代码语言:javascript
复制
// 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];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4596234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档