0

目前我在 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

4 回答 4

1

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

- (void) toggleMenu
{
    if (self.navigationController.visibleViewController == self)
    {
        [self.navigationController popViewControllerAnimated: YES];
    }
}
于 2013-07-25T05:04:35.077 回答
0

使用此代码:

 [self dismissViewControllerAnimated:YES completion:nil];
于 2013-07-25T04:38:22.837 回答
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];

:)

于 2013-07-25T04:45:06.900 回答
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)中,“back”是触发navigationcontroller弹出方法的方法。像这样:

-(void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
于 2013-07-25T05:02:38.517 回答