左箭头 - UINavigationItem

时间:2011-01-04 16:58:33

标签: iphone ios uinavigationbar uinavigationitem

我四处寻找,似乎无法弄明白。我相信很多人会为我提供这样的链接,我很可能已经看过了。但是,如果有人可以请告诉我代码执行以下操作:

我希望UINavigationBar中的左箭头为“后退”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];

2 个答案:

答案 0 :(得分:2)

我认为这可能就是你要找的东西。

// 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];

答案 1 :(得分:1)

您应该使用UINavigationController在屏幕上弹出/推送UIViewControllers。导航控制器会自动为UINavigationBar添加UIViewControllers,因此您无需像创建它一样创建它们。

这是一个示例。我没有寻找内存泄漏。 在app委托中,您将找到此方法:

- (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是UIViewController,代表层次结构的第1级。我有

- (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];
}