0

I am using the regular QuickDialog controller code from their tutorial:

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

How can I add a 'cancel' button to the navigation bar? I tried:

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

... but that didn't work. Any suggestions?

Jason
  • 14,517
  • 25
  • 92
  • 153

3 Answers3

0

The proper way to do this is to adjust the code for the `leftBarButtonItem' like this:

navigation.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
    [self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];
Jason
  • 14,517
  • 25
  • 92
  • 153
0

I know this is late but hopefully this will help someone else. I had a similar problem, and was able to use the following code to create a button to remove the form from view

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

I tried your code in my sample logic and achieved the cancel button in the following way.

You need to set your navigationItem's leftbarbuttonitem on the viewcontroller of a navigation controller, instead of setting straight to your navigationcontroller.

// -- 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];
}]];
raja
  • 1,151
  • 7
  • 9