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.
Related
Right now, in FirstviewController, I got a button, when i clicked it, I get the value back using delegate. Right now, I want to send this value to SecondViewcontroller and reload it's tableview data. How to do that? How about use nsnotificationcenter, but i have tried, it's not working. I post the notification in the delegate that implemented in Firstviewcontroller. the code like this:
FirstviewController.m
// delegate that get selected cat
- (void)didSelectSubCat:(SubCat *)cat;
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"DidSelectCat" object:self userInfo:#{#"catId": cat.catId}];
}
SecondViewcontroller.m
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(selectedCat:) name:#"DidSelectCat" object:nil];
}
- (void)selectedCat:(NSNotification *)notif
{
NSLog(#"userinfo: %#", [notif userInfo]);
}
In SecondViewCOntroller create protocol with a methode
#protocol TeamListViewControllerDelegate <NSObject>
-(void)SecondViewController:(SecondViewController*)teamListViewController data:(NSString*)data
#end
declare delegate
#property(nonatomic,assign) id<TeamListViewControllerDelegate> delegate;
In firstViewcontroller follow that protocol and implement that method and inside that method refresh table.
I hope this will help.
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];
I do have two ViewController class, one firstviewController other secondViewController in first viewcontroller i call this [self dimissModalViewControllerAnimation:NO];
to dimiss the view! now i need to dimiss the same view from another secondViewController class.
So do i need to call super in that!
[super dismissModalViewControllerAnimated:NO];
Or Do i need to create any protocol for dismissing the view! from another secondViewController class.
Can any guide me with this issue.
you can register a notification in firstViewController's viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"MyNotification" object:nil];
Add the event handler in firstViewController
- (void)handleNotification:(NSNotification*)note {
[self dismissModalViewControllerAnimated:NO];
}
Then you can trigger the event in secondViewController
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:nil ];
You should only be using super when you're overloading a method definition, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Login / Signup";
}
Typically, if you're trying to tell one view to do something from another view, delegates are your friend. You could create a weak delegate variable to hold a reference to the view controller to be dismissed, and call [delegate dismissModalViewControllerAnimated:NO];
Hi i am very new in iPhone/iPad developmet.
In my application on clicking of button in want to show view controller like presentModalViewController and i am able to do that which contains the UITableView with some numbers of values. on selecting particulate row i want to pass values to controller which is behind that controller.
for that i am using apple sample application PhotoPicker code. http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html
But i am not able to understand the what i did wrong in my code.
I am not able to go in the code which is in the MyViewController.m
- (void)didFinishWithCamera
{
[self dismissModalViewControllerAnimated:YES];
//Here is my some logic
}
can any one help me for this...how to call this function from OverlayViewController?
please refer above link and guide me or give me some steps or guide me for the same.
use delegation.
I use something like this in a app I'm writing at the moment:
// MySecretSelectionViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[delegate mySecretSelectionViewController:self didSelectObject:[self objectForIndexPath:indexPath] atIndexPath:indexPath];
}
// MyViewController.m
- (void)mySecretSelectionViewController:(MySecretSelectionViewController *)es didSelectObject:(MySecretObject *)object atIndexPath:(NSIndexPath *)indexPath {
// do something with the selected object
[self dismissModalViewControllerAnimated:YES];
}
- (void)showMySecretSelectionViewController:(id)sender {
MySecretSelectionViewController *vc = ...
vc.delegate = self;
// present ViewController
}
You can also do this with use of NSNotificationCenter.
Inside MyViewController.m:
- (void)viewDidLoad
{
// your code
// Add observers
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishWithCamera) name:#"YourObserverName" object:nil];
}
+ (void)callDidFinishWithCamera
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"YourObserverName" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// your code
}
From OverlayViewController.m:
[MyViewController callDidFinishWithCamera];
Use the above class method to call didFinishWithCamera in MyViewController from OverlayViewController
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]; }