iOS TableView not reloading if current view is tableViewController - iphone

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
NSLog(#"local notifications count = %d", [[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
}
This is the method from app delegate, and I need to reload the table view when a notification arrives.
How can I implement the reloadData, since Xcode won't accept if I write "[TableViewController.tableView reloadData];"?

//Just do one thing, as you got the notification , post on more notification as follows in the method ....
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
[[NSNotificationCenter defaultCenter] postNotificationName:#"RELOAD_DATA"object:nil];
}
//the add observer in viewDidLoad of that view controller where your table is added..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTableNotification:)
name:#"RELOAD_DATA"
object:nil];
//and make a method in the same class
- (void)receiveTableNotification:(NSNotification *)pNotification{
[your_table_view reloadData];
}
//now remove obser in dealloc in your view controller class where you add observer ..
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

instead of TableViewController.tableView use [self.viewController.tableView reloadData];
If your current view Controller is not the tableView, consider using NSNotificationCenter to post a reload notification

use NSNotificationCenter postNotification: mechanism for posting a reloadTable notification which you can catch in your class having tableViewController by observer to fire reloadData for your tableview

Please call this in app deligate within the call backfunction
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
UINavigationController *navControl = [[[self tableViewController] viewControllers] objectAtIndex:lastObject]; //
id Obj = [[navControl viewControllers] lastObject];
if([Obj isKindOfClass:[Required class]])
{
[Obj reloadDataOfTable];// made an function to desired class.
}
}

Related

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.

motionEnded not called in appDelegate

I want to integrate shake feature throughout the app. So I am doing everything in appDelegate. I need to push a viewController, I am able to push in motionBegan, but i wanted to do it motionEnded. yes motion ended does work in a view controller, but in app delegate it is not being called.
Doing as
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder{
return YES;
}
motionEnded not called
-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(event.subtype==UIEventSubtypeMotionShake){
NSLog(#"motionEnded called");
}
}
motionBegan called
-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(event.subtype==UIEventSubtypeMotionShake){
NSLog(#"motionBegan called");
}
}
you could basically register your viewController for applicationDidBecomeActiveNotification or any depending on your needs
for example in your viewController's viewDidLoad method you could register it for notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMethod)
name:UIApplicationDidBecomeActiveNotification object:nil];
and implement this method in your class, your myMethod will call everytime your application will become active
-(void) myMethod(){
// do your stuff
}
finally un-register viewController from the notification in dealloc method
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

what method within a ViewController's class can I call to check when it has been brought to the foreground?

what method within a ViewController's class can I call to check when it has been brought to the foreground?
For example Im looking at a page on my application and I decide to close the application and go back to it later. When I go back to it the same view as I was looking at was on the screen. However... As soon as I open the application I want to segue over to another view.
How can I do this?
Currently trying this:
- (void) applicationDidBecomeActive:(NSNotification*) notification
{
[self checkActivity];
// Do your stuff here
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
return self;
}
- (void)checkActivity{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"Checking if re-authentication required...");
if([[defaults objectForKey:#"shouldgotologin"] isEqualToString:#"yes"]){
NSLog(#"View Should go to login...performing segue");
[defaults setObject:#"no" forKey:#"shouldgotologin"];
[defaults synchronize];
[self performSegueWithIdentifier:#"backtologin" sender:self];
} else {
NSLog(#"Should go to login is not true.");
}
}
Register your view controller to observe UIApplicationWillEnterForegroundNotification:
1) Inside view controller's init method:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
2) Inside view controller's dealloc method:
[[NSNotificationCenter defaultCenter] removeObserver:self];
3) Also, have your view controller implement this method:
- (void) applicationWillEnterForeground:(NSNotification*) notification
{
// This method will be called just before entering the foreground;
// Do your stuff here
}
If the timing of UIApplicationWillEnterForegroundNotification doesn't suit you, check all the available notifications for UIApplication here:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
Jump To ApplicationDelegate File, you will find following methods.
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
Remember, It is not the viewController who receives the notifications related to the Application states like willResignActive,didEnterBackground,willEnterForeground. ApplicationDelegate object is going to handle those notifications. So, Try putting your logic in above methods.
Hope that helps. If not, add your queries using comments below my answer.

How to register for an NSNotification from a UILocalNotification?

I have a tabbar application and let's say that I want to switch to the second tab and popup an alert at 12:00, even if my application is not running.
I got all the code for UILocalNotification working correctly, but then I thought that the best way to do that would be by posting a notification from the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
// Handle launching from a notification when the app is NOT running
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
[tabBarController setSelectedIndex:1];
[[NSNotificationCenter defaultCenter] postNotificationName:#"AlertNotification" object:self];
}
return YES;
}
Then, in my SecondViewController.m, I have:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popUpAlert:) name:#"AlertNotification" object:nil];
}
But this does not work. I suspect that the notification is sent while the viewDidLoad of the SecondViewController has not been called yet, right? Is it possible to work this out? And do you agree on my approach of using NSNotificationCenter in this case?
Thanks in advance.
I quickly created a test project and got it working by putting the notification registration in awakeFromNib (assuming SecondViewController is created in a xib file)
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popUpAlert:) name:#"AlertNotification" object:nil];
}
I guess, you are right. It doesn't work because you are posting the notification before adding the view controller as observer for it.
Another approach would be to add a bool property to the app delegate to indicate whether the app has been started from the local notification.
The app delegate can be requested from anywhere in the app with [[UIApplication sharedApplication] delegate].
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];

iPhone - Launching selectors from a different class

I'd like to reload a table view which is in another class called "WriteIt_MobileAppDelegate" from one of my other classes which is called "Properties". I've tried to do this via the NSNotificationCenter class - the log gets called but the table is never updated.
Properties.h:
[[NSNotificationCenter defaultCenter] postNotificationName:#"NameChanged"
object:[WriteIt_MobileAppDelegate class]
userInfo:nil];
WriteIt_MobileAppDelegate.m
-(void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadItProperties:)
name:#"NameChanged" object:self];
}
- (void) reloadItProperties: (NSNotification *)notification {
NSLog(#"Reloading Data"); //this gets called
[[self navigationController] dismissModalViewControllerAnimated:YES];
[self.navigationController popToRootViewControllerAnimated:YES];
[self.tblSimpleTable reloadData];
[self.tblSimpleTable reloadSectionIndexTitles];
// but the rest doesn't
}
What am I doing wrong here?
Seems like you are using the object parameter wrong:
addObserver:selector:name:object:
notificationSender
The object whose
notifications the observer wants to
receive;
that is, only notifications
sent by this sender are delivered to
the observer. If you pass nil, the
notification center doesn’t use a
notification’s sender to decide
whether to deliver it to the observer.