How to call a method from another method in objective C? - iphone

-(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

Related

How to call a non-void function? Xcode

How do I call a non-void function? Normal [self methodName]; works. But how do I do this for a method that returns an NSString. I keep getting an error. For example:
+ (NSString *)formulateYQLRequestFor:(NSArray *)tickers
How do I call this? [self formulateYQLRequestFor]; gives me an error.
Sorry about the formatting, for some reason safari won't let me indent.
Thanks!
+ designates a class function. You call it with the class name, not an instance.
Instead of:
[self formulateYQLRequestFor:myArray];
Do this:
[MyClassName formulateYQLRequestFor:myArray];
Alternatively, you can do this:
[[self class] formulateYQLRequestFor:myArray];
You don't have to do anything with the return value if you don't want to. At least with ARC, the return value will be automatically released. However, since it's unlikely that the function does anything on its own, you probably should do something with the return value:
NSString *returnValue = [[self class] formulateYQLRequestFor:myArray];
// Do something with returnValue
Finally, if you want to call the function without passing in an array, you still need the array parameter, but perhaps the function will accept nil for the array:
NSString *returnValue = [[self class] formulateYQLRequestFor:nil];
There are two problems with your call to [self formulateYQLRequestFor];
Firstly, the method takes a parameter, which you haven't provided. Because of this, the compiler is looking for the method called formulateYQLRequestFor instead of formulateYQLRequestFor: This is significant, because the : is part of the method name in Objective-C. So you are trying to call a method that doesn't exist.
Secondly, self is sending a message to an instance of your class. The + in the method signature indicates that you have a class method, and so self does not respond to the method you are trying to call.
The correct way to do this is:
NSString *resultString = [[self class] formulateYQLRequestFor:someArray];
where someArray is a valid NSArray parameter.
I don't know what - (NSString *)formulateYQLRequestFor: does with the NSArray, but if it isn't necessary you can just call [self formulateYQLRequestFor:nil];. Alternatively you can call it with an empty array [self formulateYQLRequestFor:[NSArray array]];.

block delegate methods of class in iphone

I am having a problem I am working on a class which is subclass of UITextField.
Which will be used in many classes further.
But I don't want to let user to use it's delegate methods in any way.
Is there any way to do this ?
Override setDelegate: so that it throws an exception or logs an instruction on what to do. That way your API users will know what's actually going on.
-(void) setDelegate: (id <UITextFieldDelegate>) delegate
{
NSLog(#"*** Use the blocks API instead of calling %s", __PRETTY_FUNCTION__);
[self doesNotRecognizeSelector: _cmd];
}
Override the -setDelegate: method such that it never actually sets a delegate. You can just provide an empty method that fails to call super:
-(void) setDelegate:(id<UITextFieldDelegate>) delegate
{
// this method intentionally empty to prevent a delegate from ever being set
}

How to call a method from another method with arguments

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];
}

pass a variable between two methods

I was trying to pass a variable of type NSString from a method to another. what I have done is:
-(void)some{
NSString *lat = something
NSString *longt = somethingElse
[self test:lat:longt];
}
and then
- (IBAction)test:(NSString *)lat:(NSString *)longt{
doSomeThing
}
But my problem is that now the IBAction button is activated without my press.
how do I run the method IBAction under my control
you cannot pass arbitrary parameters through an IBAction, you can either pass the control sending the action or nothing:
-(IBAction)action:(id)sender;
or
-(IBAction)action;
The test method should not be an IBAction. Instead do this:
- (void)test:(NSString *)lat:(NSString *)longt{
//doSomeThing
}
-(IBAction)myAction:(id)sender
{
//call test from here
[self some];
}
You should use void as returm type of method not ibaction. If u need it for some action thn call it from there.
use
- (void) test:(NSString *)lat:(NSString *)longt
instead of
- (IBAction)test:(NSString *)lat:(NSString *)longt

Objective-C performSelector when to use colon?

Do you only postfix the method name with a : if you are calling a foreign object?
For some reason
[self performSelector:#selector(myMethod:) withObject:nil afterDelay:5];
Does not work but
[self performSelector:#selector(myMethod) withObject:nil afterDelay:5];
Does!
EDIT:
Declared in the implementation of a class but not the interface.
- (void)myMethod
{
// Some stuff
}
The colon represents a method argument. Since myMethod takes no arguments its selector can't have a colon. If you had multiple arguments like this...
- (void)myMethod:(id)method object:(id)object enabled:(BOOL)bool {
// Some Stuff
}
... the selector would be #selector(myMethod:object:enabled:)
In Objective-C the colons are part of the method name. That is, myMethod and myMethod: are distinct selectors (and in your case, only the latter exists).
For instance, for a method declared like:
-(void)doSomethingWithFoo:(int)foo andBar:(int)bar;
The selector is doSomethingWithFoo:andBar:.