Register for remote notifications in navigation controller - ios

I have many different views in app. While the application state is active I would like to have a generic response to remotenotifications that is not an alertview. One way to do this would be to place the notification observer in uinavigationcontroller rather than in the different view controllers and then place the notification element in navigationcontroller.view. However, thus far nothing appears in navigationcontroller.view when I try to add a label there. Has anyone had success doing this?

If I understand correctly, it seems like you want to show a view whenever your app receives a remote notification and you want to be able to display the view from anywhere inside the app. If this is correct, the best approach would be to create a new UIWindow object and display that as an overlay on top of your app's main window. This answer describes how to create a UIWindow and display it. Additionally, this library does something similar to what you're trying to accomplish.

Related

write something to already opened view, when coming back from the modal view

Edit something in the parent view, when moved from the child modal view. Should I use "viewWillAppear" or some other pre-defined functions for the view to be appeared in foreground..
There are actually plenty of ways to achieve what you want.
One would be checking on viewWillAppear,
another way is to create protocol, and call some protocol method when child is going to be dismissed.
Also, you could have a property in the child class to hold object
reference to the parent, and when being dismissed call some method
on parent to notify the parent that the modal is being dismissed.
You could use NotificationCenter, and post a notification, and
handle the notification on the parent to update it, as well.
I don't know which one is the most suitable for you, if you give more context to the problem, I could clarify the answer. Good luck!
Edit:
Here is the official Apple documentation;
Here you can find information about how to use the notifications and notification center.
You can search the Google for more on Notifications and NotificationCenter.

UIAlertController as Child View Controller

In order to allow for better customization in my app, I want to put UIAlertController within a UIViewController subclass. The alert should be a child of the UIViewController to allow for more flexibility.
What is the best way to achieve this?
I have tried so far to add the alert as a child view controller. When I want the alert to show up, I present the container view controller modally.
I have also tried to call presentViewController on the view controller (with the alert as a parameter). However, I'm not sure what the correct approach is to achieve what I am looking for.
Edit: Neither of these solutions were working as I had hoped, but I don't know if that is because I did something wrong or because the approach is wrong.
Edit 2: The added functionality is providing UIKeyCommands to make selection of the alert options easier. I am not subclassing UIAlertController for two reasons: (1) the documentation states that it should not be subclassed, and (2) adding key commands UIAlertController doesn't work.
Could you please explain what sort of flexibility such an approach provides? I'm not doubting that it does, as I'm relatively new to iOS development, but I can't quite figure out what you're trying to achieve using a child view controller.
If you simply want to present a UIAlertController from your view controller, just create the alert controller, configure it, and use presentViewController to display it.
As a general approach, however, I follow the advice found in the top answer in this thread. It suggests using either a UIAlertController subclass or category with a custom show method that provides the alert controller with its own window so that alerts can be presented 'globally' so to speak.
I don't think your question is absolutely clear, but maybe you want a custom pop-up view controller, not alert? Then you probably need something like STPopupController - it's customisable and easy to use.

Adding a subview that stays in place as you navigate through views

I am looking to create a subview that looks like a banner drop down view from the Navigation Bar.
or like this
I feel like I see this effect all the time but have been struggling for a while to recreate this. I have it working on single view applications but I would like it to stay in place as I navigate from view to view. Right now I have the view setup in the storyboard and would like use this because I had issues attempting this programatically.
To create this "drop down banner view" and have it stay in place (until the user dismisses it) as a user navigates from screen to screen I see two solutions, each of which I have stumped myself on.
Create my own master view as the window.rootViewController
I see this as the cleaner solution in the end, but a bit harder to implement. Would it be possible to create a blank UIView as the rootViewController and whenever the app needs to drop down an alertBanner it could tell the rootController to do so? The view hierarchy would be something like
window -> masterViewController -> alertBannerController -> Navigation Controller -> otherViewControllers
but I cannot seem to have this set up the proper way.
Create an instance of my AlertBannerView from a subclass of the UINavigationController
Instead of calling the method to create a dropDownBanner from the rootViewController another option I see is subclassing the navigationController to be able to drop down this subview. This way it could still persist as the user navigates around views.
Once again I am having problems setting this up properly to work with the existing NavigationControllers
Conclusion
I do not know what is the best approach here.
This is different than the Apple Push Notifications drop down screen because I would like to customize it for the apps UI
Any tips on how to properly set up a custom view as the rootViewController would be great (where do I do this? what methods do I need to call?)
The problem to solve here is to have the alert banner view remain in the window until the user dismisses it even if they are navigating from screen to screen.
Thanks!
Depending on which version of iOS you're working with, yeah there are a lot of possibilities and ways of doing this. In fact, there are a lot of people who already have.
Best place for getting some ideas on how to attack this problem, to me, is by looking at an existing solution. CocoaControls is a great place for this.
For instance, here is a relatively recent one: https://www.cocoacontrols.com/controls/mpgnotification
And here is a list of a bunch of them ( they aren't sorted in any particular order unfortunately though ) : https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=notification

Animate colour changes of background to different View Controllers at same time

I have 3 different ViewControllers that are inside a combination of methods to get one result. During the process I need to change smoothly with some kind of animation the background colour dynamically to show possible different user behaviours. The question is:
Is there any way to change all backgrounds at same time or will I need to check what colour I have each time in each one and pass it to the next view controller and continue the animation there?
If you're asking if you can change the background on view controllers that are not yet loaded or on view - you can. However you need to pass the info to the view controller when you're about to display it on screen to the user. Then change colours in viewWillAppear or ViewDidLoad.
If your viewcontrollers are loaded. You can use NSNotification.
look this link
Send and receive messages through NSNotificationCenter in Objective-C?
Wherever you want to change color write the receiveNotification code in viewDidLoad and call a method with it.
May be it will work for you.

iOS: UIApplicationDidReceiveRemoteNotification

Is there anything like this outside of the application delegate? I would just like to be able to do certain things in my view controllers when I receive a notification. Will I just have to import each of them and check the type of class against my current view controllers in order to perform actions on them?
Essentially, if I received a remote notification and I didn't have an exact reference to the top most view controller on the stack, is there a good way to access a class or instance method of that controller? Or maybe even some properties?
Why don't you just send an NSNotification (don't mistake notification center and Apple Push Notifications) to them? You have a nice article here talking about it.

Resources