How can external UITableView delegate hide the UISearchBar keyboard? - iphone

I have a table view with a data source/delegate in another file. In addition, there is a search bar above the table view that belongs to the first file. In other to hide the keyboard when scrolling, I would need to call:
[self.searchBar resignFirstResponder]
But the
(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
method is in the delegate. So how would I hide the keyboard when scrolling in this case?
Thanks!

you could send a notification in scrollviewwillbegindragging. tableview delegate:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[[NSNotificationCenter defaultCenter] postNotificationName:#"resign" object:nil];
}
searchbar delegate:
-(void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(goTo:) name:#"resign" object:nil];
}
-(void)goTo:(NSNotification*)notification {
[self.searchBar resignFirstResponder];
}

There are many ways to do,a couple of them are below.
option 1:
add below line after initializing your table object
[yourTableView setKeyboardDismissMode:UIScrollViewKeyboardDismissModeOnDrag];
or
option 2:
Get your tableview's superview(i'm expecting that as aViewcontrollerObj.view) and forcibly end its editing .
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
UIView *tableviewSuperView = yourTableView.superview;
[tableviewSuperView endEditing:true];
}
Hope that helps
Happy coding :)

Related

NSNotificationCenter postNotificationName exec_badaccess

I have a view controller, when it's dissming with completion, I post a notfication, and in a subview which contained in another view controller, has added as a oberserve. But, when it tries to execute post notificaiton methode, exec_bad_access happend. what's wrong? The codes are:
BrandListByIdViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *bid = self.brands[indexPath.row][#"id"];
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"SelectedBrandId" object:nil];
}];
}
SearchNewProduct.h
#interface SearchNewProduct : UIView
#end
SearchNewProduct.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didSelectedBrandId::) name:#"SelectedBrandId" object:nil];
}
}
- (void)didSelectedBrandId:(NSNotification *)notif{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
Even I get rid of the userInfo, still bad access. I created a similar situation in another new project, it works fine.
I didn't realize that you were dealing with a UIView and not a UIViewController (should have read your question better). I think what is happening is that the View is receiving notifications even after being released. Make sure to call dealloc in your UIView and remove itself as an observer:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Also, put an NSLog in that UIView's initWithFrame method to see if it is being called more than once.
This question is very similar:
ios notifications to "dead" objects
Not sure if this is the reason, but when you add your view to the notification center, your selector is wrong:
selector:#selector(didSelectedBrandId::)
There should only be one colon. The entire line should be:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didSelectedBrandId:) name:#"SelectedBrandId" object:nil];
Two colons indicates the method takes two arguments, but it only takes one.

Call [tableView reloadData]; on a viewController from a modalViewController

I have a modalViewController that comes up over the top of a viewController with a tableView. When the user clicks a button on the modalViewController I want to reload the tableView within the viewController with this:
[tableView1 reloadData];
I do not want to put the reload in the viewDidAppear or viewWillAppear methods as they get called when i do not need the tableView to reload (i.e. when the user clicks the back button to return to the tableView).
Is there a way to do this?
Try
1) write one method which reloads the table data.
2) Call it on the back button clicked.
This is the classic delegate pattern problem, in your modal view controller you need a delegate reference to the current view controller presenting it
//Modal
#protocol ModalVCDelegate
- (void)tappedBackButton;
#end
#class ModalVC: UIViewController
#property id<ModalVCDelegate> delegate;
#end
#implementation
- (void)backButtonTapped:(id)sender
{
if (self.delegate)
[self.delegate tappedBackButton];
}
#end
Now, in your presenting VC, just process this delegate message
//Parent VC
- (void)showModal
{
ModalVC *vc = [ModalVC new];
vc.delegate = self;
//push
}
- (void)tappedBackButton
{
[self.tableView reloadData];
//close modal
}
You can use delegate . If find it more harder then alternative is to use NSNotificationCenter. You can see accepted answer for Refreshing TableView. This is really very short, easy and understandable way.
using Notification like bellow Method:-
Create NSNotificationCenter at yourViewController's ViewdidLoad Mehod
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ReloadDataFunction:)
name:#"refresh"
object:nil];
[super viewDidLoad];
}
-(void)ReloadDataFunction:(NSNotification *)notification {
[yourTableView reloadData];
}
Now you can Call this Notification from your modelViewController BackButton or else you want from calling this Refresh notification like putting this line of code:-
[[NSNotificationCenter defaultCenter] postNotificationName:#"refresh" object:self];
NOTE: postNotificationName:#"refresh" this is a key of particular Notification
Try to use this one
Make a Button and click on this button and than you can reload your data.
This button make custom and use it on background.
- (IBAction)reloadData:(id)sender
{
[tblView reloadData];
}
You can use NSNotification to refresh table on ViewController.
Inside viewController :
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
Write code in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadMainTable:)
name:#"ReloadTable"
object:nil];
- (void) reloadMainTable:(NSNotification *) notification
{
[tableView reload];
}
Inside ModelViewController:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"ReloadTable"
object:nil];
Here you can also send custom object instead of nil parameter. But be care full about removal of NSNotification observer.

How would I go about reloading a view controller's table view the page curl on another view controller is closed.

How would I go about reloading a view controller's table view after the page curl on another view controller is closed.
I have a tableview on viewController1 with a transistion using a page curl to viewController2.
viewController1 has a tableView that needs to be reloaded after the page curl is closed.
thanks for any help
Ok,
in your viewControllers1 register a notification in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"MyNotification" object:nil];
and add the method:
- (void)handleNotification:(NSNotification*)note {
[tableView reloadData];
}
in your viewController2 post the notification when you will go to viewController1 or when ever you want to reload the table:
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:self];
In viewController1's viewWillAppear method, you can call the table view's reloadData method.
In viewController1's
- (void)viewDidDisappear:(BOOL)animated
{
[tableView reloadData];
}
tableView has to be a member.
In first viewcontoller.h put this in viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handle_data) name:#"reload_data" object:nil];
-(void)handle_data
{
[yourtableview reloaddata];
}
In second viewcontroller.h
[[NSNotificationCenter defaultCenter] postNotificationName:#"reload_data" object:self];

Dismiss UIActionSheet before device rotate?

In portaint Actionsheet show in tabbar. In landscape - in window. I heed do remove UIActionSheet from tabbar before rotate, for after rotate show in wondow.
Inside – willRotateToInterfaceOrientation:duration dismiss action sheet dont work.
You can use NSNotification for this. Add this notifier in your viewdidload file you willl get notify when orientation is going to change:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(OrientationChanged:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
…add this method:
-(void)OrientationChanged
{
}
See NSNotification class reference.
Me i dismiss before rotation and show after, then the UIActionSheet is replaced at middle;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[APP.validSave dismissWithClickedButtonIndex:1 animated:NO];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[APP.validSave showInView:APP.navController.view];
}

How to refresh UITableView after app comes becomes active again?

I would like my UITableView to reloadData once my app is active again, after a user exits the application. I know I need to implement (in my app delegate):
- (void)applicationDidBecomeActive:(UIApplication *)application
but im not sure how to reference the current UITableView?
UPDATE:
My UITableView is a separate controller. It is actually presented as follows
AppDelegate > Root View Controller > Pushes UITabBarController modally which has a UITableViewController
following up on Ole's answer above
add this when initializing the viewcontroller
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
add the actual method in the controller
- (void)becomeActive:(NSNotification *)notification {
NSLog(#"becoming active");
}
be sure to clean up the notification
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
If you can't access your view controller from the app delegate, you could also have your controller listen to the UIApplicationDidBecomeActiveNotification notification.
you can create your class called TableViewManager. in there register list of UITableView so that you can refresh any table you want.
it's like this, in yourTableViewManager class, you have a method called
- (void)RefreshTableView:(UITableView *)tableView {
if(tableView != nil)
[tableView reloadData]; }