pass a variable between two methods - iphone

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

Related

Calling a method in another method?

I've got a delegate method which just has a bit of code in there that puts a % sign on the end of the number entered.
- (void)textFieldDidEndEditing:(UITextField *)UItextfield {
NSString *oldTextFieldValue = UItextfield.text;
UItextfield.text = [NSString stringWithFormat:#"%# %%",oldTextFieldValue];
}
Could I instead of having that, have the following action
-(IBAction)Calculate:(UITextField *)UITextfield;
{
NSString *oldTextFieldValue = UItextfield.text;
UItextfield.text = [NSString stringWithFormat:#"%# %%",oldTextFieldValue];
}
And then in the Delegate function, call that action? Something like
-(void)textFieldDidEndEditing:(UITextField *)UItextfield {
[self Calculate:self]
}
I tried that, it doesn't work. I know it'll get me to the same result but I just want to know if it can be done. I think i'm asking can a method (Calculate) be called in another method (textFieldDidEndEditing) and how.
You are providing self as the method argument which is the instance of the class you are in. Which in this case is wrong since the method argument should be an instance of UITextField. Try instead [self Calculate:UItextfield] in your method.
Calling other methods from methods happen all the time in most programming languages.
It's a great way to split code up and reuse code in different places without having to copy/paste.
(This might be too basic for you. Sorry in that case)
Things may be easier to understand if you use standard naming conventions too. ('likeThis' for variables and method names; 'LikeThis' for class names)
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *oldTextFieldValue = textField.text;
textField.text = [NSString stringWithFormat:#"%# %%",oldTextFieldValue];
}
textField here, is the pointer to the UITextField object which just finished editing.
You want to pass this object to your new 'other' method.
[self calculate:textField];
self is a pointer to an instance of the current class. For example, in a UIViewController subclass called 'MyViewController', self refers to the current instance of this class.
Since the -calculate method is an instance method (beginning with a '-') it requires you to use self. The variable textField is passed after the colon.
- (void)calculate: (UITextField*)textField {
NSString *oldTextFieldValue = textField.text;
textField.text = [NSString stringWithFormat:#"%# %%",oldTextFieldValue];
}
Use only the IBAction keyword when you want the method to be called from a UIComponent in an xib or storyboard.

How to Pass parameters in IBAction?

I am accessing IBAction programatically & want to pass two parameter with this IBAction call.
Can any one suggest easy way...
IBActions are usually called by user interface elements, and they can't have an arbitrary number of parameters.
If you want to call the action method programmatically, you could abuse the sender parameter by passing a dictionary as an argument, holding the actual arguments you want to pass, like so:
- (void) foo
{
[self myAction: [NSDictionary dictionaryWithObject: #"bar" forKey: #"baz"]];
}
However, I would recommend creating an additional method with two parameters; the IBAction can call it with arguments appropriate to the sender, and programmatically you can call it using whatever arguments you need. This would be a possible outline for the code:
// The atual "logic" method, doing sth interesting
- (void) foo: (NSString *) s bar: (NSInteger) i
{
// some code
}
- (IBAction) myAction: (id) sender
{
// can be invoked by a button, or any view action
if (sender == self.buttonX) {
[self foo: #"x" bar: 42];
}
if (sender == self.buttonY) {
[self foo: #"y" bar: 4];
}
}
- (void) methodCallingFooBarProgrammatically
{
[self foo: #"s" bar: 17];
}
You can pass an array in the IBAction method like this:
-(IBAction)method:(id)sender
{
[sender objectAtIndex:0];
}
or you can do it like this:
-(IBAction)methodName:(NSString *)stringName:(NSString*)stringName2
{
// You can pass an array and even a dictionary
}
IBAction method follow a spesific pattern either
- (IBAction)action:(id)sender;
or
- (IBAction)action:(id)sender forEvent:(UIEvent *)event;
where sender is the UI object that sends the event, and event being the UIEvent itself.
If you are not sending these arguments then you don't want an IBAction method. Define a normal method that takes the two arguments you want and if you IBAction methods need to call it as well then do that. IBAction methods are defined as IBAction so that interface builder can find them in your code, so there is no reason to define an IBAction method that does not follow the pattern above.
the IBAction methods could receive two parameters about the sender object and the touch event, you cannot "pass" anything, you can receive only these via:
- (IBAction)action
- (IBAction)action:(id)sender
- (IBAction)action:(id)sender forEvent:(UIEvent *)event
you could use only the sender's tag property to pass a custom identifier as NSInteger.
HERE IS THE POINT
everything else what you would like to "pass" must be exists on your Model layer already! if you know what it is...
therefore, you can reach your datas from the Model layer after you receive the action.

Calling function in xcode iPhone

If I have this function for button click
-(IBAction)loginButton:(UIButton *)sender{ }
How can I call this button function from this another method
-(void)increaseAmount {
myNumber = myNumber+0.01;
if (myNumber >= 1) {
[self loginButton:sender];
}
progressViewAuto.progress = myNumber;}
I have tried the above code but getting error that USE OF UNDECLARED IDENTIFIER 'sender'
Simply pass in nil or even self.
And, if you want to be perfectly correct, you can link your UIButton to IBOutlet UIButton* theLoginButton; and then say [self loginButton:theLoginButton];.
But this would only be needed if you actually reference the sender in your loginButton routine, and that's the exception rather than the rule.
You probably just want to send nil, like this:
[self loginButton:nil];

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

Calling Button-Clicked Function From Code

I have a function set up in my code like so:
-(IBAction) buttonClicked: (id) sender{
//various expressions here
}
The question is, how can I call this buttonClicked() function from somewhere else in the code, without requiring a user click the button. For example, how can I call it from viewDidLoad()?
Thank you for any help.
If you're calling in the same file, you can use:
[self buttonClicked:self];
Depending on what your method does.