Delegate AVAudioPlayer for audioPlayerEndInterruption method - iphone

My ViewController name is Mainviewcontroller in which i m doing all the actions of AVAudioplayer.
If in h file i do
#class MainViewController;
#protocol MainViewControllerDelegate
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
#end
#interface MainViewController : UIViewController <UIActionSheetDelegate, InfoDelegate, AVAudioPlayerDelegate>
I am confused in the above statement i m supposed to do AVAudioPlayerDelegate or MainViewControllerDelegate
If i do #protocolAVAudioPlayerDelegate then i get warning in yellow stating that duplicate protocol definition of AVAudioPlayer is ignored
when i have not defined #protocolAVAudioPlayer anywhere else.
Another confusion i have is that i m supposed to declare these methods in h file first only then implement in m file
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
Please help.

I think you are mis-using the #protocol construct here. You can go ahead and drop the entire #protocol block and just implement your class. You simply need to indcate your class is a delegate for an instance of AVAudioPlayer like this:
#interface MainViewController : UIViewController <UIActionSheetDelegate, InfoDelegate, AVAudioPlayerDelegate>
Then when you instantiate your AVAudioPLayer object, be sure to assign the MainViewController as the delegate:
audioPlayInstance.delegate = self;
Lastly, implement the two methods you want to be called on your AVAudioPlayer instance (I called it audioPlayInstance above). You only need to put these in your .m file, NOT you header. The header declaration for these methods is handled by the AVAudioPlayer.h class file.
Good Luck!

Related

Why can't I use my View Controller as a property?

for some reason I get an error here
#import "OtherViewController.h"
#interface ViewController: UIViewController
#property (nonatomic, strong) OtherViewController *otherViewController;
this gives me an error saying 'unknown type name OtherViewController' why is this happening to me? Is this not the way to send messages to other view controllers. If so what is the way you are supposed to do this?
From what you have posted, there isn't anything wrong with what you have posted. That being said, you don't need to declare a property for the other VC. What you need to do is declare a public property on OtherViewController (like an NSString) and then you can access that property from the ViewController class. Something like (assuming you are using Storyboards):
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"MySegueID"]) {
OtherViewController *ovc = segue.destinationViewController;
ovc.myPublicNSStringProperty = #"Something";
}
}
Try to import the header in the .m file
#import "OtherViewController.h"
and in the header .h file add
#class OtherViewController;
I can't know for sure since there's not enough info given, but I've seen similar errors if you have circular imports. That is, if OtherViewController.h includes #import ViewController.
So, you could try using #class in one of them to avoid the circular references:
#class OtherViewController;
The, be sure to include in your ViewController.m:
#import "OtherViewController.h"

Trouble with error 'cannot find protocol declaration'

I got SO questions also same as this and I tried with solution but still its not working for mine so asked this again.
This is my VCWithProtocol.h
#protocol mydemoDelegate
#optional
-(void)demoDelegateMethodWithSuccess:(BOOL)yesOrNo;
#end
#interface VCWithProtocol : UIViewController
{
id<mydemoDelegate>mydelegate;
}
#property (nonatomic,assign)id<mydemoDelegate>mydelegate;
VCWithProtocol.m
`#synthesize mydelegate`
This is my class where I am trying to use my Delegate
#class VCWithProtocol;
#interface VCTOUseDelegate : UIViewController <mydemoDelegate> //here is where it shows error with cannot find protocol declaration
VCTOUseDelegate.m
VCWithProtocol *obj = [[VCWithProtocol alloc] init];
obj.mydelegate = self;
I tried with Importing VCWithProtocol but not working as well
Instead of #class VCWithProtocol; write #import VCWithProtocol.h
And some good practice Use capital letter for first character of any class name and protocol.
e.g. MyDemoDelegate and avoid retaining delegate use
#property (nonatomic,assign)id<mydemoDelegate>mydelegate;
Synthesize your mydelegate, and call
[self mydelegate];
You need to add #import "VCWithProtocol.h" to the top of your .m file.
#property (nonatomic,**weak**)id<mydemoDelegate>mydelegate;
delegate always needs weak link

protocol method is not being called by the delegate object

Another case of protocol method not being called - NO idea what am I doing wrong here...
Here is the code, omitting unnecessary info...
first header file: AccessNumber.h
#protocol AddItemDelegate <NSObject>
#required
- (void) processSuccessful: (BOOL)success;
#end
#interface AccessNumber : UIViewController <UITableViewDelegate, UITableViewDataSource, ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate> {
id <AddItemDelegate> delegate;
}
#property (retain) id <AddItemDelegate> delegate;
#end
first .m file: AccessNumber.m - I am calling the protocol method from viewdidload just for testing purposes, it should basically get called from another method, but same thing for the sake of this convo (tried both of course)
#import "AccessNumber.h"
#import "History.h"
#synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
....
[[self delegate] processSuccessful:YES];
}
second file header: History.h
#interface History : UIViewController <UITableViewDelegate, UITableViewDataSource, AddItemDelegate> {
....
}
method implementation in history.m
- (void) processSuccessful: (BOOL)success {
NSLog(#"SUCCESS");
}
Appreciate any help. Thanks!
In the code i don't see something like:
theAccessNumber.delegate = self;
You must set the History instance as the delegate property of the AccessNumber instance.
Regards.
The code seems a little bit funny but you are not telling your "AccessNumber" class who is his delegate, even though you are making the "History" class implement the protocol established by yourself (otherwise you would get a warning).
You have to set the delegate for the "AccessNumber" class like this when setting it up from within "AccessNumber.m":
self.delegate = historyInstance;
or like this when setting it up from within "History.m":
accessNumberInstance.delegate = self;
There are very vew situations where you should retain a delegate, normally your delagate will outlive the object. So change your property from retain to assign. And be sure you are setting the delegate. Where are you doing it? If your object really depends on it you should be passing it in the constructor (iniWithDelagate). Try doing a NSLog before calling the delagate method just to see if it isn't nil.

Expected specifier-list before "MainViewController / Method -x not found return type defaults to id

I have the following imports in my EquivalenceClassGroup
#import "MainViewController.h"
but then in my property in EquivalanceClassGroup.h:
#property (nonatomic, assign) MainViewController *myController;
I get this error: "Expected specifier-list before MainViewController when compiling
If I change the import to: #class MainViewController and comment out the import of MainViewController.h that error goes away but then then XCode can't find the methods in my MainViewController from EquivalenceClassGroup.m so from here:
-(id)initWithLetterNumbers: (int)numOfLettersInWord enteredLetter: (NSString *) str controller:(UIViewController *)controller {
myController = (MainViewController *) controller;
letterArray = [myController getLetterArray];
[myController getLetterArray];
I get: "Method -getLetterArray not found return type defaults to id" on that last line
I have this method defined in the MainViewController: -(NSArray*)getLetterArray;
and there are no errors in that interface file or the m file. In the equivalence class if I type in [myController then space, I cant seem to find any methods.
From the MainViewController class I do import the EquivalenceClassGroup and use the methods without any problems
You have to make sure that you import the MainViewController.h file in your .m file. All the #class does is tell the interface that there is such a class, but it doesn't tell it anything about the class. That is what #import essentially does. The #class in the .h file is just good programming practice to make sure that you are doubling importing or anything. Hope that helps!
I think you have to write #import "MainViewController.h" in EquivalanceClassGroup.h file and
#class MainViewController in MainViewController.h file.
#class MainViewController
in your EquivalenceClassGroup.h
#import "MainViewController.h"
in EquivalenceClassGroup.m
This is called "forward declaration" and is quite common.

access a property in UIView in Viewcontroller?

I have been given a project to edit. I think this is a simple question but want to explain it in detail.I usually set up iPhone projects with interface builder and then have a view controller h and m file.
However this has been set up in a different way I am new to, the view has been coded.
The h file is a simple viewcontroller class like this:
#interface MainViewController : UIViewController
{
}
- (id)init;
#end
And then the m file has this:
#interface MainView : UIView
{
NSUinterger firstinterger;
}
- (id)initWithImages:(NSArray *)inImages;
#end
And then it has the #implementation MainView just after that with lots more code.
Further down however is where I need to add my code just after
#end
#implementation MainViewController
But I need to access the NSUinterger named first integer and I am unable to. I have tried a few ways of synthesizing etc. but I think I am doing it wrong. How I would get the value of it? I can access it in the code before the #implementation MainViewController but not after which is where I need it.
Synthesize the variable in MainView. Have an instance of the MainView in MainViewController and then you can access it by
MainView *mv = [[MainView alloc] init];
mv.firstInteger // gives you the variable.
NSUinterger firstinterger in your code shows no ';' at the end of that line, do you get compilation errors or is it just a typo in your question?