I am creating a popupView in my application using MJPopupViewController. Now, I need to update a UILabel on the main view when a button is pressed on the popupView. Either I need to update the UILabel on main view as soon the button is pressed(preferably) or when it is closed.
I have already tried viewWillDisappear and viewWillAppear methods, but both don't seem to work.
you can use NSNotificationCenter for calling other class Method from current class like bellow example:-
add Notification at MainClass in your ViewDidLoad Method:-
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(UpdateLable:)
name:#"UpdateLbl"
object:nil];
[super viewDidLoad];
}
-(void)UpdateLable:(NSNotification *)notification {
//Update lable code here
}
Now you just need to call this method From your popupView class Button click Action for calling update Notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"UpdateLbl" object:self]
;
Hope it's Helps you:)
Related
I'm not sure about how to achieve this and hence i'm asking this question.
I have 2 ViewController(VC) named abVC and xyVC.
abVC contains only TableView and xyVC contains only a button.
When i tap a button on xyVC, it should reload tableview inside abVC without moving to that abVC.
Is it possible ? If yes.. then please help me,How to do that?
You can do it with both block and NSNotification.
With block you can implement it as follows:
make a property as follows in xyzVC.h:
#property(strong,nonatomic)void(^callBack)();
Synthesize this property in xyzVC.m
#synthesize callBack
Call this block in Buttons click event
- (IBAction)btnClick:(UIButton *)sender {
if (callBack)
{
callBack();
}
}
Implement it in abcVC where you want call back:
[abVC setCallBack:^{
//reload your tableview here
}];
With NSNotification you can implement it as follows:
Register for receiving notification in abcVC.m
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadTableView) name:#"ReloadTable" object:nil];
- (void)reloadTableView{
// Reload your tableview here
}
Now post the notification from xyzVC
- (IBAction)btnClick:(UIButton *)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTable" object:nil];
}
Note: If you are using NSNotification then don't forget to remove it when your view controller is dismissed
I hope this will help you :)
Add this line in your button action method
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadTable" object:nil];
Add this code where you have implemented table, so in abVC:
inside viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadTableViewData) name:#"ReloadTable" object:nil];
Add following method
- (void)reloadTableViewData{
[self.myTableView reloadData];
}
Implement dealloc method (to prevent crash)
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ReloadTable" object:nil];
}
I dont want to add observer in viewDidAppear and remove in viewDidDisappear.Will not serve my case.
I have tried doing it in dealloc.
My root VC is in navController.Then a second VC is pushed in navController, where I addObserver for notifications to be sent from rootVC.The problem is when I pop the secondVC its dealloc is not called immediately or may be somtimes not called alltogether.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newMessagesNotification:) name:_newMessageNotificationListenerName object:nil];
}
- (void)newMessagesNotification:(NSNotification *)notification {
//some implementation
}
If you do not want to remove in the ViewDidDisapear than i think you should remove it right after you called the navigationCotnroller pop methode. But I think we can't tell you the exact moment when you should remove because we don't know when you want to remove it and why it isn't good in the ViewWillDisapier or in the ViewDidDisapier.
Hi I am developing Iphone application in which am registering one notification observer for UIApplicationWillEnterForegroundNotification. Now I want to remove that one from another view controller. My code looks like
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationBecomeActive)
name:UIApplicationWillEnterForegroundNotification
object:nil];
And I am creating one method for removing observer:
-(void) removeObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
and I am calling this method from other view controller but its not working. I think I have to store observer. But I don't know how to do this. Need help. Thank you.
The second view controller needs a reference to the instance of the first view controller. Let's assume it is hold in a property:
#property (nonatomic, strong) FirstViewControler *firstViewController;
Then your code to remove the fist view controller as an observer would look like this:
- (void)removeObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self.firstViewController];
}
The missing part is: You have to set the property somewhere. Where to do that depends on your code.
I have one main view as shown in picture. I add 2 subviews into that and each has their own view controller.
In ipadMainViewController,
self.dTVC= [[dialoguesTableViewController alloc] initWithNibName:#"dialoguesTableViewController" bundle:nil];
[self.dTVC.view setFrame:rectFordTVC];
[self.view addSubview:self.dTVC.view];
After that, I want to remove the view of dialoguesTableViewController if I press a button in CategoriesViewController. But, I can't remove it.
In CategoriesViewController, I write like this but dialoguesTableViewController can't be removed from ipadMainViewController. How shall I do this?
In CategoriesViewController, I write code like this but it is not working.
self.dTVC= [[dialoguesTableViewController alloc] initWithNibName:#"dialoguesTableViewController" bundle:nil];
[self.dTVC.view removeFromSuperview];
So there are few ways how to do it:
First way:
Add observer to ipadMainViewController initialization method or viewDidLoad method it depends on your needs.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(buttonPressed)
name:#"kNotificationDidPressedButon"
object:nil];
Add -buttonPressed method to ipadMainViewController controller for removing your view or other your purposes.
- (void)buttonPressed
{
// remove view here
}
in the CategoriesViewController in the method where you tap on the appropriate button add this code:
[NSNotificationCenter defaultCenter] postNotificationName:#"kNotificationDidPressedButon"
object:self];
Second way:
Add delegate property to CategoriesViewController. you can find info how to make delegate for example here: link
Third way:
Use objective-c blocks
Initial advice as for beginner:
I suggest you to start from first way, because it is most simplest for understanding. Also you have to remove observer in ipadMainViewControllerr in -dealloc or -viewWillDisapper method, it depends of where you have add observer e.g. in -init method or in -viewDidLoad or -viewWillAppear callback;
[[NSNotificationCenter defaultCenter] removeObserver:self];
try this....
add below code where you can remove view
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(removeFromSuperview) name:#"REMOVE" object:nil];
}
-(void)removeFromSuperviev{
[view removeFromSuperview];
}
add below code form you need to remove
[[NSNotificationCenter defaultCenter] postNotificationName:#"REMOVE" object:nil];
I want to show a full page image Ad every time a UIViewController is shown.
I think I have to call the method inside a viewDidAppear or ViewWillAppear, but they are being called once.
- (void) viewDidAppear:(BOOL)animated{
[self showAds];
}
- (void) showAds{
//Do Something
}
What should I do to call a method every time a uiviewcontroller is shown( even if its already created)?
ViewWillAppear will be called every time a UIViewController is shown,but won't be called when the app is back to foreground.
you can use Notification to achieve your goal by following code,
This scenario is specially when your app is in background and user press HOME button to active it.
Register for Notifcation when your application enterForground in viewDidLoad only.
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleEnteredBackground)
name: UIApplicationDidBecomeActiveNotification
object: nil];
write a method to invoke when application enterForground
-(void)handleEnteredBackground
{
NSLog(#"%s",__FUNCTION__);
// Your stuff here
}
Dont forget to Remove Observer in viewDidUnload method
[[NSNotificationCenter defaultCenter] removeObserver:self];
Post New Notification everytime your application enterForground
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidBecomeActiveNotification object:nil];
}
ViewWillAppear should be called every time. Use:
- (void) viewWillAppear:(BOOL)animated{
[self showAds];
}