将后退按钮设置为自定义UIImage

时间:2013-07-25 04:36:55

标签: iphone ios objective-c

目前我在UIBarButtonItems中使用我自己的自定义图像,代码如下:

UIButton *profileBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
    [profileBarButton setImage:[UIImage imageNamed:@"profile-barbutton.png"] forState:UIControlStateNormal];
    [profileBarButton addTarget:self.navigationController action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];



    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:profileBarButton];

当我有一个定义的动作要调用时,这很有效,例如呈现一个modalviewcontroller并打开编辑模式。但是,我很困惑我如何设置从一个视图回到另一个视图的动作,而不是模态?是否有一种我可以通过编程方式调用的特定方法?通常导航控制器会处理这个......

4 个答案:

答案 0 :(得分:1)

从一个视图回到另一个视图,不是模态的,你可以这样写:

- (void) toggleMenu
{
    if (self.navigationController.visibleViewController == self)
    {
        [self.navigationController popViewControllerAnimated: YES];
    }
}

答案 1 :(得分:0)

使用此代码:

 [self dismissViewControllerAnimated:YES completion:nil];

答案 2 :(得分:0)

检查此代码......

    MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyNib" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentModalViewController:nc animated:YES];

[vc release];
[nc release];

然后通常推送代码......

    OtherViewController *vc = [[OtherViewController alloc] initWithNibName:@"MyOtherNib" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];

:)

答案 3 :(得分:0)

试试这段代码:

UIImage *backButtonImage = [UIImage imageNamed:@"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

self.navigationItem.leftBarButtonItem = customBackBarItem;

在@selector(后退)中,“back”是触发navigationcontroller弹出方法的方法。像这样:

-(void)back {
    [self.navigationController popViewControllerAnimated:YES];
}