Calling a method from another class that removes views - ios

Hi I am hoping this is an easy answer and it is just because I am new to this.
I am trying from inside another class be able to remove views that exist in another class.
In the class I am calling the method I do this.
ViewController *viewController = [[ViewController alloc] init];
[viewController closeNotifiactions];
And then in my other class here is the method where I am trying to remove the views. If I place an NSLog in this method it is called and works, however the removeFromSuperview codes do not work.
-(void)closeNotifiactions
{
[spinner removeFromSuperview];
[loadingView removeFromSuperview];
}
And in my .h file I reference the closeNotifications method like this: -(void)closeNotifiactions;
Is there something I need to do different to make the closeNotifications part work? Thank you!

You can use NSNotification to access another class properly.
Class A:
//Create NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closeNotifiactions) name:#"notification" object:nil];
Class B:
//Just Call NSNotification to call that method
[[NSNotificationCenter defaultCenter] postNotificationName:#"notification" object:nil];
I hope this will help you.

ViewController *move = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];;
[move closeNotifiactions];
[move release];

Related

How to do push navigation with Notification Observer?

I have two classes. A and B for example.
I have created an observer in A like this
- (void)viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(somethingHappens:) name:#"notificationName" object:nil];
}
-(void)somethingHappens:(NSNotification*)notification
{
NavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"contentViewController"];
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"loyaltyVC"];
navigationController.viewControllers = #[vc];
}
I want to call this observe from page B. I'm using like this
[[NSNotificationCenter defaultCenter] postNotificationName:#"notificationName" object:self];
Do you have any suggestion how we can do this ?
To be precise this are not push notifications. The following are just normal in-app-notifications.
Your -(void)viewDidLoad should contain a call to its super method at some point.
//in A
#implementation AThingy {
id<NSObject> observeKeeper;
}
- (void)viewDidLoad {
[super viewDidLoad];
observeKeeper = [[NSNotificationCenter defaultCenter] addObserverForName:#"notificationName" object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification * _Nonnull note) {
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:#"contentViewController"];
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"loyaltyVC"];
nav.viewControllers = #[vc];
}];
}
// not mandatory but to explain it complete
-(void)dealloc {
// could be placed in some other method also.. like -viewDidDisappear:
[[NSNotificationCenter defaultCenter] removeObserver:observeKeeper];
}
#end
//in B
#implementation BThingy
-(void)postNotify {
[[NSNotificationCenter defaultCenter] postNotificationName:#"notificationName" object:nil];
}
#end
But keep in mind that NotificationCenter is working relative to its observing Objects and so it matters if you watch and post in general on (nil) or on some specific object. Why is that? Because you could decide to instantiate two objects from the same class that observe notifications and still specifically observe each notification meant for them or both.
Have you tried to print a log in method somethingHappens:?
Is your method -(void)didLoad ever been called?

NSNotificationCenter: addObserver that is not self

Here's the question:
Can one View Controller add another View Controller as an Observer to the defaultCenter before the second view has been loaded?
I have a model class that creates a NSURLSession, grabs some data, builds an array, and sends notifications that it's done (along with a pointer to the array).
My app loads with a Map View that instantiates the model, calls the method to create the array, listens for the notification, and drops pins using the array.
I have a Table View tab that I want to load using the array built by the map.
Can my Map View add my Table View Controller as an observer before the Table View is loaded?
Something like:
[[NSNotificationCenter defaultCenter] addObserver: TableViewController ...
Thanks for any insight. I'm figuring this out as I go.
-----------------EDIT--------------------
viewDidLoad from MapViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.delegate = self;
_model = [[WikiModel alloc] init];
_lastArticleUpdateLocation = [[CLLocation alloc] initWithLatitude:0 longitude:0];
_lastUpdateUserLocation = [[CLLocation alloc] initWithLatitude:0 longitude:0];
// Listen for WikiModel to release updates.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadData:)
name:#"Array Complete"
object:_model];
//table view listener attempt ...
UITableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"tableViewController"];
[[NSNotificationCenter defaultCenter] addObserver:tvc
selector: #selector(updateDataSource:)
name:#"Array Complete"
object:nil];
[self.navigationController pushViewController:tvc animated:YES];
}
From the TableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Listen for WikiModel to release updates.
/*
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateDataSource:)
name:#"Array Complete"
object:nil];*/
}
-(void)updateDataSource:(NSNotification *)notification
{
_wikiEntries = [[notification userInfo] objectForKey:#"wikiEntryArray"];
[self.tableView reloadData];
NSLog(#"************received***********");
}
This is possible as long as you pass the pointer to your view controller as the observer. Here's an example:
//Go to the detail view
VC2ViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"VC2ViewController"]; //ID specified in the storyboard
[[NSNotificationCenter defaultCenter] addObserver:dvc selector:#selector(notificationReceived:) name:#"MyNotification" object:nil];
[self.navigationController pushViewController:dvc animated:YES];
Then, you can post your notification as usual:
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:nil];
I just tested the above code and it worked fine for me, the following function was called in my detail view controller:
-(void)notificationReceived:(NSNotification *)notification {
NSLog(#"RECEIVED");
}

Presenting a View Controller with an xib from an SKScene

I have tried many things, none of which are working. I have an SKScene, and when the game is over, I want it to go back to the original view controller. Thank you in advance.
I have tried this in my SKScene
homeScreenViewController *viewCont = [[homeScreenViewController alloc] init];
[viewCont viewDidLoad];
This in my other view controller
constructinoViewController *view = [[constructinoViewController alloc ] init];
[self presentViewController:view animated:YES completion:nil];
It mostly says The view is not in the view hierarchy.
You need a way to send the parent viewController a message.
In your viewController which presents the SKScene, add the following line in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closeScene) name:#"closeScene" object:Nil];
Also, add this method
-(void)closeScene
{
//Remove the SKView, or present another viewController here.
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"closeScene" object:nil];
}
Then in your SKScene, at the point where your game ends,
[[NSNotificationCenter defaultCenter] postNotificationName:#"closeScene" object:nil];
You can do this by using delegates as well.
If you have presented the ViewController, then you need to dismiss it to return to previous ViewController.
You should use
[self dismissViewControllerAnimated:YES completion:NULL];

Dismissing UIPopoverController with UINavigationController

I'm currently building an iPad app in which I need to implement a pop over view.
I have set up a view controller like I always do:
Create UIViewController wit xib file
set the xib up and do the necessary programming in it's .h & .m files
now in the view controller I'm loading it from (from a UIBarButtonItem), I have this code:
- (void) action
{
ItemContent *newItem = [[ItemContent alloc] initWithNibName:#"ItemContent" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:newItem];
_popover = [[UIPopoverController alloc] initWithContentViewController:nav];
[_popover setPopoverContentSize:CGSizeMake(557, 700) animated:YES];
_popover.delegate = self;
[_popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
This properly displays my view- and UINavigationController in a UIPopOverController. So far so good!
In the newItem view controller, I have made a bar button in my navigation bar that says "Done". When that button is pushed, I want the UIPopOverController to disappear. How do I do this:
Set a method for when te button is pushed. In this method I want to call a function on the view controller that loaded the Popover to dismiss it again.. but how do I do this?
Put things shortly
How do I make my UIPopOverController call a method on the view controller that loaded the UIPopOverController?
I have been searching SO for a while but none of the solutions and answers solve my problem. If I missed something please inform me ;)
Thank you so much in advance!
You can do this by delegate...
In NewItem.h declare a protocol
#protocol NewItemDelegate
-(void)onTapDoneButton;
#end
Now create a delegate property like this
#property (nonatomic, assign) id<NewItemDelegate>delegate;
In NewItem.m in doneButtonPuhsed method call this
[self.delegate onTapDoneButton];
Change this method a bit
- (void) action
{
ItemContent *newItem = [[ItemContent alloc] initWithNibName:#"ItemContent" bundle:nil];
newItem.delegate =self;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:newItem];
_popover = [[UIPopoverController alloc] initWithContentViewController:nav];
[_popover setPopoverContentSize:CGSizeMake(557, 700) animated:YES];
_popover.delegate = self;
[_popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
Now implement NewItemDelegate method below this action method.
-(void)onTapDoneButton{
//dismiss popover here
}
I had this problem too and solved it using notifications.
In your parent controller, in viewDidLoad method, you have to add an observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(closePopover)
name:#"closePopoverName"
object:nil];
Then, in your viewDidUnload method you remove the observer like this:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Of course, you have to create the function which actually dismisses the popover:
- (void) closePopover
{
[_popover dismissPopoverAnimated:YES];
}
Then, in your ItemContent controller, you just post a notification when you want to close the popover:
[[NSNotificationCenter defaultCenter] postNotificationName:#"closePopoverName" object:self userInfo:nil];
You could define a protocol on ItemContent and use it on your CallerViewController. If you want to close your Popover, just call your delegate method, which you will implement on your CallerViewController

Update splitViewController from ModalViewController

I need to update on of my splitView's from a modalView, but I am not having any luck.
for instance: From my rootView (which is a UITableVIewController) if I set an option to hide a certain section from the rootView table in my ModalView, when I dismiss the modalview, the setting doesnt take affect on screen for the tableview, same goes for detailView.
i've tried:
MyRootView *mrv = [MyRootView alloc] init];
[mrv updateTable];
[mrv release];
[mrv updateTable]; is located in my RootView and contains a [tableView reloadData];
If I place an NSLog in there, that prints, just that the table doesnt reload while on screen.
Even tried viewWill/DidAppear, no avail.
Any help much appreciated!
So I was able to resolve this issue with Notifications.
[[NSNotificationCenter defaultCenter] postNotificationName:#"update" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateView:) name:#"update" object:nil];
- (void)updateView:(NSNotification *)notification {
[tableView reloadData];
}

Resources