UISearchBar Not appearing - cocoa-touch

I have view controller, into the view i have put a table view, and a search bar into the table's header... the search bar is not showing up, just the empty table view.
Do i need to do something additional? I'm pretty sure its to do with the view outlet of the UIViewController, set to View...
Thanks

For anyone else who may be landing on this question, I had a very similar situation where a UITableViewController with a UISearchBar added to it wasn't displaying. If you find yourself in this situation, double check that you are actually calling:
initWithNibName:#"MyNibName" bundle:nil
to init your view controller instead of the common Table View init of:
initWithStyle:UITableViewStylePlain
I was foolishly adding the search bar to the Nib, and then loading it with the style init (which skips the Nib entirely and loads the table view from scratch)

try setting the tableHeaderView of your tableView to your searchBar.
self.tableView.tableHeaderView = searchBar;
if you are using IB, be sure to connect the outlets so that it gets referenced.

Try this,l it worked for me:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tableView.tableHeaderView = _searchBar;
[_searchBar becomeFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[_searchBar resignFirstResponder];
self.tableView.tableHeaderView = _searchBar;
}
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
searchBar.frame = CGRectMake(0,MAX(0,scrollView.contentOffset.y),320,44);
}

If you set your view controller as a part of tabbar controller, besides "Class" property in IB set the "NIB name" property. It takes me several hours to figure it out.

Fixed using a UISearchBar with controller.

Related

Xcode Totally dismiss ViewController

I have a very simple question:
How can I "dismiss" a ViewController completely?
After this I want the ViewController to load like if it had been the first time I load the ViewController.
How could I do this? I already tried setNeedsDisplay() but thats not what I want.
Thanks
Jannes
I think what you mean is to remove the view (not the view controller) That way, when you enter the main view, it will run viewDidLoad again. I have done something similar in a view controller within a navigation controller. When I click the 'back' button, I wanted to remove the view and force it to load again the next time I enter it. I was able to do that with this code:
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove)
{
NSLog(#"removing view %#", v);
[v removeFromSuperview];
}

IBAction Subview with Storyboard

I'm trying to add a subview using storyboard. It's being displayed correctly, but there's no button (IBAction) in subview that works correctly, even with empty code, the app always crashes when clicked.
TableViewController.m
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Load SubView
SubviewViewController *subview = [self.storyboard instantiateViewControllerWithIdentifier:#"SubviewViewController"];
[self.view addSubview:subview.view];
}
...
SubviewViewController.m
- (IBAction)btnCloseSubview:(id)sender
{
// Crashes the app when clicked, even empty as it's.
}
You shouldn't just add another view controller's view to your view like that, it can get you into trouble. When I've tried that, it sometimes works and sometimes not. The proper way is to also add the controller as a child view controller of TableViewController:
SubviewViewController *subview = [self.storyboard instantiateViewControllerWithIdentifier:#"SubviewViewController"];
[self addChildViewController: subview];
[subview didMoveToParentViewController:self];
[self.view addSubview:subview.view];
You probably need to set the frame of subview's view as well (subview.view.frame = self.view.bounds if you want it to be the same size).
As an alternative, you could create a view, not a view controller, in a xib file, and add that as a subview of your view. In that case, you would set the file's owner of the xib to TableViewController, and put the button's method in TableViewController.
Look into UIContainerView, it handles sub-view controllers. You will need to handle your own communication between parent and child view controllers, but other than that it will work like you need it to for a sub-view, as long as your sub-view has the proper outlets and actions set up in the storyboard with it's view controller.
Have a look for more in-depth information on container views.

View controller unset his tag when pushed into nevigation controller

... I guess.
This is the situation:
here a simple method, in a view controller, where we can push a botton to go in another view:
- (IBAction)actionNext:(UIButton *)sender {
self.numeriUtiliListaViewController = [[ALCNumeriUtiliListaViewController alloc] init];
[self.numeriUtiliListaViewController.view setTag:[sender tag]];
[self.delegate vai:self.numeriUtiliListaViewController title:#"Numeri Utili"];
}
the delegate's method is:
- (void)vai:(id)view title:(NSString *)title
{
ALCParentViewController *viewController = (ALCParentViewController *)view;
viewController.delegate = self;
NSLog(#"tag: %d",[viewController.view tag]);
[self.myNavigationController pushViewController:viewController animated:YES];
}
This system works well, but the only thing is the tag that i've logged in this last method, here it was print correctly, but in the view loaded by the navigation controller, when i try to catch the value in the viewDidLoad, it's 0.
Any ideas?
Thanks in advance
Update 1 2013-01-30:
if i try to print the the tag in the viewWilAppear method of the viewcontroller pushed in the navigationcontroller, i'll give the right value... why? i don't know
Whats happening here is that in your actionNext method, when you are setting the tag of the viewController's view using : self.numeriUtiliListaViewController.view, as soon as you access the view propert of the viewController, viewDidLoad method is called in the viewController. So even before the setTag function is executed, viewDidLoad has already been executed, and it is showing tag = 0. But your viewWillAppear/viewDidAppear methods will be called only when the actual view appears and by then setTag has been executed, so it shows correct value.
Makes sense? Hope this helps

UITableView delegate not called when UIViewController is popped

I have a view controller that contains a UITableView. When the view controller gets pushed onto the stack, the table view delegate methods get called and it populates the table.
Then I push another view controller onto the one that contains the table view. What I would like to make happen is that - when the second view controller gets popped and I return to the previous view controller, those uitableview delegate methods get called again (so as to repopulate the table).
In other words, how do you repopulate a uitableview when popping to a view controller.
Thanks
In viewDidLoad or viewDidUnload, viewWillAppear, viewWillDisappear (whichever of these is right for your situation) you can put [myTable reloadData];:
// If you can include some code it would help as I am a bit uncertain
// about exactly what you are trying to do from the question but
// you should use whichever of these is correct for project:
- (void)viewDidLoad {
[super viewDidLoad];
[myTable reloadData];
}
- (void)viewDidUnload {
[myTable reloadData];
}
- (void)viewWillAppear {
[super viewWillAppear];
[myTable reloadData];
}
- (void)viewWillDisappear {
[super viewWillDisappear];
[myTable reloadData];
}
What you should do is add a [self.tableView reloadData] (or whatever your table variable is) call inside the viewWillAppear method of its view controller. This will cause the table view to be reloaded both when being pushed (as it does now) and when other view controllers are popped to reveal it.

-(void)viewWillAppear:(BOOL)animated doesn't called

My application is view based in my app in one view i write view will appear but this method doesn't call. My code is
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"view will Appear");
[tableView reloadData];
}
Can anyone tell why viewWillAppear method not called.
Sorry I forgot To tell You That This method call first time but when i remove a subview from this view viewWillAppear not called. Please Suggest me how to solve this problem.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(#"view will Appear");
[tableView reloadData];
}
if still it is not calling then try to call through code , as per example [classobj viewWillAppear:NO];
The viewWillAppear and other related methods are called to the viewcontroller that are linked with the rootViewController of the mainWindow.
So if you are using a view based application, the viewWillAppear method of the first view controller will work properly and others wont.
I think that the problem you are seeing is that viewWillAppear: is a method on a UIViewController, and not a method on UIView. viewWillAppear: is called on the view controller to indicate that the controller's view will become visible.
If you have added the code above to a class based on UIView, that code won't get called. You need to move that code to your view controller -or- you might achieve the result that you are looking for by implementing the didMoveToSuperview method in your UIView based class instead.
didMoveToSuperview will be called on your view when your view is added to another view using addSubview:.
Hi Here I did instead
[self.view addSubview:viewcontroller.view];
I Used this:
[self presentModalViewController:viewcontroller animated:YES];
and write this method now my problem is solved.
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"==========view will appear");
}