In this part of my code I get a warning message saying it doesn't implement the custom protocol I made.
detailViewController.delegate = self;
How do I implement the protocol?
Then when I run the program it crashes saying
'-[DetailViewController setDelegate:]: unrecognized selector sent to instance 0x6a1c450'
Declare your class like this:
#interface DetailViewController : UIViewController <MyProtocol> {
// Class stuff
}
Related
I'm trying to combine two plugins written in Objective-C. I have Plugin1 and Plugin2 which execute fine enough independently. I'm trying to add the Plugin2.m code to my Plugin1 Classes folder and execute both at the same time.
When I do this, Plugin1.m seems to execute first, I guess because it has IBAction calls and Plugin2.m doesn't? This is fine, but I'd like to run Plugin2.m within a function in Code1.m. So In the code below, when the IBAction call in Plugin1 is initiated I would like it to do what Plugin2 normally does and then continue with Plugin1 methods.
Plugin1.h:
#import Plugin2.h
#interface Plugin1: NSWindowController {
...
}
+(void) Plugin2;
#end
Plugin1.m:
#import "Plugin1.h"
#import "Plugin2.h"
#implementation Plugin1
-(id) loadPlugin1
{
...
}
-(IBAction) computeStuff:(id)sender
{
[self Plugin2];
//Plugin2* testRun = [Plugin2 alloc] init];
...do other stuff
}
#end
Plugin2.h
#interface Plugin2 : PluginFilter {
...
}
#end
Plugin2.m:
#import Plugin2.h
#implementation Plugin2
-(void) initPlugin
{
...
}
#end
Unfortunately I can't troubleshoot this from within Xcode, I have to install and test the plugin on my program to test. But when I keep an eye on Console and try the above I get "-[Plugin1 Plugin2]: unrecognized selector sent to instance 0x7....
in your plugin1 interface you defined
+(void)plugin2;
but implemented no method
+(void)plugin2 {
}
when you invoke + method you need to tell which class you call the method from because self refers to an object and not the class.
[self.class plugin2];
// OR
[Plugin1 plugin2];
Hints: try to follow naming conventions in objective-c.
This will help you distinguish if a definition is a -method: or Class
Consider reading about delegate design patterns and the use of <Protocols>
Also define special -initPlugin in your Plugin2 implementation and use a return type. Otherwise you initialise nothing.
-(instancetype)initPlugin;
I'm doing some cocoa programming follows the Cocoa programming for Mac OS X, I'm trapped in Chapter 8(NSArrayController), I'm following the guide defined in that book, but I don't know why the app always raising unrecognized selector sent to instance error.
My code is here RaiseMan, If you have any suggestion, Thanks very much.
I've fixed it myself. The behaviour of NSArrayController add method is to copy an object, not holding the pointer of the object, What I've encountered is I haven't implement NSCoping protocol in class Person, I fixed it like this:
in Person.h declar NSCoping:
#interface Person : NSObject <NSCopying> {
NSString *personName;
float expectedRaise;
}
and in Person.m implement it:
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
I have an iPad app that I am porting from Windows C#. I created the entity and attributes (parital listing in image):
This is how I defined the attributes in the app:
This is the code to initialize MagicalRecord (v2.3) in AppDelegate.m file:
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:#"bim.sqlite"];
defaultContext = [NSManagedObjectContext MR_defaultContext];
I have this code in the DetailViewController.m file:
#import "AppDelegate.h"
#import "Books.h"
#class Books;
#interface DetailViewController : UIViewController <UITextFieldDelegate> {
}
This is the code causing the error:
- (IBAction)aAddRecordToCoreData:(UIButton *)sender {
Books *bks = [Books MR_createEntity];
bks.aAuthor = oAuthor.text;
bks.aBinding = oBinding.text;
bks.aBookDescription = oDescription.text;
This is the error I'm getting:
-[Books setAAuthor:]: unrecognized selector sent to instance
As far as I can tell, this is the same code I have used in another iPad app which is working correctly in the App Store. I doubt it has anything to do with MagicalRecord, but one never knows. What am I missing? or doing wrong?
I have developed a generic controller for sliding UIView and I manage UIViewController (and obviously subclass ) and must call a specific method (freeze) only if a subclass of UIViewController respond to this method:
-(void)freezeRootViewController
{
if([_rootViewController respondsToSelector:#selector(freeze)])
[ ((id) _rootViewController) freeze];
}
I don't know the class of _rootViewController but I know it is a subclass of UIViewController, for this reason I have tried to cast my _rootViewController to ID, but I can't compile:
Environment: XCode 4.5.1, iOs 4.3+ with ARC
Error: "No know instance method for selector 'freeze'"
Note: I CAN'T force the developer to use a specific UIViewController subclass for the _rootViewController.
After checking if a "generic" type responds to a selector you invoke it by using performSelector:/performSelector:withObject: if it is a simple 0-1 parameter method, otherwise use an NSInvocation. Since freeze has no arguments you would just use performSelector:.
-(void)freezeRootViewController
{
if([_rootViewController respondsToSelector:#selector(freeze)])
[_rootViewController performSelector:#selector(freeze)];
}
This seems to be a warning rather than an error - you should be able to compile your code and just ignore this diagnostic message. However, if you want to get rid of it completely, you can declare a protocol and do the cast as follows:
#protocol MyProtocol <NSObject>
- (void)freeze;
#end
[(id <MyProtocol>)_rootViewController freeze];
I'm trying to implement a protocol.
I've looked at the documentation here and I understand the concepts although I think I'm missing a few things.
I'm trying to make a view that the user taps on a filename in a table view triggering 'didSelectRowAtIndexPath' which will in turn notify the delegate that the user has selected a file (triggering didSelectFileName in the delegate) and passing the fileName. I've declared the protocol as follows;
#protocol FileList <NSObject>
- (void)didSelectFileName:(NSString *)fileName;
#end
My questions are:
How do i set the 'fileName' value so that when 'didSelectFileName' is called it has the current value in it
How do I tell my code to trigger 'didSelectFileName' in the delegate.
You cannot just send a message to a protocol (nor setting values). You send the message to a class that conforms to the protocol.
When you say a class conforms to a protocol (#interface MyClass : NSObject <MyProtocol> { etc) you can safely send any messages to the class with the selectors that conform to the methods in the protocol.
So if we take your protocol for example we can have a class that can send messages to a delegate:
#interface MyClass : NSObject {
id<FileList> _delegate;
}
#end
#implementation MyClass
- someMethod {
NSString *fn = #"Hello.";
[_delegate didSelectFileName:fn];
}
#end
Just make sure you implement the methods that are in your protocol in you delegate.
You don't need to redefine the methods in the interface of your delegate class.
Here are some good reads about protocols:
http://www.otierney.net/objective-c.html#protocols
http://mauvilasoftware.com/iphone_software_development/2008/05/a-brief-intro-to-objective-c-p.html
Protocol versus Category
//In table View method
- (void)tableView didSelectRowAtIndexPath....... {
UITableViewCell *cell = [tableView methodToGetCell];
if(delegate && [delegate respondsToSelector:#selector(didSelectFileName:)]){
[delegate didSelectFileName:cell.text];
}