The behaviour different between automatic presentation UISearchController and present the search controller myself.
#implementation MyViewComtroller
// click search barbutton on right of navigationBar
- (void)searchAction:(id)sender {
ArticleSearchViewController *searchResultsController = [[ArticleSearchViewController alloc] init];
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
searchController.searchResultsUpdater = searchResultsController;
searchController.searchBar.delegate = searchResultsController;
searchController.delegate = searchResultsController;
searchController.hidesNavigationBarDuringPresentation = NO;
[self presentViewController:searchController animated:YES completion:nil];
}
#end
#implementation ArticleSearchViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ArticleDetailController * articleDetailController = [ArticleDetailController new];
[(UINavigationController *)self.presentingViewController pushViewController:articleDetailController animated:YES];
}
#end
The push action happend below UISearchController.
You are presenting the search controller in wrong way. Look at part of the documentation.
You use a search controller in tandem with your existing view
controllers. When you have a view controller with searchable content,
incorporate the search bar of a UISearchController object into your
view controller’s interface. When the user interacts with that search
bar, the search controller automatically displays a new view
controller with the search results that you specify.
Instead of this in your main controller:
[self presentViewController:searchController animated:YES completion:nil];
You should do this on iOS 11:
self.definesPresentationContext = YES;
self.navigationItem.searchController = searchController;
Or this on pre iOS 11:
self.definesPresentationContext = YES;
self.navigationItem.titleView = searchController.searchBar;
Or place anywhere else the searchController.searchBar in your presented view hierarchy.
You should also setup the search controller in a place like viewDidLoad and not on a button press.
And the way you are getting a reference to your navigationController in ArticleSearchViewController has to be updated to something like:
[self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES];
View controller with searchable content:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ArticleSearchViewController *searchResultsController = [sb instantiateViewControllerWithIdentifier:#"ArticleSearchViewController"];
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
searchController.searchResultsUpdater = searchResultsController;
searchController.searchBar.delegate = searchResultsController;
searchController.delegate = searchResultsController;
searchController.hidesNavigationBarDuringPresentation = NO;
// [self presentViewController:searchController animated:YES completion:nil]; //WRONG
self.definesPresentationContext = YES;
self.navigationItem.searchController = searchController;
}
#end
Results Controller:
#implementation ArticleSearchViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController * articleDetailController = [UIViewController new];
[self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// update appropriately
}
#end
This is a better answer:
definesPresentationContext = true
just write this in your MyViewController
Related
I am new to iOS programming.
I have to implement a location search based on the third party map API, but I think I have messed up multiple view controllers.
Scenario:
I have a mapviewController for displaying the mapview. And I have a searchController for searchbar. There is a resultviewController property in searchController for display the search suggestions.
I have tried to add search VC as child VC to map VC, but once I click to the search bar it returns "Application tried to present modally an active controller".
So then I remove search VC from map VC & the search bar is not activated.
Therefore I want to ask how to handle these VCs, and avoid the case that map view covers the search bar and suggestion lists.
Thank you so much.
UPDATE:
I'm building a search bar in code. My aim is to build the following style in iOS.
like this http://i1.tietuku.com/cd4efa23d97af75ft.jpg
I pushed the map viewController to the navigation controller from menu.
[self.navigationController pushViewController:mapviewController animated:YES];
And trying to add a searchController to it.
- (void)viewDidLoad {
[super viewDidLoad];
_resultsTableController = [[UITableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_resultsTableController.tableView.tableHeaderView = _searchController.searchBar;
self.searchController.searchBar.delegate = self;
_resultsTableController.tableView.delegate = self;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, kHeight(100), kScreen_Width , kScreen_Height-kHeight(100))];
[self addChildViewController:_searchController];
[self.view addSubview: _searchController.view];
[self.view addSubview:mapView];
}
for UISearchController you no need to add it as child view controller and and instant of using UITableViewController uses UITableView it make easy for you and
here below is code that is .m file
free to run that code and without uses storyboard.
#interface ViewController ()
{
UITableView *tableview;
UISearchController *searchcontroller;
NSMutableArray *array;}
#end
#implementation ViewController
(void)viewDidLoad
{
[super viewDidLoad];
array = [[NSMutableArray alloc]initWithObjects:#"one",#"two",#"three",#"four", nil];
tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];
[self.view addSubview:tableview];
tableview.dataSource = self;
tableview.delegate =self;
searchcontroller = [[UISearchController alloc]initWithSearchResultsController:nil]; // Create Reference of searchController
searchcontroller.searchResultsUpdater = self;
searchcontroller.searchBar.backgroundColor = [UIColor redColor];
searchcontroller.searchBar.barStyle = UIBarStyleBlack;
searchcontroller.searchBar.barTintColor = [UIColor whiteColor];
searchcontroller.dimsBackgroundDuringPresentation = false;
[searchcontroller.searchBar sizeToFit];
tableview.tableHeaderView = searchcontroller.searchBar;
MKMapView *mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(tableview.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2)];
[self.view addSubview:mapview];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
{
NSLog(#"update ");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// Default is 1 if not implemented
{
return 1;
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
cell.textLabel.text = array[indexPath.row];
return cell;
}
According to me you should use only one view controller which has a map on it. Over the map you can simply add custom search view and on typing/ taping search you can show search results using UITableview with some dropping animation keeping some max frame fixed, And finally on taping any result remove this UITableView and show location of map. If time permits I will create a sample for you, cant give assurance but will try. Mean while you can also go through Maps reference may be helpful
I'm presenting a modal view controller using following code,
[[mInfo objectForKey: kNavigationController] presentViewController:(UIViewController*)modalViewControlr animated:YES completion:nil];
UITableView. On selecting a table view cell I want to navigate it to another UIView called navigatedView.
Is this can be done by embedding self (i.e,modalViewControlr) in navigation Controller and add view (i.e, navigatedView) to a view controller and present it?
for example,
// in modalViewControlr
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:self];
UIViewController *vc = [[UIViewController alloc]init];
[vc.view addSubview:self.navigatedView];
self.navigatedView.frame=vc.view.frame;
[passcodeNavigationController pushViewController: vc animated:YES];
//
}
Please help....
I prefer do it in this way which is easier to understand.
1.Embed the navigationController in AppDelegate:
ModalViewControlr *vc = [[ModalViewControlr alloc]init];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = passcodeNavigationController;
2.Create a new class NavigatedViewControllerand custom the controller in viewDidLoad
Create an UIView call navigatedView
#property (nonatomic, strong) UIView *navigatedView;
-(void)viewDidLoad
{
[super viewDidLoad];
_navigatedView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.navigatedView.backgroundColor = [UIColor redColor];
[self.view addSubView:self.navigatedView];
}
3.Then return to modalViewControlr where you want to push from.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NavigatedViewController *vc = [[NavigatedViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
I created a search bar at the header view of a table:
self.searchResults = [NSMutableArray arrayWithCapacity:[self.list count]];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor clearColor];
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[self.searchController.searchBar becomeFirstResponder];
And when tap a certain row in the tableview, a new view controller will be pushed.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"HEY ROW %ld", indexPath.row+1);
ZLContainerViewController *controller = [[ZLContainerViewController alloc]init]; // a container for UIPage
[self.navigationController pushViewController:controller animated:YES];
}
However, after pushing the new view controller, the search bar is still there. How can I solve this problem?
Any suggestion is much appreciated!
Use the below code in viewDidLoad.
Swift:
self.definesPresentationContext = true
Objective-C:
self.definesPresentationContext = YES;
This solved the problem for me.
One of these two UISearchBarDelegate delegate methods should fix your problem
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchBar resignFirstResponder];
}
First of all, sorry for my English.
I'm trying to do an app for iPad with a navigation controller which pushes view controllers when a button "Next" is selected. But I also want to have a popover, called from a button in the nav bar, that allows the user to "jump" from one view controller to another, pushing it with the tableView:didSelectRowAtIndexPath: and the pushViewController:animated: methods, but it's not working.
Summary:
Tab bar -> switches between FirstViewController and SecondViewController (works just fine)
Nav bar (button Next) -> switches between SecondViewController, FirstSlideController and SecondSlideController (it's also good)
Popover -> user selects SecondViewController, FirstSlideController or SecondSlideController (here's the problem!)
Codes:
AppDelegate
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navigationController1, navigationController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
TableViewController(popover)'s didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0){
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:#"FirstSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else if(indexPath.row == 1){
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:#"SecondSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
}
SecondViewController (with delegate suggested by maros)
-(void) showPopover:(id) sender
{
TableViewController *PopoverView = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
self.popOver = [[UIPopoverController alloc] initWithContentViewController:PopoverView];
self.popOver.delegate = self;
[self.popOver presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections: UIPopoverArrowDirectionUp animated: YES];
}
I tried to print self.navigationController and it says it's null. I'd appreciate any help.
Thank you.
Presnented UIPopoverController is not pushed on the navigation stack in the view controller from which it was presented. It is a separate view controller.
Therefore the navigationController inside the popover is nil.
What I would recommend you is to create a delegate MyNavigationPopoverDelegate (the class that creates a popover (PopoverController). Pass it's instance as an delegate to the TableViewController.
After users clicks on some button inside popover, call delegate's method to processes button clicks (myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex).
Then maybe dismiss delegate?
and finally change navigation however you want! :)
#protocol MyNavigationPopoverDelegate
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
#interface TableViewController : UITableVieController // your viewController in popover
... // your code
#property (nonatomic, weak) NSObject <MyNavigationPopoverDelegate> * delegate;
... // your code
#end
#implementation TableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate myNavigationPopover:self clickedButtonAtIndex:indexPath.row];
}
...
#end
// defines that SecondViewController implements the delegate's method
#interface SecondViewController <MyNavigationPopoverDelegate> : UIViewController
// your code
#end
// code where you presenting popover
#implementation SecondViewController
// This is the method that is executed after your button press and it is responsible for presenting a popover
- (void) presentPopover{
...
myPopover.delegate = self; // setting the delegate
[myPopover presentPopoverFromXXX ...]; // however you present it
...
}
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex
{
UINavigationController *currentNavigationController = ; // get the navigation controller from the tab bar
if(buttonIndex == 0){
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:#"FirstSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
else if(buttonIndex == 1){
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:#"SecondSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
else{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
}
#end;
I have a table:
- (void)viewWillAppear:(BOOL)animated
{
self.searchBar.delegate=self;
self.searchResultTable.delegate=self;
self.searchResultTable.dataSource=self;
[self.searchResultTable setHidden:YES];
}
In my cells I insert text and when I click on cell I need go to another table but this not work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewController *tvc=[[TableViewController alloc]initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:tvc animated:YES];
}
Assuming you are doing something like this to present this controller:
SearchViewController *searchViewController = [SearchViewController alloc] init];
[self presentModalViewController:searchViewController animated:YES];
then your view controller doesn't have a navigation controller
You need to change it to
SearchViewController *searchViewController = [SearchViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:searchViewController];
[self presentModalViewController:navController animated:YES];
then [self.navigationController pushViewController:tvc animated:YES]; will work