如何将'cancel'按钮添加到快速对话框控制器?



我正在使用他们教程中的常规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];
}];

。但这没有用。有什么建议吗?

正确的方法是调整"leftBarButtonItem"的代码,如下所示:

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

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

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

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

您需要在导航控制器的

视图控制器上设置导航项的左栏按钮项,而不是直接设置到导航控制器。

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

最新更新