Multiple view controllers subscribe to bluetooth notification - ios

guys,
I am new to the iOS development. I designed a class A that implements CBCentralManagerDelegate, CBPeripheralDelegate.
Also. there is a protocol that kinda wrapped the bluetooth notifications in an easy to understand methods.
My questions now, i have multiple view controllers that needs to respond to the bluetooth notifications. I am not sure how to make it work. Do I make my view controllers delegates of my class A? It seems kinda awkward. Whats the best way to achieve this? Thanks.

If I understand your design correctly, you are wrapping whatever notifications you need to handle and whatever delegate callbacks that are being sent back to you with the class A - I think it's not awkward at all, just define a protocol for this class to establish good delegation to whatever other classes in your project need these services and hook them up. It would also be best in my opinion to set this class up as a singleton so that it will be the only one managing the callbacks and notifications and it will be easily accessible throughout the application.
Good luck

Related

Delegate with instance variable of view controller

I am creating an Obj-C application, and am working on a single view. However, within this viewful, there is enough logic that I want to separate that from the view controller itself. I decided to create a delegate, but my current problem is how these two should communicate. Two potential solutions I have been considering:
1) An instance variable of the view controller within the delegate. Then when any view updates need to happen I simply call the appropriate method on that instance variable.
2) Notifications being fired off by the delegate and add the view controller as an observer.
Is there one method that is obviously superior or does it depend on the situation? I don't know the proper way to go about this and am trying to approach this from the correct angle.
If it's non-UI related responsibilities you want to separate from the view controller, it's very natural and recommended to have another object responsible for those tasks. In this case, you can either have a singleton for those responsibilities, or simply a NSObject subclass instance strongly referenced in your view controller.
If it's pure UI related tasks you want to separate from the view controller, just to organize the source code clearer, you should instead create categories for the view controller and name the categories according to their responsibilities, for example, MyViewController(Localization), MyViewController(UserInteration), etc.
This is a broad question. However, if what you are trying to do is simply refactor existing code to make maintenance easier without changing any functionality, use instance variables. Your code execution sequence will be unchanged and your method can return status information on the execution results. Your original controller and your new refactored class are tightly coupled - know a lot about each other.
Notification logic is event-based (something interesting happens and an event is sent to registered observers). The logic is executed when the observer receives the event so there is no guarantee you can control the exact execution sequence, and the caller does not know if the observer received or successfully processed the message - the are loosely coupled.
When in doubt look at what Apple does in its framework classes like UITableView. Delegates give you fine control over when things happen.

Objective C: is Posing available for ios?

I am trying to implement posing for one ios project.
The scenario:
Defining class of controller at run time
I realise that poseAsClass or class_poseAs is not available for ios
& also deprecated for macOX.
will be grateful to any directions to implement posing in ios. Thanks
The whole pose / swizzle approach is really useful if you want to tamper with the OS / private SDK supplied classes - but you generally shouldn't be doing that and it's not a good idea to use it as a standard approach in your own code.
The scenario: Defining class of controller at run time
You would usually do this by using an abstract superclass / interface / #protocol to define the interface that your potential controllers need to implement and then switching them in and out at runtime.
In your case it seems that you would have one controller which acts as a proxy for the true controller. You also don't technically need an #protocol because UITableViewController is effectively your abstract superclass, but it would be best for your proxy to be a UITableViewController and own the view and for your other controllers to be NSObject subclasses and simply conform to the UITableView DataSource/Delegate protocols.
You should look into Method Swizzling. It helps you change the functions/function bodies at run time.
There is a great tutorial here.

Defining class of controller at run time [duplicate]

I am trying to implement posing for one ios project.
The scenario:
Defining class of controller at run time
I realise that poseAsClass or class_poseAs is not available for ios
& also deprecated for macOX.
will be grateful to any directions to implement posing in ios. Thanks
The whole pose / swizzle approach is really useful if you want to tamper with the OS / private SDK supplied classes - but you generally shouldn't be doing that and it's not a good idea to use it as a standard approach in your own code.
The scenario: Defining class of controller at run time
You would usually do this by using an abstract superclass / interface / #protocol to define the interface that your potential controllers need to implement and then switching them in and out at runtime.
In your case it seems that you would have one controller which acts as a proxy for the true controller. You also don't technically need an #protocol because UITableViewController is effectively your abstract superclass, but it would be best for your proxy to be a UITableViewController and own the view and for your other controllers to be NSObject subclasses and simply conform to the UITableView DataSource/Delegate protocols.
You should look into Method Swizzling. It helps you change the functions/function bodies at run time.
There is a great tutorial here.

What is the correct way to setup a Model for IOS?

I am creating an app and using Parse.com for Data Storage.
I would like to use a Model for all the different aspects of Parse that I use. For example, if I store some Game data I would like a model to handle doing any finds/updates/entries into a Game model held on Parse.
So I would like a Model to handle all these methods, instead of adding these into the View Controllers.
What is the best/correct way to set these up? I have heard of singletons but not sure if they would be correct here.
At present, I set these up using a subclass of NSObject. I then create all the methods as class methods (no instance methods as there is no instance to create). I then call any class method as usual.
[GameModel classMethodName];
Would this be correct? Is there a better approach or any issues with this approach?
At present, I set these up using a subclass of NSObject. I then create
all the methods as class methods (no instance methods as there is no
instance to create). I then call any class method as usual.
This doesn't sound like a very good plan. The job of a model is to manage the data that your application operates on. That means that your model should have data to store, which means that you should be instantiating your model.
Building support for your back end infrastructure (Parse, in your case) is a good idea. It's much better to put it there than to put it in your view controllers. But where does that supporting code store the game state, for example, or the player's history? It should store it in the model object(s).
Without getting into the whole singletons good vs. singletons bad can of worms (you should Google that), it sounds like you're using class methods for much the same reason that people often use singletons: easy access. Instantiating your class and using instance methods instead of class methods would mean that you have to find a way to share the model with those objects that need it, and that's more work than just using the globally accessible class methods. Doing that work will make for a better application, though.
For example, you could restrict each view controller's access to just those parts of the model that it needs. The game play view controller might only get to see the current game board, and the high scores view controller would only see the player's history. That eliminates many possible bugs -- you'll never have to wonder if the game board view controller is causing a problem with the player history, because it never sees that part of the model.

Which is better - relying on delegates or persisting through objects - iOS

After doing some reading I notice Apple pushes the use of delegates on controllers communicating with one another.
So say I have a Menu controller which needs to communicate with the help controller (and they need to share info - lol don't ask me why they would). I could do this through delegates, but why use delegates when I can pass the required information on through a help object and have the help controller pick back up this info that was changed in the previous controller.
Delegates still couples - however loosely it may be, wouldn't the use of objects be the best practice here for re usability?
Delegates are great for responses, especially if the response isn't going to be immediate.
If you're simply telling the help controller something like what information to display, passing it directly is great. If you want to know, for example, when the help controller is done displaying help for the user a delegate is clearer and usually superior.

Resources