how can to call this:
-(BOOL)setCloudEnabledAndOverwriteCloudWithLocalIfConfirmed:(void (^)(void (^setConfirmationAnswer)(BOOL answer)))confirmationBlock
Thanks.
Massimo.
BOOL someResult = [instance setCloudEnabledAndOverwriteCloudWithLocalIfConfirmed:^(void(^setConfirmationAnswer)(BOOL answer)){
// do something if needed
setConfirmationAnswer(YES);
}];
[object setCloudEnabledAndOverwriteCloudWithLocalIfConfirmed:^(BOOL answer){
/* do your stuff here */
}];
Related
How to pass the parameter
-(void)errorValue:(void(^)(NSError*))error{
[self errMssg];
}
-(void)call{
(void(^)(NSError*))error;
[self errorValue ?];
}
Please let me know how to pass (void(^)(NSError*))error to the method!
#All
Thanks in Advance
You need to properly declare a block variable first. Then you just pass it by name like any other variable:
void(^myBlock)(NSError *) = ^(NSError* error) {
// Do something
};
[self errorValue:myBlock];
Alternatively, you can pass a block literal directly:
[self errorValue:^(NSError* error) {
// Do something
}];
I'm getting a EXC_BAD_ACCESS exception when I call performSelector:withObject: from a object that does implement the method I'm trying to call. Here's my code
SEL newSelector = NSSelectorFromString(#"mySelector:withCustomObject:");
[self performSelector:newSelector withObject:myCustomObject];
This causes a crash. However when I do this
[self performSelector:#selector(mySelector:withCustomObject:) withObject:myCustomObject];
it works.
Any ideas on why this is happening?
PS: none of the parameters are nil.
MORE CODE:
// My code to call this method
SEL newSelector = NSSelectorFromString(#"mySelector:withCustomObject:");
[self performSelector:newSelector withObject:self withObject:myCustomObject];
// this code is NOT called.
- (void) mySelector:(jObject *)sender withCustomObject:(jEvent *)customObject
{
NSDictionary *handlerData = [aProperty objectAtIndex:[event positionInMethodStack]];
NSString *newTitle = [handlerData objectForKey:#"newTitle"];
}
"mySelector:withCustomObject:" is the signature of a method with 2 arguments, such as
- (void)mySelector:(id)firstArgument withCustomArgument:(id)secondArgument { ... }
But you call performSelector:withObject:, which sends a message with only one argument to mySelector. The second argument is undefined, which probably causes the crash.
So if mySelector actually has 2 arguments, use performSelector:withObject:withObject:, otherwise fix the signature of the selector.
I want to call another method from the updateButtonPressed method.
This is what I tried:
-(IBAction) updateButtonPressed{
[self loadScrollViewWithPage];
}
But the problem is that the loadScrollViewWithPage method has arguments. That method is like this:
- (void)loadScrollViewWithPage:(int)page {
}
How can I call this method?
If I understand correctly, you are wondering how to pass arguments along with messages to objects, is that right? Try:
-(IBAction) updateButtonPressed{
int foo = 4;
[self loadScrollViewWithPage:foo]; // a colon, followed by the argument
}
I suggest you read up on the Objective-C language in general, though.
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/introduction/introobjectivec.html
- (IBAction) updateButtonPressed{
int tempValue=5;
[self loadScrollViewWithPage:tempValue];
}
-(void) callme {
//statements
here I call another method "callmeagain"
}
}
But it is not working. Is there another way to do it?
To call an ObjC method, use the syntax [foo methodName:param andAlso:param2 …] In your case, try
-(void)callme {
[self callmeagain];
}
Another Method could be
[self performSelector:#selector(callmeagain)];
It is basically the same thing as Kenny's Suggestion
For the method:
[NSThread detachNewThreadSelector:#selector(method:) toTarget:self withObject:(id)SELECTOR];
How do I pass in a #selector? I tried casting it to (id) to make it compile, but it crashes in runtime.
More specifically, I have a method like this:
+(void)method1:(SEL)selector{
[NSThread detachNewThreadSelector:#selector(method2:) toTarget:self withObject:selector];
}
It crashes. How do I pass in the selector without crashing, so that the new thread can call the selector when the thread is ready?
The problem here isn't passing a selector to a method, per se, but passing a selector where an object is expected. To pass a non-object value as an object, you can use NSValue. In this case, you'll need to create a method that accepts an NSValue and retrieves the appropriate selector. Here's an example implementation:
#implementation Thing
- (void)method:(SEL)selector {
// Do something
}
- (void)methodWithSelectorValue:(NSValue *)value {
SEL selector;
// Guard against buffer overflow
if (strcmp([value objCType], #encode(SEL)) == 0) {
[value getValue:&selector];
[self method:selector];
}
}
- (void)otherMethodShownInYourExample {
SEL selector = #selector(something);
NSValue *selectorAsValue = [NSValue valueWithBytes:&selector objCType:#encode(SEL)];
[NSThread detachNewThreadSelector:#selector(methodWithSelectorValue:) toTarget:self withObject:selectorAsValue];
}
#end
You can convert between selectors and string objects using the NSStringFromSelector() and NSSelectorFromString() functions. So you can just pass string objects instead.
Alternately, if you don't want to change your methods, you can create an NSInvocation to create an invocation for your method call (since it can set up invocations with non-object arguments), and then to call it do [NSThread detachNewThreadSelector:#selector(invoke) toTarget:myInvocation withObject:nil];
Use NSValue, like so:
+(void)method1:(SEL)selector {
NSValue *selectorValue = [NSValue value:&selector withObjCType:#encode(SEL)];
[NSThread detachNewThreadSelector:#selector(method2:)
toTarget:self
withObject:selectorValue];
}
NSValue is intended as an object-wrapper for arbitrary non-object types.
Please see: passing a method as an argument
If you don't want to specify an object just use nil.
[NSThread detachNewThreadSelector:#selector(method:) toTarget:self withObject:nil];
If you need to pass an object to the selector it would look something like this.
Here I'm passing a string to the method "setText".
NSString *string = #"hello world!";
[NSThread detachNewThreadSelector:#selector(setText:) toTarget:self withObject:string];
-(void)setText:(NSString *)string {
[UITextField setText:string];
}