How can i use MBProgressHUD in multiple ViewControllers? - objective-c

I have a problem using the MBProgressHUD library. I can use it normally in my main ViewController, but, then, when i try to import the .h file in other view controller, i have the following error:
Redefinition of enumerator 'MBProgressHUDModeIndeterminate'
Redefinition of enumerator 'MBProgressHUDModeDeterminate'
...
Does anyone knows how can i manage it?
Thanks
EDIT: I have the following imports in my main view controller
#import "ViewController.h"
#import "FMDatabase.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#implementation ViewController
When i try to put this imports in other view controller, i get the errors:
#import "FinalViewController.h"
#import "Parse/Parse.h"
#import "MBProgressHUD.h"
#implementation FinalViewController
I have used the MBProgressHUD library from this link https://github.com/matej/MBProgressHUD

You're probably #include-ing MBProgressHUD.h rather that #import-ing it. #import ensures that it's only imported once.

I've used MBProgressHUD exclusively in my apps, and across multiple view controllers and never seen that issue before.
You could try and add your import statement to your .pch file rather than import for each view controller. It will add the header even if you don't use it, but might be worth a go.
Also, make sure you only have one copy of the header file in your project. The error you are showing almost looks like it is being defined twice which would indicate too many headers.

Related

Circular Importing of ViewController

In Appdelegate I am Importing Some Class for showing the view and in that view I have required to import the Same AppDelegate for accessing some method and variable but when I am doing this. Its shows circular import error.
I have tried by importing by #class in .h file and import in .m file.
In both classes you should only use #class in the .h file and then add the #import in the .m file. This is the correct solution for the problem you describe.
Note that the circularity could be bigger because a 3rd class could be #imported and it in turn #imports another class to make the circular link. Generally you should use #class in your .h files unless you have to #import (for the superclass and #protocols you implement).

Why is Core Plot delegate protocol not found?

I try to use Rays tutorial of Core plot for my iPad App
I use a SplitViewController
And I want to have a scatter plot in my DetailViewController
I change my Code to:
#interface GWDetailViewController : UIViewController <UISplitViewControllerDelegate,CPTPlotDataSource>
and now I get this error:
Cannot find protocol declaration for 'CPTPlotDataSource'
to add frameworks and librarys was no problem
What is wrong?
Make sure that the .h file for your view controller contains the following import statement:
#import "CorePlot-CocoaTouch.h"
If you dont want to import header file every time, you can Import it in .pch file for once.
In your scenario ,Open .pch file(usually placed in supporting file folder ) and add following code.
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "CorePlot-CocoaTouch.h"
#import "CPDConstants.h"
#import "CPDStockPriceStore.h"

how to access a newly created class in objective c

I get the feeling that this is a very stupid question, but I cannot find an answer anywhere. I've created a new class in an XCode project called "Word", with its own .h and .m files, which inherits from NSString, and has its own declared instance variable, method, etc. How do I access it within the viewcontroller.h and .m files? I though it would just show up in the little suggestion box in XCode, like the pre-made classe, but it doesn't. How do I use my new class?
#import "Word.h"
I will note that subclassing NSString is extremely uncommon and almost never what you mean to do. You probably meant to have Word have an NSString property.
in your .h, at the top type #import " then start typing Word.h. It should autocomplete if the .h and .m files are in your project correctly.
You just need to add the following to your ViewController.h
#import "Word.h"

Strange Delegate Protocol Declaration error?

So in a header file I have the following code:
#import <AudioToolbox/AudioToolbox.h>
#interface alertController:UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer *player;
}
I have both the framework linked and It works just fine in another file within the same project, but I am getting the error that it cannot find the protocol declaration for "AVAudioPlayerDelegate", nor does it recognize the line AVAudioPlayer * audioPlayer because it is an unknown type name. Any ideas why this isn't working despite my import statement, including the framework, and the fact that it's working fine in another file?
AVAudioPlayer (and its delegate) come from AVFoundation, not AudioToolbox. Your other project probably imports <AVFoundation/AVFoundation.h> either through a separate included header file or through your PCH. You should replace that #import with
#import <AVFoundation/AVFoundation.h>

Declaring a subclass in ObjC fails

Here's what I'm trying to do:
I want to put away some methods into separate .h and .m files for a better overview of my code.
So basically I have the myViewController which I want to extend with the method myReactionOnAnimationDidEnd: as a category.
So I declared in "myCustomClasses.h" the following to extend it with my desired method:
#import "myViewController.h"
#interface myViewController (myReactionOnAnimationDidEnd)
- (void)myReactionOnAnimationDidEnd:(NSNotification *)aNotification;
#end
The implementation in "myCustomClasses.m" is:
#import "myCustomClasses.h"
#import "myViewController.h"
#implementation myViewController (myReactionOnAnimationDidEnd)
- (void)myReactionOnAnimationDidEnd:(NSNotification *)aNotification {
self.myLabel1.text = #"Test";
}
#end
The Compiler throws a build failed error "Cannot find interface declaration for 'myViewController'"
So here's my questions:
The first weird thing is, that everything works fine if I do exactly the same for UIViewController instead of myViewController. But since myViewController is a subclass of UIViewController, why shouldn't it work for myViewController as well(#interface iSnahViewController : UIViewController)?
The other weird thing is that the #implementation in "myCustomClasses.m" works just fine if I skip on the #declaration completely. Now how can that be??
Thank you guys!
Any help much appreciated!!
Hans
Now, the funny thing is, that this very same building error comes up even if I create the category with the New File -> ObjC - Category Template. It basically creates the following two files:
in the header file:
#import "myViewController.h"
#interface myViewController (myCategories) //<-- "Cannot find interface declaration for 'myViewController'"
#end
and with the .m file
#import "myViewController+myCategories.h"
#implementation myViewController (myCategories)
#end
And that's already enough to bring up the error from above.
myCustomClasses.m should probably include myCustomClasses.h and not myViewController.h, so the compiler sees the category declaration. Since myCustomClasses.h already includes myViewController.h, the compiler should have all necessary declarations.
I finally got it working!
As the building error occured, I was importing the Category-.h File into the primery Class's .h file:
#import "myViewController+myCategories.h" //<-- in the "myViewController.h"
which lead to the building error from above. This seems to be wrong! So don't do that.
I still don't fully understand how the "myViewController" Class gets to know about it's categories without even having their .h files imported, but as this appears to be a working way of how this is done, I wanted to share with you.
Thanks everybody for helping!!