UISearchbar inside uinavigationcontroller - iphone

Looks like dumb question, but...:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 20.0, self.navigationController.view.bounds.size.width, HEADER_HEIGHT)];
mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:bar contentsController:self];
mySearchDisplayController.delegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.searchResultsDelegate = self;
[self.navigationController.view addSubview:self.bar];
All behavior is working fine while i don't press a cancel or hided tableview.
After that search bar is closed and i see a clear navigation bar, without search bar.
I has change to viewDidload, but nothing changed.
Before start printing:
after:

Try to add searchbar as titleView for navigationBar. It may work.
Refer to this site:
http://iphonesdevsdk.blogspot.com/2011/04/custom-tableview.html
It may helps you

Related

is there a way to get the standard back button within the navigation bar in this setting?

I have a tab bar based app.
But from one screen the user may go to a more detailed view which does not have the tab bar.
The detailed screen has a navigation bar at the top and a simple button on the right as part of the navigation bar which works. ( I have created the navigation bar and the right button within Bar Button Item in IB and attached it to the outlets)
However since I have to go back from this screen I like to add the STANDARD back button using the standard navigation bar. (I could add it manually in IB, but I do not have the standard image readily available and thought using the standard would be "smarter")
This is where I create the detail view (which is SettingsVC2) within SettingsVC1 :
SettingsVC2 *settingsVC2 = [[SettingsVC2 alloc] initWithNibName:#"SettingsVC2" bundle:nil selectedTCNumber:tcNumber];
settingsVC2.delegate = self;
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = #"Back";
settingsVC2.navigationItem.backBarButtonItem = temporaryBarButtonItem;
settingsVC2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:settingsVC2 animated:YES completion:nil];
While the navigation bar itself appears in SettingsVC2 no back button appears in it.
This is the code within SettingsVC2 itself:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selectedNumber: (NSInteger) numberx
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:#"settings"];
self.view.backgroundColor = [UIColor whiteColor];
self.tableView.opaque = TRUE;
navBar.tintColor = [UIColor colorFromRGBIntegers:W_COLOR_R green:W_COLOR_G blue:W_COLOR_B alpha:W_COLOR_A];
navBar.topItem.title = NSLocalizedString(#"Settings2", #"Settings2");
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = #"Back";
/*
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[self.navigationItem.backBarButtonItem setEnabled:YES];
[self.navigationItem.backBarButtonItem setStyle:UIBarButtonItemStyleDone];
*/
navBar.topItem.backBarButtonItem = temporaryBarButtonItem;
}
return self;
}
Neither this code nor the code commented out makes the back button appear.
Is there a "standard" way to get the back button (without using a UINavigationController in my case) or do I have to just add it manually in the IB and get the appropriate background image?
Many thanks!
You have to add the backbuttonItem to the first view, not the second one. This if course is very confusing. Anyway, I use these two methods to set and clear that back button title:
- (void)defaultBackButtonTitle
{
UIBarButtonItem *temporaryBarButtonItem = [UIBarButtonItem new];
[temporaryBarButtonItem setTitle:#"Back"];
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
}
- (void)clearBackButtonTitle
{
self.navigationItem.backBarButtonItem = nil;
}
You call the first when you push the viewController you want to have the back button, and the second when you get viewWillAppear (in the first top view controller).

Adding a searchbar above a UITableview

How is a searchbar added above a UITableView? Would just the searchbar be enough or would the search bar and search display be chosen?
I want to redraw the tableview with the items that are found by the search.
UISearchDisplayController is specifically designed to solve your problem. You should be able to figure out how to use it from reading the documentation.
Of course you could just handle the UISearchBar (or even UITextField if you want to build your own search bar) yourself.
Here's some code to get you started:
- (void)viewDidLoad {
[super viewDidLoad];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;
}
Pretty much all there's left to do is to implement the delegates. If you need any help with that let me know, but may I suggest you ask a new question for each problem you encounter a long the way. Of course if you leave a comment here I will take a look at it.

SearchBar in table view with searchDisplayController programmatically no edit

I'm trying to create a tableview with a searchbar inside the header view of the table. I'd like to use a searchDisplayController to manage everything.
I've created everything programmatically (I'm not feeling comfortable with IB) trying to set all the correct properties, but it seems that I'm missing something, because when the table shows up I'm not able to edit the text in the searchbar and see any animation.
Here is a part of the code:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBarTMP=[[UISearchBar alloc]init];
self.searchBar=searchBarTMP;
[searchBarTMP release];
self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
self.searchBar.delegate=self;
self.searchBar.showsScopeBar=YES;
self.searchBar.keyboardType=UIKeyboardTypeDefault;
self.searchBar.userInteractionEnabled=YES;
self.searchBar.multipleTouchEnabled=YES;
self.searchBar.scopeButtonTitles=[NSArray arrayWithObjects:NSLocalizedString(#"City",#"Scope City"),NSLocalizedString(#"Postal Code",#"Scope PostalCode"),nil];
self.tableView.tableHeaderView=searchBar;
self.searchBar.selectedScopeButtonIndex=0;
self.navigationItem.title=NSLocalizedString(#"Store",#"Table title");
//SearchDisplayController creation
UISearchDisplayController *searchDisplayControllerTMP = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController=searchDisplayControllerTMP;
[searchDisplayControllerTMP release];
self.searchDisplayController.delegate=self;
self.searchDisplayController.searchResultsDelegate=self;
self.searchDisplayController.searchResultsDataSource=self;
//....continue
}
I know that when you use a searchbar alone you must deal with its delegate protocol, but I'm guessing that the searchDisplayController manage for you as seen in the Apple sample code. (build up with IB).
Any suggestion?
Thank you,
Andrea
Found it...
After putting in the header of the table view must write
[self.searchBar sizeToFit];
If you are using ARC, make sure you create an iVar for the UISearchDisplayController in your header file.
If you create an UISearchDisplayController using:
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
it will get released by ARC, it will not call any delegate methods and when you'll call self.searchDisplayController (the UIViewController's property) it will be nil.
So, the fix is:
In your header (.h) file:
#interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
UISearchDisplayController* searchDisplayController;
UISearchBar *searchField;
UITableView* tableView;
NSArray* searchResults;
}
and in the implementation (.m) file:
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
searchField.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
tableView.tableHeaderView = searchField;
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);
When implemented like that, you can call both self.searchDisplayController and searchDisplayController in the rest of your code.

How to remove a navigationController (iPhone)

My app has couple of normal views (V1, V2), without a navigationController, but when and ADD button is pushed creates a view with a navigationController like this:
CreateNewEventViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
tableViewController = [[NewEventTableViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
tableViewController.navigationController.title = #"Add";
[self.view addSubview:navigationController.view];
}
so the NewEventTableViewController is an UITableViewController. When done filling the data from the table, last cell is a button to save it and then go back to one of the main views (V1, V2).
NewEventTableViewController.m
V1 *myV1 = [[V1 alloc] init];
[self.view.superview addSubview:myV1.view];
but the navigationController I had stays. Any way of removing it?
update
I've tried this, but all I get is a white screen. Also removing after adding the new view. (this code is placed in NewEventTableViewController)
[self.navigationController.view removeFromSuperview];
V1 *myV1 = [[V1 alloc] init];
[self.view addSubview:myV1.view];
update 2
Is not what I really needed but anyway I can use this.
[self.navigationController.view.superview removeFromSuperview];
it removes the navigationController and it's table view and it displays the view I had before I call the CreateNewEventViewController.
Not much sure why you wanna do that, but there is a possibility to hide navigationController
self.navigationController.navigationBarHidden = YES;

Gray UISearchBar w/matching scope bar programmatically

I'm trying to recreate this UISearchBar (as seen in the Table Search example code):
alt text http://img168.imageshack.us/img168/6378/43558113.png
All the examples I've seen to do this involve using a xib, however I need to do it programmatically. The problem is changing the tint color also changes the cancel button's tint:
alt text http://img243.imageshack.us/img243/1375/screenshot20100527at944.png
Any ideas?
Associating the search bar with a UISearchDisplayController magically provides a lot of standard look and behavior such as:
gray tint without affecting cancel button
auto showing/hiding of cancel button
width adjustment around any tableview indexes
In my tableview controller I've done the following:
- (void)viewDidLoad {
[super viewDidLoad];
// setup searchBar and searchDisplayController
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
searchBar.placeholder = #"Search";
self.tableView.tableHeaderView = searchBar;
UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
// The above assigns self.searchDisplayController, but without retaining.
// Force the read-only property to be set and retained.
[self performSelector:#selector(setSearchDisplayController:) withObject:searchDC];
searchDC.delegate = self;
searchDC.searchResultsDataSource = self;
searchDC.searchResultsDelegate = self;
[searchBar release];
[searchDC release];
}
I totally agree with Scott McCammon.
However using a performSelector:withObject: on setSearchDisplayController: would not be my approach. This depends on private API which can change at any moment. If Apple would remove their private implementation your app will crash.
A better way would be to override the searchDisplayController: in your view controller to return your instance of UISearchDisplayController:
- (UISearchDisplayControlelr *) searchDisplayController {
return yourInstanceOfASearchController;
}
I don't understand the need for the call to setSearchDisplayController: or the override for searchDisplayController. Under iOS 4.3.2 initWithSearchBar:contentsController: appears to set searchDisplayController for the UIViewController instance passed as the contentsController argument. Perhaps this was a problem in earlier iOS releases, but it appears redundant in the current release.