tobros91
tobros91

Reputation: 668

How to call my method in cocoa, self doesn't work

Working on a program that will record some things from the webcam when a user presses physical buttons connected to the mac via phidgets. Have call methods on other places in my app simply doing [self method: input], but on one place it doesn't work. What could be wrong?

This is the method i want to run if i get inputchange in my program.

Also i do -(void)reportButton2:(NSInteger)inputVal:(NSInteger)inputInd; in my .h file.

-(void)reportButton2:(NSInteger)inputVal:(NSInteger)inputInd {

//NSLog(@"phidget för port = %%d med signal %%d", ind, val);

if(inputVal == 1)
{

    NSError* error;

    NSFileManager* deleteMgr = [NSFileManager defaultManager];
    NSString* path = @"/Users/Shared/tempFile.mov";
    [deleteMgr removeItemAtPath:path error:&error];

    [mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:@"/Users/Shared/tempFile.mov"]];

}
else if(inputVal == 0)
{
    [mCaptureMovieFileOutput recordToOutputFileURL:nil];
}  
}

The code below give me result if imput from the buttons change. Here i just can't seem to call reportbutton2.

If i try to use [self reportButton2..] in gives me "Use of undeclared identifier 'self'"

int gotInputChange(CPhidgetInterfaceKitHandle phid, void *context, int ind, int val) {

what to do here?

return 0; 

}

Upvotes: 1

Views: 2933

Answers (5)

alexchernyy
alexchernyy

Reputation: 1216

You can also use your delegate.

SomeNameAppDelegate *delegate = (SomeNameAppDelegate *)[[NSApplication sharedApplication] delegate];
[delegate yourMethodName];

It works like in example if your target method in AppDelegate class. But when you have an access to delegate, you can create a pointers to necessary classes and use them over delegate.

Upvotes: 2

Peter Hosey
Peter Hosey

Reputation: 96323

I usually put something like this at the top of callbacks like gotInputChange:

MyObject *self = (id)context;

Then I can use self throughout the function as if it were a method.

The other thing being in a function makes harder is asserting conditions. The regular assertion macros, such as NSAssert and NSParameterAssert, require both of the implicit arguments to every method (self is one of them) to exist. In a C function, you must use NSCAssert, NSCParameterAssert, etc. instead.

Upvotes: 2

tobros91
tobros91

Reputation: 668

Okay thanks! Din't have any clue that it was C would never have solved it by myself. Did some googeling and this one did the trick for me.

[(id)context performSelectorOnMainThread:@selector(reportButton2:)withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:ind], [NSNumber numberWithInt:val], nil] waitUntilDone:NO];

Upvotes: 0

Rajavanya Subramaniyan
Rajavanya Subramaniyan

Reputation: 2155

Quickest but not soo nice way is to make your Class a singleton and access it from the gotInputChange function.

Upvotes: 0

mmmmmm
mmmmmm

Reputation: 32661

The problem is that gotInputChange is a C function not a Objective C method and so has no udea what self is as it does not belong to a class.

for [self reportButton2... = to work it needs to be a method in your class

Upvotes: 4

Related Questions