How to add a 'cancel' button to a QuickDialog controller? - ios

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?

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

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

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

Related

iOS - [ObjC] NavigationBarButtonItem is not showing when the navigation bar's root view is presented modally

In my app I have two tabs that handle different set of functions.
One tab is the user tab, when the user switch to this tab, the tab controller checks whether the user has logged in. If not, it shows a button(LoginBtn) which triggers a log in view controller to show when tapped.
I intend to present the log in controller modally with a navigation bar.
However, the navigation bar is not showing the right button item although I've initiated it.
Here's the code
- (void)clickLoginBtn{
LogginController* _cLogginController = [[LogginController alloc] init];
UINavigationController *_cNavController = [[UINavigationController alloc] initWithRootViewController:_cLogginController];
_cNavController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"取消" style:UIBarButtonItemStylePlain target:self action:#selector(dismissLoginView)];
[_cNavController.navigationItem.rightBarButtonItem setTintColor:kColorWhite];
_cNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:_cNavController animated:YES completion:nil];
}
What could be the problem? Is it possible that is because I present to controller modally?
You have put your buttons to the login controller:
_cLogginController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"取消" style:UIBarButtonItemStylePlain target:self action:#selector(dismissLoginView)];
You shouldn't add navigation items to the navigation controller.
try this code.I think this issue is solved by this code
- (void)clickLoginBtn{
LogginController* _cLogginController = [[LogginController alloc] init];
_cLogginController .hidesBottomBarWhenPushed=No;//You need to add this line
UINavigationController *_cNavController = [[UINavigationController alloc] initWithRootViewController:_cLogginController];
_cNavController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"取消" style:UIBarButtonItemStylePlain target:self action:#selector(dismissLoginView)];
[_cNavController.navigationItem.rightBarButtonItem setTintColor:kColorWhite];
_cNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:_cNavController animated:YES completion:nil];
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DemoViewController * _cLogginController = [[DemoViewController alloc]init];
_cLogginController = [storyboard instantiateViewControllerWithIdentifier:#"DemoViewController"];
_cLogginController .hidesBottomBarWhenPushed = NO;
UINavigationController *_cNavController = [[UINavigationController alloc]initWithRootViewController:_cLogginController];
_cNavController.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
_cNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
_cLogginController.navigationItem.title = #"取消";
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView:)];
flipButton.tintColor = [UIColor grayColor];
_cLogginController.navigationItem.rightBarButtonItem = flipButton;
[self presentViewController:_cNavController animated:YES completion:nil];

Add a navigationbar with a back button to the app [duplicate]

This question already has answers here:
how to create back button in navigation bar
(3 answers)
Closed 9 years ago.
I need a step by step tutorial to add a navigation bar including a back button to my project.
My rootViewController defined in the AppDelegate is the LoginViewController. After successfull login it goes to the MainView and then to the SingleView
How would I add a navigation bar and a back button? This is the last thing I need for my app. I already tried many things
for example:
everything in the viewDidLoad method
First Try
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
Second Try
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
What exactly do I write into the RootViewController and what do I write into the other UIViewController to get a butotn?
Edit 2 after Popeye's suggestion
//Appdelegate.m
LoginViewController *viewController = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
[navCon setNavigationBarHidden:NO];
self.window.rootViewController = viewController;
//LoginViewController.m
[self.navigationController setNavigationBarHidden:NO];
ToDoListViewController *viewController = [[ToDoListViewController alloc] init];
viewController.stringUserId = //userid//;
[self presentViewController:viewController animated:NO completion:nil];
//ToDoListViewController.m
[self.navigationController setNavigationBarHidden:NO];
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
UINavigationItem *right = [[UINavigationItem alloc] initWithTitle:#"Hello!"];
right.leftBarButtonItem = myBarButtonItem;
[self.navigationController.navigationBar pushNavigationItem:right animated:YES];
still, no buttons!
Best way is add NavigationController to your rootViewController and you also able to hide and show NavigationBar by using following code
yourNavigationController.navigationBarHidden:YES/NO;
you can add rootViewController with navigationController by
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginVC];
navCon.navigationBarHidden = YES/NO;
self.window.rootViewController = navCon;
.
.
.

UIViewController presented modally show up shifted to the top

I'm presenting a UIViewController with presentModalViewController:animated.
CMImportViewControlleriPhone *import = [[CMImportViewControlleriPhone alloc] initWithNibName:#"Import-iPhone" bundle:nil];
[import setModalPresentationStyle:UIModalPresentationFormSheet];
[import setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:import animated:YES];
[import release];
However the top bar is not visible, and it is seems shifter to the top (there is an empty space on the bottom).
This is viewDidLoad in which I set the Close button on the navigationItem
- (void)viewDidLoad
{
[super viewDidLoad];
closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(closeButtonPushed:)];
[[self navigationItem] setRightBarButtonItem:closeButton];
[closeButton release];
}
thanks
You should add a navigation bar and then present modalView
CMImportViewControlleriPhone *obj = [[CMImportViewControlleriPhone alloc] initWithNibName:#"Import-iPhone" bundle:nil];
[obj setDelegate:self];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:obj];
[self presentModalViewController:navigationController animated:YES];
[obj release];
[navigationController release];
hope this helps. happy coding :)
If you're working with iPhone remove
[import setModalPresentationStyle:UIModalPresentationFormSheet];
When you add a UIBarButtonItem, the NavigationController is nil, and navigationBar is nil also. So it doesn't work with navigationItem.
closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(closeButtonPushed:)];
[[self navigationItem] setRightBarButtonItem:closeButton];
You should add a NavigationController for the import object, and present it.
CMImportViewControlleriPhone *import = [[CMImportViewControlleriPhone alloc] initWithNibName:#"Import-iPhone" bundle:nil];
[import setModalPresentationStyle:UIModalPresentationFormSheet];
[import setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:import];
[self presentModalViewController:import animated:YES];
[import release];
[nc release];

Can't show popover controller with UIBarButtonItem

i use storyboard for developing UI for my project. There are many issues were solved, but this problem killing me. I have added action for UIBarButtonItem :
- (IBAction)pressAddActionButton:(UIBarButtonItem *)sender {
if (_mode == itemSelect) {
LookUpTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"lookupTable"];
vc.key = #"title";
vc.data = [Linesheet MR_findAllSortedBy:#"title" ascending:YES];
vc.lookUpDelegate = self;
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[self.myPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
} else {
self.mode = itemSelect;
}
}
If i using storyBoard segue for showing popover - all good, but if i do it in runtime a popover does not show. I should manually create UIBarButtonItem.
Thanks for help!!!
Update, code for buttons:
- (void)setupNavigationItems {
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *addItem;
if (_mode == itemSelect) {
addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressAddActionButton:)];
} else {
addItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self
action:#selector(pressDoneButton:)];
}
[addItem setStyle:UIBarButtonItemStyleBordered];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressActionButton:)];
[action setStyle:UIBarButtonItemStyleBordered];
[toolbar setItems:[NSArray arrayWithObjects:separator, addItem, action, nil] animated:YES];
}
Make sure you're hitting the code as you expect using a breakpoint on the presentPopoverFromBarButtonItem line. Also, make sure that the myPopoverController property is declared strong if you're using arc, as otherwise it will be nil before the presenting line.
Did you check if LookUpTableViewController *vc actually gets a proper instance? If not then check that your storyboard does have #"lookupTable" set as your controller's identifier.
The code is right. Try replacing the sender with self.navigationItem.leftBarButtonItem or self.navigationItem.rightBarButtonItem. The sender may not be what you are expecting.
Also remove the if (_mode == itemSelect) clause for testing I'm not sure why you need to access the ivar _mode ivar directly.

UINavigation bar back button not responding

I have a cameraview from which I'm pushing another view of with TableViewController. I can push the second view from my main view by using navbar controller. But the back button from the second view which is table view does not respond. I can't figure out why.
Thanks in advance.
#WrightCS Thank you for your immediate response.
In my app delegate.
ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController :navController];
[self.window makeKeyAndVisible];
return YES;
Right now I'm using a temporary button to push the view.
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
}
There are a few reasons your back button may not work:
You are using a custom UIButton and the frame/bounds are not correct you can use [yourButtonName sizeToFit]
to size it properly.
You are adding a button programmatically and not adding a click event:
[yourButtonName addTarget:self action:#selector(yourButtonNameTapped:) forControlEvents: UIControlEventTouchUpInside];
Don't forget your event.
...
-(void)yourButtonNamedTapped:(id)sender
{
...
}
docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
If you are using a UINavigationController, back will automatically show up when you use [self.navigationController pushViewController:viewControllerName];
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:tableVC animated:YES];
}
I think it will be helpful to you.
- (void)viewDidLoad
{
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidClicked:)];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
-(void)backButtonDidClicked :(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
It should work for you!!

Resources