How to dismiss IOS ViewController from a toolbar added to the view - ios

I have a ViewController that contains a textfield. When the user begins editing the text, I want to display
a ViewController that the user can use to select the font for the textfield. I am using Xcode 5 with
storyboards and compiling for IOS 7. This is where I present the view controller...
- (void)textViewDidBeginEditing:(UITextView *)textView
{
FTViewController *fontSelector = [[FTViewController alloc] init];
[self presentViewController:fontSelector animated:YES completion:nil];
}
I am using a Cocoapods project called FTFontSelector to pick the font. In the FTViewController that gets
presented, I have the following code that runs when the view controller loads. This code adds a UIToolbar
with two buttons, one of which I would like to be used to dismiss the view controller, but all of the
attempts I have made at making this happen have not worked.
- (void)loadView;
{
CGRect frame = [[UIScreen mainScreen] applicationFrame];
self.textView = [[UITextView alloc] initWithFrame:frame];
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.textView.textAlignment = NSTextAlignmentLeft;
self.textView.font = [UIFont systemFontOfSize:FONT_SIZE];
self.textView.text = #"Hello World!";
self.changeFontButton = [[UIBarButtonItem alloc] initWithTitle:#"Font"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(changeFont:)];
self.dismissFontButton = [[UIBarButtonItem alloc] initWithTitle:#"Dismiss"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismissFont:)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)];
toolbar.tintColor = [UIColor lightGrayColor];
toolbar.translucent = YES;
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
toolbar.items = #[self.changeFontButton, self.dismissFontButton];
self.textView.inputAccessoryView = toolbar;
self.view = self.textView;
}
This is the method I call to dismiss the view controller that I need help with. I have commented out
what I have tried.
- (void)dismissFont:(id)sender;
{
[self.textView resignFirstResponder];
// This crashes!
//[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
}
I suspect part of my problem is that there is no navigation controller in my presented view controller,
but when I tried to add one, the text went behind the view (I could see a few pixels of the top of the text),
and there was no back button, so something I tried to do there was wrong.
I am still pretty new to IOS programmng, so there is a lot I still don't understand. I have the rest of
my app working pretty well now, but this last piece is giving me a headache. I am hoping someone here can
show me how to dismiss this view controller.

[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
sends a message to the parent view controller of your view controller. This is clearly not what you want. At best it would dismiss your view controller.
[self dismissViewControllerAnimated:YES completion:NULL];
is what you should do.
From -dismissViewControllerAnimated:completion:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
In this case your view controller (self) is the presenting view controller.

Related

UISearchController search bar appears but not working/triggering

In my view controller I am adding a search bar programmatically and it appears exactly the way i want but when i click on it do nothing i can't write/type anything on the search bar, And there is no error on the log. I added search bar in another view controller same way,it does works but in that particular it doesn't work. I am pushing this view controller from previous view controller so it has a navigation back button on the left (check the image) it works too.
In the view controller there is a TableView and a CollectionView i want to add the search for the table view. I was wondering if there is anything to do with having both of this in the same view controller.
Code to add search bar
- (void)viewDidLoad {
//other stuff on view controller
[self setSearchView];
}
-(void)setSearchView{
//searchController is added as a property on .h file
//and all the delegate are also added
//#property (strong, nonatomic) UISearchController *searchController;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater= self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.navigationController.toolbarHidden=YES;
self.navigationItem.titleView=self.searchController.searchBar;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
}
I am coming to this view controller from a previous view controller and pushing this view controller when a button is clicked .And i am using nib file
Button action
- (void)ButtonClicked {
MYNextViewController *nextViewController = [[MYNextViewController alloc] initWithNibName:#"MYNibFileName" bundle:nil];
// I also have a tab bar on the bottom which i am hiding for that view controller when pushing
nextViewController .hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:nextViewController animated:YES];
}
Image:
Any help will be greatly appreciated. Hope i made my self clear...
Finally i was able to solve this problem with some change in view hierarchy thanks to #Tj3n , but not sure if this is the correct solution but right now its working ..
I also had to remove this line and add few more codes for other purpose
self.definesPresentationContext = YES; //removed...
also answer of that questions was helpful
I had a similar issue and tried the answers here, but it didnt work for me. Then stumbled upon this post https://forums.developer.apple.com/thread/122972
Basically for me disabling Automatically Sync Pasteboard worked. (Simulator -> Edit -> Automatically Sync Pasteboard)
//Please use this code
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)];
_searchBar.tintColor = [UIColor blueColor];
//[_searchBar setImage:[UIImage imageNamed:#"cross_icon"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
_searchBar.placeholder = #"Enter drug name";
//_searchBar.showsBookmarkButton = YES;
_searchBar.barTintColor = [UIColor clearColor];
UITextField *txfSearchField = [_searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor whiteColor];
txfSearchField.tintColor = [UIColor blackColor];
txfSearchField.textColor = [UIColor blackColor];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
_searchBar.showsCancelButton = NO;
[_searchBar becomeFirstResponder];
[self.navigationController.navigationBar addSubview:_searchBar];
For your particular case, Use this.
self.searchController.obscuresBackgroundDuringPresentation = NO;
This will allow you to use the same tableview to interact with search result.

Changing size of modal dialog ios

How to I change the size of modal dialog? Why is this not working?
SlingDialogViewController *slingDialog = [[SlingDialogViewController alloc] init];
slingDialog.modalPresentationStyle = UIModalPresentationFormSheet;
slingDialog.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
slingDialog.view.superview.frame = CGRectMake(0, 0, 900, 700);
[self presentModalViewController:slingDialog animated:YES];
Do this:
SlingDialogViewController *slingDialog = [[SlingDialogViewController alloc] init];
slingDialog.modalPresentationStyle = UIModalPresentationFormSheet;
slingDialog.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:navController animated:YES completion:nil];
slingDialog.view.superview.frame = CGRectMake(0, 0, 900, 700);
slingDialog.view.superview.center = self.view.center;
It's important to call presentViewController:animated:completion first, from my experience at least.
The modal controllers in iOS have fixed sizes. I don't recommend trying to change them, it never works reliably.
A workaround is to implement it by yourself. A modal (presented) controller is a very simple functionality:
A view overlapping your presenting controller, so that it can't be clicked (and it is dimming the contents to make them look backgroundish)
A child view controller (and its view) added to your controller.

UINavigationController as child of UINavigationController

I don't know if the title explains the question itself but here it is ...
I have a UINavigationController which is the parentViewController of a UINavigationController. The thing is the childViewController behaves strange, when I add it as a child it first has the gap for the statusBar (it doesn't occupy the entire screen) and if I "solve" that "bug" by hiding and showing the navigationBar, the gap goes away but now the child doesn't respect the frame I set manually.
Then I tried to continue and when I presented a modal on the child and dismiss it, the entire child goes away ...
What would be wrong there? The parent-child relationship with both containers or what?
Thanks in advice
EDIT: Here's an example project showing the strange behavior
http://www.mediafire.com/?8saa68daqfkf335
EDIT 2: I found a solution actually and I didn't find it really clear on Apple Docs, it says the childViewControllers take its frame from the parentViewController they belong to, but it doesn't say that if the parentViewController "reappears" (like a push on it) the childViewControllers get resized again by the parentViewController frame ... Hope this helps anyone
I believe it would be better to present the second navigation view controller as a modal view controller.
For example, replace your current presentController selector with something like:
- (void)presentController:(id)sender {
ChildViewController1 *cvc = [[ChildViewController1 alloc] initWithNibName:#"ChildViewController1" bundle:nil];
nc3 = [[UINavigationController alloc] initWithRootViewController:cvc];
nc3.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:nc3 animated:YES completion:nil];
UIBarButtonItem *i = [[UIBarButtonItem alloc] initWithTitle:#"X" style:UIBarButtonItemStyleBordered target:self action:#selector(close)];
cvc.navigationItem.leftBarButtonItem = i;
}
Then, your close selector could become:
- (void)close {
[nc3 dismissViewControllerAnimated:YES completion:nil];
}
(though I'd recommend moving the creation of the button and handling the close actually in ChildViewController1.m).
Of course, this would take all the creation of the navigation controller off ViewController.m's viewDidLoad selector:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(0, 100, 100, 40);
[b setTitle:#"present" forState:UIControlStateNormal];
[b addTarget:self action:#selector(presentController:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
}
Hope it works!

How to get new view from clicking barbuttonitem?

I have made a barbuttonitem programmatically on a toolbar overlay. All of this appears when my camera is accessed. The button functions fine and the selector is fully capable of calling it.
Here's the overlay in .m , take a look at the doneButton selector.
- (UIView*)CommomOverlay {
//Both of the 428 values stretch the overlay bottom to top for overlay function. It
doesn't cover it.
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,420)];
UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[FrameImg setImage:[UIImage imageNamed:#"newGraphicOverlay.png"]];
FrameImg.userInteractionEnabled = YES;
UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 428, 320, 50)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
action:#selector(doneButtonPressed)];
[myToolBar setItems:[NSArray arrayWithObjects: cancelButton, flexiSpace2, flexiSpace,
doneButton, nil] animated:YES];
[view addSubview:FrameImg];
[view addSubview:myToolBar];
return view;
}
Here's method I'm using for my void function.
//NewViewController is the nib I want to bring up when done is clicked.
-(void)doneButtonPressed {
NewViewController *UIView = [[NewViewController alloc] initWithNibName:nil bundle:nil];
UIView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:UIView animated:NO];
[UIView release];
NSLog(#"Acknowledged!");
}
The button is being called, but nothing happens. As you can see I added an NSLog function to see whether it was being called or not. "Acknowledged" shows up in the log when the button is clicked, so it's not a problem with the selector. The app doesn't crash when i use the button either, which should occur had I done some heavily faulty coding (in fact I don't even get any warnings about it), as far as Xcode is considered, everything is fine which, needless to say isn't.
I also have my .h files of NewViewController in place correctly in the view controller in which this code is being implemented.
UIView is the name of a class. Don't use it as the name of a variable. At best it is hugely confusing and is a probable reason for your problems. Rename UIView to something like myView everywhere in doneButtonPressed.

Modal window not being dismissed

I have two programmatically created buttons you can see in my viewDidLoad method. On the modal window I have a button that calls the cancelSearch method via a delegate. When I place a breakpoint on my cancelSearch method it is hit, so I know my delegate is set up correct, but even though it calls this line [self dismissViewControllerAnimated:YES completion:nil]; it's not actually closing the modal window.
The code below is all from my main controller view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showActionMenu:)];
actionButton.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(showSearchMenu:)];
searchButtonItem.style = UIBarButtonItemStyleBordered;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
NSArray* buttons = [NSArray arrayWithObjects:actionButton, searchButtonItem, nil];
[toolbar setItems:buttons animated:NO];
self.navigationItem.title = #"Census Management";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[[RKClient sharedClient] get:#"censusmanagement" delegate:self];
}
- (IBAction)showActionMenu:(id)sender
{
[self performSegueWithIdentifier: #"CMActionSegue" sender: self];
}
- (IBAction)showSearchMenu:(id)sender
{
ehrxCMSearchView *search = [[self storyboard] instantiateViewControllerWithIdentifier:#"cmSearch"];
search.selectedOptions = self.selectedOptions;
search.delegate = self;
[self.navigationController pushViewController:search animated:YES];
}
- (void)cancelSearch:(ehrxCMSearchView *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
You would dismiss a modal view using something similar to:
[self dismissModalViewControllerAnimated:YES];
This will dismiss the modal view which was loaded using something similar to:
[self presentModalViewController:search animated:YES];
However looking at your code snippet, it appears the search view controller is being pushed onto the navigation stack using the following line:
[self.navigationController pushViewController:search animated:YES];
So I you probably need to pop the view from the navigation stack rather than trying to dismiss it as a modal view:
[self.navigationController popViewControllerAnimated:YES];
If your view controller is modally presented, you should use this:
[self.presentingViewController dismissModalViewControllerAnimated:YES];
The presentingViewController property is available in iOS 5 only. So, if you're targeting older versions of iOS, you have to use self.parentViewController instead (use the appropriate one for each iOS version, you have to handle this).
If you make this control in your parent/presenting view controller, then just call this:
[self dismissModalViewControllerAnimated:YES];

Resources