如何在QuickDialog控制器中添加“取消”按钮?

时间:2013-11-13 09:25:26

标签: ios iphone uinavigationcontroller uinavigationbar quickdialog

我正在使用他们教程中的常规QuickDialog控制器代码:

QRootElement *root = [[QRootElement alloc] init];
root.title = @"Hello"
root.grouped = YES;

QSection *section = [[QSection alloc] init];
QEntryElement *hello = [[QEntryElement alloc] initWithTitle:@"Hello World" Value:@""];

[root addSection:section];
[info addElement:hello];

UINavigationController *navigation = [QuickDialogController controllerWithNavigationForRoot:root];
[self presentModalViewController:navigation animated:YES];

如何在导航栏中添加“取消”按钮?我试过了:

navigation.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
    [self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];

......但那没用。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

执行此操作的正确方法是调整`leftBarButtonItem'的代码,如下所示:

navigation.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
    [self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];

答案 1 :(得分:0)

我知道这已经晚了但希望这会对其他人有所帮助。我遇到了类似的问题,并且能够使用以下代码创建一个按钮来从视图中删除表单

navigation.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDone target:self action:@selector(dismissModalViewControllerAnimated:)];

答案 2 :(得分:0)

我在示例逻辑中尝试了您的代码,并按以下方式实现了取消按钮。

您需要在导航控制器的viewcontroller上设置navigationItem的leftbarbuttonitem,而不是直接设置为导航控制器。

// -- present your root controller.
UINavigationController *navigation = [QuickDialogController controllerWithNavigationForRoot:root];
[self presentModalViewController:navigation animated:YES];
[[[navigation topViewController] navigationItem] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController dismissModalViewControllerAnimated:YES];
}]];