Is it possible to get the argument from the selector varibale.
For Example
-(void)methodTest:(NSString*)someArg{
SEL selector = #selector(methodTest:);
[self testCall:selector];
}
-(void)testCall:(SEL)selectorArg{
//I would like to get the parameter from the selector (selectorArg)
}
My Questions:
1. Does the selector has the argument, someArg? If not, how to create the selector variable with argument.
2. What is the other way around to do the same?
Just curious to know.
If you create a selector like:
SEL selector = #selector(methodTest:);
It means (Note the :) that the selector expects an argument.
You can pass argument to such method like:
[self performSelector:selector withObject:argument afterDelay:0.0];
And the method syntax will be:
- (void)methodTest:(id)someArgument;
If the selector is created by the following syntax:
SEL selector = #selector(methodTest);
Then you can't pass any argument to this selector.
You can call like:
[self performSelector:selector withObject:nil afterDelay:0.0];
And the method syntax will be:
- (void)methodTest;
I'm not totally understand what you searching for, but I'm guessing you need
performSelector method. Please check this SO thread: iOS - How to implement a performSelector with multiple arguments and with afterDelay?
Related
I want to use a selector to call a func in my views controller, I want ot pass the number of a channel through to the func. This needs to be an Int, but I cant get the selector to respond correctly. How can i resolve this?
- (void)sendBusGetPar {
[self.viewController performSelector:#selector(getBusParams:) withObject: self.bus.channelNumber];
}
And this is the recieving func in the controller
- (void)getBusParams:(NSInteger)busChannel {
[[self tcpControl] sendMessage:cmd(GETPAR) Module:module(BUS) ModuleIndex:busChannel
Section:section(CONTROL) SectionIndex:0
Unit:unit(FADER) UnitIndex:0
Param:param(POSITION) Value:0];
}
I know its to do with conversion and types but i cant see why i cant send a channel number through as an Int in this case?
I have also tried this but the selector doesnt respond so i assume its wrong?
[self.viewController performSelector:#selector(getBusParams:) withObject: [NSNumber numberWithInteger:self.bus.channelNumber]];
performSelector: should not be abused for normal method calling.
Why don't you just call the method directly?
[self.viewController getBusParams:self.bus.channelNumber];
If your viewController has a generic type, cast it to the correct type:
[(MyViewController *) self.viewController getBusParams:self.bus.channelNumber];
The method says withObject. Unfortunately NSUInteger is not an object.
A suitable solution is to create an NSNumber object from the scalar integer and pass that.
[self.viewController performSelector: #selector(getBusParams:)
withObject: #(self.bus.channelNumber)];
On the receiver side you need to cast it down with the unsignedIntegerValue
property.
- (void)getBusParams:(NSNumber *)busChannel
[[self tcpControl] sendMessage:cmd(GETPAR)
Module:module(BUS)
ModuleIndex:busChannel.unsignedIntegerValue ...
In the case of method over riding in objective c how selector knows that which method needs to call via selector?
As we dont pass any arguments in slector section...
Ex:
in tmp.m file
There is 2 methods with different arguments
-(void)details
{
}
-(void)details:(NSDictionary *)result
{
}
And when m call another method with the use of selector as:
[mc detailstrac:[[NSUserDefaults standardUserDefaults] valueForKey:#"userID"] tracid:self.trac_id selector:#selector(details:)];
How selector knows to call which method !
I have checked that
-(void)details:(NSDictionary *)result
{
}
this method is called every time then what about
-(void)details
{
}
this ?
Selector will know on the basis how you call the method like from your example,
[mc detailstrac:[[NSUserDefaults standardUserDefaults] valueForKey:#"userID"] tracid:self.trac_id selector:#selector(details:)];
when you call #selector(details:) then the selector will call this method
-(void)details:(NSDictionary *)result { }
And When you call #selector(details) then the selector will call
-(void)details { }
The main difference here is #selector(details) and #selector(details:).
Hope you understand my point!
Happy Coding!
i have a class MatchDayDataController , having a method pushIncompleteDataToServer.
from another class , SummaryVC.m i want to call pushIncompleteDataToServer in performSelectorInBackground.
Code:
MatchDayDataController *sharedDataController = [MatchDayDataController sharedDataController];
[self performSelectorInBackground:#selector([sharedDataController pushIncompleteDataToServer]) withObject:nil];
It shows some syntax error in performSelectorInBackground. What i missed here? pls guide.
[self performSelectorInBackground:#selector([sharedDataController pushIncompleteDataToServer]) withObject:nil];
This would make the code to search for the method in the same class
It should be:
[sharedDataController performSelectorInBackground:#selector(pushIncompleteDataToServer) withObject:nil];
which would call the method in the sharedDataController class
Also, in the method performSelectorInBackground: withObject: the withObject is for the parameters to be passed to the selector method. I this case, since there are no parameters, we pass nil.
Try this,
[sharedDataController performSelectorInBackground:#selector(pushIncompleteDataToServer) withObject:nil];
instead of
[self performSelectorInBackground:#selector([sharedDataController pushIncompleteDataToServer]) withObject:nil];
You need to replace self with sharedDataController.
[sharedDataController performSelectorInBackground:#selector(pushIncompleteDataToServer) withObject:nil];
The selector will be performed on the receiver of the performSelectorInBackground message, and self doesn't implement that method.
I am relative new to the usage of selectors so am playing around with it, I do not want to pass the selector any object, instead it´s purpose is only tell the class to update itself. I declare them as any other method, but only when it takes a parameter does it work, why is this?
//This works
- (void) updateButtonImageState:(id)object;
// Calling it
[cell performSelector:#selector(updateButtonImageState:) withObject:#"object"];
-
//This crashes
//Declaring the selector
- (void) updateButtonImageState;
// Calling it
[cell performSelector:#selector(updateButtonImageState:)];
You should not use colon at the end of selector name then:
[cell performSelector:#selector(updateButtonImageState)];
I would like to be able to execute a piece of code that is defined in a string. I am aware of performSelector: but the object that will perform the selector is going to be different.
Example strings
[[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasFlash]
[UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]
So what I would like to do is something along the lines of
SEL selector = NSSelectorFromString(#"[[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasFlash]");
if (selector) {
// Show flash buttons
}
You can not fire a selector that calls a nested method call.
Selectors are only method names with showing number of arguments as method:abc:yxa:
As the statement below:
SEL selector = NSSelectorFromString(#"[[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasFlash]");
is calling
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
then
[objectReturnedByAbove hasFlash]