I have a UIView class and UIViewControllerclass. I want to do after finish some task in the UIView class need to call a method in UIViewController class. So I did like this.
In my ViewClass
`
#import "IntroView.h"
#import "FirstViewController.h"
#protocol SampleProtocolDelegate <NSObject>;
#required
- (void) processCompleted;
#end
#interface IntroControll : UIView<UIScrollViewDelegate> {
id <SampleProtocolDelegate> _delegate;
}
#property (nonatomic,strong) id delegate;
-(void)startSampleProcess; // Instance method
`
Then in My ViewController class
`
#import "IntroControll.h"
#interface FirstViewController : UIViewController<SampleProtocolDelegate>
{
IntroControll *delegate;
}
`
But it shows an error in #interface FirstViewController : UIViewController<SampleProtocolDelegate>
"Cannot find protocal declaration for 'SampleProtocolDelegate'"
Why is that and How can I solve this problem.
Please help me
Thank you
The issue comes because you are importing FirstViewController.h in IntroControll.h. And then you have done the protocol declaration.
Hence the FirstViewController.h is processed first, and it is unable to find the protocol declaration since ImtroControll.h is not processed yet.
Remove the import of FirstViewController.h from IntoControll.h, if it is not needed. Else forward declare FirstViewController.
#import "IntroView.h"
#class FirstViewController;
#protocol SampleProtocolDelegate <NSObject>
#required
- (void) processCompleted;
#end
Import FirstViewController.h in the IntroControll.m file.
Related
In MyModel.h, I declared a delegate variable like this:
#property(weak) IBOutlet id <MyProtocol> delegate;
I've also seen a delegate variable declared like this:
#property(weak) IBOutlet NSObject <MyProtocol>* delegate;
I'm wondering which I should use.
Also, Xcode 6.2 indicates I'm doing something wrong because when I connect the delegate outlet in IB, Xcode still shows an empty circle to the left of the declaration instead of a filled in circle. This is what I did:
1) In IB, I dragged on Object out of the Library onto the dock, and I changed its class to: MyModel.
2) In IB, I dragged another Object onto the dock, and I changed its class to: MyController. I declared the MyController class like this:
#interface MyController : NSObject <MyProtocol>
#property(strong) IBOutlet MyModel* model;
#end
3) In IB, I hooked up the delegate outlet for the MyModel Object to the MyController Object.
But in Xcode, it still shows an empty circle to the left of the line:
#property(weak) IBOutlet id <MyProtocol> delegate;
In other words, Xcode is saying the outlet is not connected to anything--yet my app is able to communicate with the controller using the delegate property.
If I delete <MyProtocol> from that line, the circle to the left of the line fills in, i.e. Xcode is saying the outlet is now connected to something. Is that an Xcode bug?
Here are the files for my HelloDelegate app:
MyProtocol.h:
//
// MyProtocol.h
// HelloDelegate
//
#class MyModel; //#import "MyModel.h" doesn't work for some reason
#protocol MyProtocol
-(void)sayHello:(MyModel*)model;
#end
MyModel.h:
//
// MyModel.h
// HelloDelegate
//
#import <Foundation/Foundation.h>
#import "MyController.h"
#interface MyModel : NSObject
#property NSString* name;
-(id)initWithName:(NSString*)name;
-(void)doStuff;
#end
MyModel.m:
//
// MyModel.m
// HelloDelegate
//
#import "MyModel.h"
#interface MyModel()
#property(weak) IBOutlet id <MyProtocol> delegate;
#end
#implementation MyModel
-(void)doStuff {
[[self delegate] sayHello:self];
}
-(id) init {
return [self initWithName:#"world"];
}
//Designated initializer:
-(id) initWithName:(NSString *)name {
if (self = [super init]) {
[self setName:name];
}
return self;
}
#end
MyController.h:
//
// MyController.h
// HelloDelegate
//
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
#interface MyController : NSObject <MyProtocol>
#property(strong) IBOutlet MyModel* model;
#end
MyController.m:
//
// MyController.m
// HelloDelegate
//
#import "MyController.h"
#import "MyModel.h"
#import <Cocoa/Cocoa.h>
#interface MyController()
#property(weak) IBOutlet NSTextField* label;
#end
#implementation MyController
-(void)sayHello:(MyModel*)model {
NSString* labelText = [NSString stringWithFormat:#"Hello, %#!", [model name]];
[[self label] setStringValue:labelText];
}
#end
AppDelegate.m:
//
// AppDelegate.m
// HelloDelegate
//
#import "AppDelegate.h"
#import "MyController.h"
#import "MyModel.h"
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#property (strong) IBOutlet MyController* controller;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[[[self controller] model] doStuff];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
The main difference comes along when you type something as id <SomeProtocol> and then try to send it a message such as respondsToSelector: and the compiler won't let you. It comes as a surprise - or at least it sure came as a surprise to me - that id <SomeProtocol> is not a form of id. The only messages you can send to such a beast without casting are those defined in the protocol. That's in stark contrast to id plain and simple, which can be sent any known message.
Thus, in my view, as in that of those who know better than I, NSObject <SomeProtocol>* is better, because now this thing is seen by the compiler as an NSObject, and can be sent all the messages declared for NSObject.
i have a problem with accessing value from the NSTExtField in different class here is the code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSTextField *numberOfPhrases;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize numberOfPhrases;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(#"%#",[numberOfPhrases stringValue]);
}
TestClass.h
#interface TestClass : NSObject
- (IBAction)doSomething:(id)sender;
#end
TestClass.m
#implementation TestClass
- (IBAction)doSomething:(id)sender {
NSLog(#"%#",[numberOfPhrases stringValue]); ?????????
}
You obviously can't access the text field value in another class without a link to it.
To access the text field's value you either need to have one more IBOutlet to it in this class or an IBOutlet to AppDelegate so that you can access its property.
TestClass.h
#interface TestClass : NSObject
{
IBOutlet NSTextField *numberOfPhrases; // connect it to the new referencing outlet of text field by dragging a NSObject object in your xib and setting its class to "TestClass"
}
- (IBAction)doSomething:(id)sender;
#end
OR an another option is to have a IBOutlet of AppDelegate in TestClass (because if you only create a new instance of AppDelegate and not its IBOutlet then a different instance of text field will be created and you will not be able to access the value of your text field)
TestClass.h
#interface TestClass : NSObject
{
IBOutlet AppDelegate *appDel; // connect in the xib
}
- (IBAction)doSomething:(id)sender;
#end
TestClass.m
#implementation TestClass : NSObject
- (IBAction)doSomething:(id)sender
{
[[appDel numberOfPhrases]stringValue]; //get the string value in text field
}
#end
The only thing you're missing is the addition to your TestClass.m file:
#import "TestClass.h"
#import "AppDelegate.h"
#implementation TestClass
- (IBAction)doSomething:(id)sender {
AppDelegate *theInstance = [[AppDelegate alloc] init];
[theInstance numberOfPhrases];
}
#end
You need to include the class header of AppDelegate.h in TestClass.m, then you simply call an instance through [[AppDelegate alloc] init]; You'll need to link your NSTextField to the Sent Actions in Interface Builder do:Something -> TestClass and Referencing Outlets numberOfPhrases -> AppDelegate.
Output:
2014-01-21 23:32:56.499 test[6236:303] Wonders Never Cease
I have created a plugin and have some 3 classes called PluginPrincipalClass,ClassOne and ClassTwo.I have the following code snippet in my classes.
#import <Cocoa/Cocoa.h>
#interface PluginPrincipalClass : NSObject
#end
#import "PluginPrincipalClass.h"
#implementation PluginPrincipalClass
- (NSInteger)getDownloadPercentage
{
NSLog(#"getDownloadPercentage");
return 10;
}
- (void)downloadSoftwareUpdate
{
NSLog(#"downloadSoftwareUpdate");
}
#end
#import <Cocoa/Cocoa.h>
#interface ClassOne : NSObject
#end
#import "ClassOneh"
#implementation ClassOne
- (void)ClassOneMethod
{
NSLog(#"ClassOneMethod");
}
#end
#import <Cocoa/Cocoa.h>
#interface ClassTwo : NSObject
#end
#import "ClassTwo.h"
#implementation ClassTwo
- (void)ClassTwoMethod
{
NSLog(#"ClassTwoMethod");
}
#end
And in my BaseApplication to load the plugin and to call the principal classes I have the following code snippet
NSString *zStrPlugInsPath = [[NSBundle mainBundle] builtInPlugInsPath];
NSArray *zAryBundlePaths = [NSBundle pathsForResourcesOfType:#"plugin"
inDirectory:zStrPlugInsPath];
NSString * zStrPathToPlugin = [zAryBundlePaths lastObject];
NSBundle *znsBundlePlugin = [NSBundle bundleWithPath:zStrPathToPlugin];
// instantiate the principal class and call the method
Class zPrincipalClass = [znsBundlePlugin principalClass];
id zPrincipalClassObj = [[zPrincipalClass alloc]init];
NSInteger downloadPer = [zPrincipalClassObj getDownloadPercentage];
NSLog(#"downloadPer = %ld",downloadPer);
[zPrincipalClassObj downloadSoftwareUpdate];
This is working fine.If I want to call the method of ClassOne or ClassTwo.How to instantiate and call those methods from my base application.Is it similar to create the object ClassOne and call the methods with that object?
I (if I understand your question correctly), you want to use NSBundle's classNamed: method :)
Like so:
Class zSecondaryClass = [znsBundlePlugin classNamed: #"StudentClass"];
I'm having a problem with a checkbox. I want to set it to 0 (unchecked) on app launch, but the checkbox is controlled by another class "myClass" for example.
Here's what I did:
I opened Interface Builder and put a checkbox (NSButton) in my window, dragged NSObject in my MainMenu.xib window, renamed it to say "myClass". Added an outlet called "myCheckbox" (NSButton) and linked it to the checkbox I created earlier. Finally, I added some things.
Here's the code for my myClass.m:
#import "myClass.h"
#implementation myClass
- (void) changeState
{
[myCheckbox setState:0];
}
#end
myClass.h
#import <Cocoa/Cocoa.h>
#interface myClass : NSObject {
IBOutlet NSButton *myCheckbox;
}
- (void) changeState;
#end
Then I made some changes in the AppDelegate files so they execute some things when the app is launched:
#import "UntitledAppDelegate.h"
#import "myClass.h"
#implementation UntitledAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
myClass * someClass = [[myClass alloc] init];
[someClass changeState];
}
#end
UntitledAppDelegate.h:
#import <Cocoa/Cocoa.h>
#interface UntitledAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
#property (assign) IBOutlet NSWindow *window;
#end
The purpose (if this works) is to set a value to the check box depending on the setting stored in the Defaults file.
The problem might be easy or too simple but I'm only a beginner...
Some help would be appreciated, Thanks !
- (void) awakeFromNib
{
[myCheckbox setState:0];
}
in myClass.m solved it.
I made a custom cocoa framework just to experiment and find the best way to make one but ran in to a problem using it. The framework project builds and compiles just fine, but when I use it in an xcode project I get the error, 'LogTest' undeclared. The name of the framework is LogTest
Heres the code to my app that uses the framework:
TestAppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <LogTest/LogTest.h>
#interface TestAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
#property (assign) IBOutlet NSWindow *window;
#end
TestAppDelegate.m:
#import "TestAppDelegate.h"
#implementation TestAppDelegate
#synthesize window;
- (void)awakeFromNib {
[LogTest logStart:#"testing 123":#"testing 1234"]; //This is the line where the error occurs
}
#end
Framework Code........
LogTest.h:
#import <Cocoa/Cocoa.h>
#import "Method.h"
#protocol LogTest //Not sure if this is needed I just wanted a blank header
#end
Method.h:
#import <Cocoa/Cocoa.h>
#interface Method : NSObject {
}
+ (void)logStart:(NSString *)test:(NSString *)test2;
#end
Method.m:
#import "Method.h"
#implementation Method
+ (void)logStart:(NSString *)test:(NSString *)test2 {
NSLog(test);
NSLog(test2);
}
#end
If anyone knows why I am getting this error please reply.
Thanks for any help
From what I see of the header files you just posted. LogTest is not a class but an empty protocol. You should be calling logStart:: on Method not LogTest
IOW. change it to
- (void)awakeFromNib {
[Method logStart:#"testing 123":#"testing 1234"];
}