iPhone xcode - Best way to control audio from several view controllers - iphone

I am pretty new to iPhone programming.
I have a navBar with three views. I need to control audio from all of the views. I only want one audio stream to play at a time. I was thinking that it would be smart to let my AppDelegate have an instance of my audioplaying class and let the three other views use that instance to control the audio. My problem is that I don´t know how my views can use the audioplaying class in my AppDelegate.
Is this the best approach and if so, how? Is there a better way?

The cleanest way would probably be to implement a singleton that you could access from all your view controllers (e.g. [[MYAudioController sharedController] theAudio]). It's also possible to access it in your app delegate (e.g. [(MYAppDelegate *)[[UIApplication sharedApplication] delegate] theAudio], but I tend to think it's a bad idea to make view controllers dependent on the app delegate.

Related

Should a global instace of a viewcontroller be owned by app delegate

Due to a bug/problem with ZBar and iOS 7 we can not create, destroy and recreate a view controller that's used for scanning. So we need to keep an permanent and global instance of it to be accessed by several different views.
In the current solution the scanner view instance is a member of the app delegate and instanciated when the application starts. When it needs to be displayed it is accessed by [[UIApplication sharedApplication] delegate].
This is a forced solution but I still want it to be as descent as possible. Is there a better place to put the scanner view controller?
Make a Singleton class for your scanning
Better you put in .pch file,
#define MY_APP_DELEGATE ((AppDelegate *)[UIApplication sharedApplication].delegate)

iphone - communicate with previous viewcontroller after segue

Is it possible to communicate with a viewcontroller after a segue switched to another viewcontroller?
I want to know, because I need data from a viewcontroller passed on to the next viewcontroller,
but I just can't figure out how to get data from one viewcontroller to another, as they have no unique names or something which I can use to communicate with.
So can someone tell me:
If it is possible to communicate between viewcontrollers, and if possible, how?
Usually you do it the other way around, pushing values from the source to the destination. If you implement prepareForSegue in the view controller that is going to be segue'd out, you can use
[segue destinationViewController]
to get a reference to the destination view controller.
Then you can set any values in that controller that are needed using properties on that controller before it segues in.
I have searched for tutorials about making a MVC based iPhone app, but without success
Goto ITunesU and look for Paul Hegartys "iPad and iPhone Application DEvelopment" from stanford university
very good!
try the views presentingViewController property.
A typical OOP pattern is to create yet another object, a Model object (MVC paradigm), connect all the view controllers that need to communicate with this Model object, and pass any shared state or variables by setting and getting properties in this Model object. A common shortcut for very small apps is to use the App Delegate as a Model object, as any other controller can get a reference to the app delegate. But this shortcut is not very extensible to larger or reusable code.

application flow in iphone program

I am making a application for the iphone and i am using window based application and i am doing this:
#importing all UIViewController class in the appDelegate class.
creating the object for accessing the variables and function and I am controlling all
action,event and controls from the appDelegate class.
I want to ask that is this right approach. I dont want to use view based application, I want to use UIViewController class just only controllers display.
If this is not a good approach for making the application tell me the good process for doing this.
I suggest you take a look at this:
http://www.icodeblog.com/2011/10/11/back-to-basics-an-introduction-to-view-controllers/
You should also read up on the iPhone-programming-basics, you will find a lot of nice tutorials and articles with a quick Google-search for "iPhone programming".
You could just create a singleton that holds the data and methods you want to share and access it from any view controller you want. Conceptually, it's not much different then what you are doing with the appDelegate class but it is better so that you don't clutter up the app delegate.

How to get/set the rootViewController?

Now,I gonna Develop an App ,which wants to switch from many different Views irregularly,also the views need to load large resources,AKA,it's hard to manage memory.Are there any good solustion?
PS:I created a ViewController as RootViewController,and When a button was Touch,run the code as
"ViewController=newController"
.The problem came,The new View loaded wrong way,it rotate so that couldn't show in a correct way.
I google for the solution,some one said ,I should replace the rootViewController,just like that,
[UIApplication sharedApplication].delegate.window.rootViewController=newController;
But I can't get/set the rootViewController in other class though it's a singleton.
Why not having a class that handles all the view switches ?
This article describes an architecture that might be helpfull: http://www.mikeziray.com/2010/01/27/handling-your-initial-view-controllers-for-iphone/comment-page-1/#comment-607

How to use Core Data at 3rd level View of navigation

Dear All, I am trying to build a navigation based iPhone application where I want to use Core Data modal at 3rd level view of navigation. The 1st and 2nd level view of this application are just simple tableViews. I have looked at the Apple CoreDataBooks application but there it used the methods for Core Data in the AppDelegate and RootViewController while I want to use these methods somewhere else. It is possible? Can somebody refer me to some tutorial for that?
Thank you very much.
You can
Have your view controller create the Core Data stack.
Use [[UIApplication sharedApplication] delegate] to find the app delegate from anywhere, and ask it for a context.
Pass your context as an argument down through the view controllers.
It's a choice only you can make, based on the ways you expect your application to change in the future.