I have created a math based application in Xcode 4.4. I am using tabbar based app with the help of storyboard.
I have written all my math functions in a separate class called CalculationMethods which is the subclass of NSObject.
My ViewController:
// FirstViewController.h
#import <UIKit/UIKit.h>
#import "CalculationMethods.h"
#interface FirstViewController : UIViewController
#end
// FirstViewController.m
#import "FirstViewController.h"
#import "CalculationMethods.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%f",[self julianDateFinder: [self currentDayMonthAndYearFinder]]);
}
#end
As you can see I have included my CalculationMethod.h file in both FirstViewController.h and the FirstViewController.m file, but when I use the methods of that class such as julianDateFinder and currentDayMonthAndYearFinder, Xcode errors, saying:
"No Visible #interface for 'FirstViewController' declares the selector 'CurrentDayMonthAndYearFinder'"
I am new to iOS and XCode. Can anyone help me solve the error?
In the FirstViewController, to use any of the methods in the CalculationMethods class, you need to create an instance of CalculationMethods. And then access a method using this syntax: [instanceOfCalculationMethods aMethodInCalculationMethods];
For example in your case, try this:
In the FirstViewController.h file, before the #end:
CalculationMethods *_calculationMethods;
And in the viewDidLoad method:
_calculationMethods = [CalculationMethods alloc] init];
NSLog(#"%f",[_calculationMethods julianDateFinder: [_calculationMethods currentDayMonthAndYearFinder]]);
I think you're misunderstanding how Objective C works slightly.
Without adding details of the CalculationMethods.h header file I can't help you much, but that compiler warning is telling you that FirstViewController doesn't have the method currentDayMonthAndYearFinder.
The reason that this is the case is because you're calling performing the selector CurrentDayMonthAndYearFinder on self which in the context of your FirstViewController instance is in fact the instance of FirstViewController
You said it yourself that your method CurrentDayMonthAndYearFinder is on your CalculatorMethods class so I suggest you either create an instance of your CalculatorMethods class or call the class method named CurrentDayMonthAndYearFinder on your CalculatorMethods class.
The problem here is weather or not you've defined instance methods or class methods.
Do yourself a favour and update your question with the contents of CalculationMethods.h
Related
I have updated Xcode 7.3. I observe that Xcode doesn't shown the error(warning) to implement the delegate method which are marked as #required in custom protocol declaration.
Project is building successfully without implement Required delegate method. However it gives "Unrecognised selector sent to instance" error when I tried to call delegate method at runtime, the error is obvious, but I am curious about why Xcode stops showing it at compile time.
In older version(e.g Xcode 6.4) It gives. If anyone have any idea then please share it.
Thanks in advance.
Here is my protocol declaration,
CustomViewController.h
#import <UIKit/UIKit.h>
#protocol MyCustomProtocol <NSObject>
- exampleDelegateMethod: (NSString*) test;
#end
#interface CustomViewController : UIViewController
#property (nonatomic, weak) id <MyCustomProtocol> delegate;
#end
And in another class I am listening to delegate method which I declared,
in .h file,
#import "CustomViewController.h"
#interface AnotherViewController : UIViewController <MyCustomProtocol>
in .m file,
((CustomViewController*)segue.destinationViewController).delegate = self;
I got the fault, instead of listening to delegate like,
((CustomViewController*)segue.destinationViewController).delegate = self;
Changed it to,
CustomViewController* subscriptionViewController = segue.destinationViewController;
CustomViewController.delegate = self;
This solved my problem.
Thanks for everyones effort who have spent time on this.
i want to know if the following situation can be done. I have inherited a project of iOS 8. I'd like to add a method to NSObject so that all objects can see it. and I have done this already. Here is the category implementation i have created:
#import "NSObject+myCategory.h"
#implementation NSObject (myCategory)
-(void)localizeStringIncludeDefault{
NSLog(#"about to localize String");
}
#end
Now i go to a MyViewController.m for example and try to call the method but its not working its not seen:
heres the .h:
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
BOOL someFakeBoolean;
IBOutlet UIView *someView;
//etc
}
#property (nonatomic,assign) IBOutlet MyViewController *myViewController;
-(void)localizeStringIncludeDefault;
and here is the implementation *.m and my issue:
#implementation MyViewController
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self localizeStringIncludeDefault] //this call cant be done, the method is not visible
}
- (void) viewDidDisappear:(BOOL)animated
{
//etc
}
//etc
I mean it makes sense to me that i'd have to import the "NSObject+myCategory.h" into the MyViewController header to use the category but because i've inherited this code it already has a base. I dont want to go into every .h file and import this. Is a easy way to extend object so that EVERY object class sees my method ?
One option would be to add the category .h file to the pch file. Then it will be seen by every class in your project without the need to import it explicitly.
Declare your global variables or declarations in your pch file or rather make a Global.h and just import this in your pch file (helps a lot in reusability). You can declare extern items as well in your Global.h and populate in App Delegate
Here is my TextValidator class:
//TextValidator.h
#import <Foundation/Foundation.h>
#interface TextValidator : NSObject
- (BOOL) isValidPassword:(NSString *)checkPassword;
- (BOOL) isValidEmail:(NSString *)checkString;
- (BOOL) isEmpty:(NSString *)checkString;
#end
// TextValidator.m
#import "TextValidator.h"
#implementation TextValidator
- (BOOL) isEmpty:(NSString *)checkString
{
return YES;
}
- (BOOL) isValidPassword:(NSString *)checkPassword
{
return YES;
}
- (BOOL) isValidEmail:(NSString *)checkString
{
return YES;
}
#end
This is the way I try to initialise the TextValidator class in ViewController.m:
//ViewController.h
#import <Foundation/Foundation.h>
#interface SignUpViewController : UIViewController <UITextFieldDelegate>
#end
//ViewController.m
#import "SignUpViewController.h"
#import "TextValidator.h"
#interface SignUpViewController ()
#property TextValidator *myValidator;
#end
#implementation SignUpViewController
- (void)viewDidLoad
{
[[self.myValidator alloc] init]; //iOS error: No visible #interface for 'TextValidator' declares the selector 'alloc'*
[super viewDidLoad];
}
#end
When I try to compile the code I get the following error:
No visible #interface for 'TextValidator' declares the selector 'alloc'.
TextValidator class inherits from NSObject and as far as I know init and alloc functions are already defined at the base class. So why does the program gives such an error?
Note that, I already checked this topic and it doesn't work for me.
My psychic debugger, without reading your code, tells me you're calling alloc on an object instance, rather than a class. The alloc method is a static method defined by classes (typically inherited from NSObject) that returns a new instance of the underlying object. You can't ask an instance to allocate itself!
Now looking at the code, I see that you want:
self.myValidator = [[TextValidator alloc] init];
to construct a new instance, and assign it to the myValidator property.
Replace
[[self.myValidator alloc] init];
with
self.myValidator = [[TextValidator alloc] init];
The error signals that you have not implemented the alloc instance method for self.myValidator, which is true. But that's a class method that applies for all NSObject objects.
Your syntax of creating object is incorrect. Correct code:
self.myValidator = [[TextValidator alloc] init];
If you experience this randomly (like when you are changing branches), not because you forgot to declare selector.
Go to file inspector > Target Membership
uncheck the targets
then check it again
This will refresh your project.pbxproj
Then if you build, you'll see your real problem
For Others :
Check the varible name is not like the class name.
Well it happend to me.
XXXViewController * XXXViewController = [[XXXViewController alloc] init];
Don't tell anyone like I did right now.
For those who get the error of "no visible #interface for declares the selector ..."
such an error usually happens when you have mistyped the name of the method, or that method doesn't belong to that class at all and doesn't exist in your class
I had this problem today and solved it on my own. Basically you could also not be satisfying all the requirements of the function / procedure.
Go into the class itself and make sure your declaring all the requirements.
I took the class out of the header library and compared it word for word to verify it matches the function using it.
I have two UIViewController sublcasses, both of them conform to the UITextFieldDelegate protocol. IOW, I have these classes.
# MyVC1.h
#interface MyVC1 : UIViewController <UITextFieldDelegate>
# MyVC1.m
#interface MyVC1 () {
// Private variable, so not a property
UITextField *_myTextField;
}
#end
#implementation MyVC1
- (void)viewDidLoad
{
_myTextField = [self textFieldwithPlaceHolderText:#"*Text"];
}
#end
SAME CODE for MyVC2 class, except of course the class name.
However, and this is the strange part, my code compiles for MyVC1, but NOT for MyVC2. For MyVC2, compiler says "No visible #interface for "MyVC2" declares the selector "textFieldwithPlaceHolderText". What am I missing for MyVC2? I've double- and triple-checked!
Like Jsdodgers said, textFieldwithPlaceHolderText is not a method of UITextFieldDelegate. Check your #imports section on both controllers - maybe VC1 imports a category for UIViewController class that adds that method to it. A category import would look like this: #import "UIViewController+_.h"
Ok, it's late, and I'm sleepy. MyVC1 defines the textFieldwithPlaceHolderText method, but NOT MyVC2. I cut/past the viewDidLoad section, but forgot to cut/paste the method. Sorry to waste your time.
copy the method "textFieldwithPlaceHolderText" in myVc2.h in interface a
and implement in myvc2.m if i post anything wrong sorry for that
I have 2 classes geoViewController and geoMainViewController
I have a method in the geoMainViewController called getFoo
It looks like this:
- (NSString *)getFoo
{
NSString* foo = #"This is foo";
return foo;
}
I am trying to call getFoo from the geoViewController class.
I have #import "geoMainViewController.h" in my geoViewController m file.
I am trying instantiate the geoMainViewController class and call the getFoo method from the viewDidLoad in my geoViewController class like this:
- (void)viewDidLoad
{
[super viewDidLoad];
geoMainViewController* mainVC = [[geoMainViewController alloc] init];
NSString* myFoo = [mainVC getFoo];
}
It seems to be instantiating the geoMainViewController class fine but I am getting an error on NSString* myFoo = [mainVC getFoo];
The error is - no visible #interface for 'geoMainViewController' declares the selector 'getFoo'
I am sure I am missing a step because I am very new to Objective C. I am just not sure what I am doing wrong.
Any help on this would be great.
Thanks!
In your geoMainViewController.h you should declare the selector to be visible:
-(NSString *)getFoo;
Did you put - (NSString *)getFoo in your geoMainViewController.h ?
You have to make those methods visible to the outside of your object through the .h file, so other objects know which selectors they respond to. Did the autoComplete fill in the message per chance?
#import <Foundation/Foundation.h>
#interface
{
}
#property (nonatomic,strong) ;
#property (nonatomic,strong) ;
#property (nonatomic, strong) ;
- (NSString *)getFoo
#end
EDIT: (You could also just make Foo a property by the way)
Did you declare it in your header file?
Header file contains all the function declarations in the .h file and you only include the .h file in your class. So it depends on .h file. .h file will have all the functions as the .m file.
Hope it helps you.
You are misunderstanding how to use a view controller. While you can technically create an instance of a view controller in order to call one of its methods, you shouldn't do so. The normal approach is that the view controller is part of the view hierarchy and you can call methods on it when you have access to that instance. You are missing something fundamental here.
Your actual error is a missinh method declaration, I would suspect, but you have bigger problems to solve first.