Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-31-2011, 11:59 AM   #1 (permalink)
Registered Member
 
sacha1996's Avatar
 
Join Date: Mar 2011
Posts: 415
sacha1996 is on a distinguished road
Post Automaticly instead of pushing button

Hi ,
I have a button , but I want that the code wrote for the button works without pushing a button.

Code:
-(IBAction)Calculate:(id)sender{ 
	NSString *numberPT = self.number.text; 
	NSString *dividerPT = self.divider.text; 
	double resultInNum; 
	double numberdouble = [numberPT doubleValue]; 
	double dividerdouble = [dividerPT doubleValue];
	resultInNum = numberdouble / dividerdouble; 
	NSString *finalValue = [[NSString alloc] initWithFormat:@"%.2f", resultInNum]; 
	self.result.text = finalValue;
	
}
So that code works directly without pushing a button. Does anyone know how ?
Thanks !
sacha1996 is offline   Reply With Quote
Old 03-31-2011, 12:33 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default You could use a timer ...

Put this code in function without the id)sender part

Then call self Calculate on each timer click.

Otherwise just have this action fire anytime any of the text or num ers change.

Or do this on the view did load.

It all depends where and when you are getting the values.

Quote:
Originally Posted by sacha1996 View Post
Hi ,
I have a button , but I want that the code wrote for the button works without pushing a button.

Code:
-(IBAction)Calculate:(id)sender{ 
	NSString *numberPT = self.number.text; 
	NSString *dividerPT = self.divider.text; 
	double resultInNum; 
	double numberdouble = [numberPT doubleValue]; 
	double dividerdouble = [dividerPT doubleValue];
	resultInNum = numberdouble / dividerdouble; 
	NSString *finalValue = [[NSString alloc] initWithFormat:@"%.2f", resultInNum]; 
	self.result.text = finalValue;
	
}
So that code works directly without pushing a button. Does anyone know how ?
Thanks !
aldcorn@live.com is offline   Reply With Quote
Old 03-31-2011, 12:44 PM   #3 (permalink)
Registered Member
 
sacha1996's Avatar
 
Join Date: Mar 2011
Posts: 415
sacha1996 is on a distinguished road
Post

Hi , thankyou for your time.
But I'm quite new to this.
After I delete the id sender part. Than what is "call Calculate on each timer click"
Could you please give an example ?

Thankyou very much

Quote:
Originally Posted by aldcorn@live.com View Post
Put this code in function without the id)sender part

Then call self Calculate on each timer click.

Otherwise just have this action fire anytime any of the text or num ers change.

Or do this on the view did load.

It all depends where and when you are getting the values.
sacha1996 is offline   Reply With Quote
Old 03-31-2011, 12:53 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by aldcorn@live.com View Post
Put this code in function without the id)sender part

Then call self Calculate on each timer click.

Otherwise just have this action fire anytime any of the text or num ers change.

Or do this on the view did load.

It all depends where and when you are getting the values.
Actually, there's no need to remove the (id) sender part.

Just invoke that method any time you want to, like this:


[self Calculate: nil];

Write the button method to check to see if the sender is nil. If it is nil, the method was invoked directly, instead of from a button touch event.

When do you want the method to be invoked? At some regular interval?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 03-31-2011, 12:55 PM   #5 (permalink)
Registered Member
 
sacha1996's Avatar
 
Join Date: Mar 2011
Posts: 415
sacha1996 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Actually, there's no need to remove the (id) sender part.

Just invoke that method any time you want to, like this:


[self Calculate: nil];

Write the button method to check to see if the sender is nil. If it is nil, the method was invoked directly, instead of from a button touch event.

When do you want the method to be invoked? At some regular interval?
Hi Thanks for your time , but where do I have to put the [self Calculate: nil]; , in the ViewDidLoad ?
sacha1996 is offline   Reply With Quote
Old 03-31-2011, 12:59 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sacha1996 View Post
Hi Thanks for your time , but where do I have to put the [self Calculate: nil]; , in the ViewDidLoad ?
I can't answer that because I don't know when you want that code to be invoked.

Figure out what you want to trigger the code, and then you can decide where to put the call.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 03-31-2011, 01:19 PM   #7 (permalink)
Registered Member
 
sacha1996's Avatar
 
Join Date: Mar 2011
Posts: 415
sacha1996 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
I can't answer that because I don't know when you want that code to be invoked.

Figure out what you want to trigger the code, and then you can decide where to put the call.
Thankyou , As I said , I'm new to it.
So I don't really understand what you mean.
Just the code from above is implemented in the .m file of the main window.
When I run the app , I first need to press a button so that it calculates. What I have is a textfield(= divider) a label(= number) and a another label(= result).
I want that after putting a number in the textfield (for example : 8) and the value of the label is 16 , that its result (= 2) is in the label. This is al right , but I first need to press the button for to display the result.
What I want is that after puttung a number in the textfield , it is automaticly calculating (dividing) without pressing any button and giving the result in the label.

Do you need more information ?

Thankyou for your time
sacha1996 is offline   Reply With Quote
Old 03-31-2011, 01:36 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sacha1996 View Post
Thankyou , As I said , I'm new to it.
So I don't really understand what you mean.
Just the code from above is implemented in the .m file of the main window.
When I run the app , I first need to press a button so that it calculates. What I have is a textfield(= divider) a label(= number) and a another label(= result).
I want that after putting a number in the textfield (for example : 8) and the value of the label is 16 , that its result (= 2) is in the label. This is al right , but I first need to press the button for to display the result.
What I want is that after puttung a number in the textfield , it is automaticly calculating (dividing) without pressing any button and giving the result in the label.

Do you need more information ?

Thankyou for your time

Ok, so call

Code:
[self Calculate: nil];
In each of those cases.

Add that call in your view controller's -viewWillAppear method. That will take care of generating the message when the view is first displayed.

To get notified when the user changes the value of your different text fields, you need to do a couple of things:

Add the string <UITextFieldDelegate> to the @interface declaration of your view controller, like this:

Code:
@interface MyViewController: UIViewController < UITextFieldDelegate>
That tells the system that your view controller supports the UITextFieldDelegate protocol. That means that your view controller knows how to respond to certain messages the text field might send it. All the methods in that protocol are optional, so you don't actually have to respond to any of the messages.

Connect the "delegate" outlet on your text fields to "file's owner" in interface builder.

Add this method to your view controller:

- (void)textFieldDidEndEditingUITextField *)textField
{
[self Calculate: nil];
}


The textFieldDidEndEditing: message is one of the messages that a UITextField can send it's delegate. It tells you that the user is finished making a change to the contents of the text field. In our case, we just use it to trigger your calculate method.

That should do it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 03-31-2011, 02:05 PM   #9 (permalink)
Registered Member
 
sacha1996's Avatar
 
Join Date: Mar 2011
Posts: 415
sacha1996 is on a distinguished road
Default

Thanks , that worked !

But I have still one little question.

I've tried to do the same for that code :

Code:
- (IBAction)Total{
	float x = ([billTotal.text floatValue]);
	float c = x+([tipAmount.text floatValue]);
	
	total.text = [[NSString alloc] initWithFormat:@"%2.f", c];
	
}
But It didn't worked.

Here , it will display the sum of a textfield + a label.
How do I have to do here that I don't have to press ?
sacha1996 is offline   Reply With Quote
Old 03-31-2011, 02:18 PM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sacha1996 View Post
Thanks , that worked !

But I have still one little question.

I've tried to do the same for that code :

Code:
- (IBAction)Total{
	float x = ([billTotal.text floatValue]);
	float c = x+([tipAmount.text floatValue]);
	
	total.text = [[NSString alloc] initWithFormat:@"%2.f", c];
	
}
But It didn't worked.

Here , it will display the sum of a textfield + a label.
How do I have to do here that I don't have to press ?

What do you mean "It didn't worked."? What happens? Does the total text field change at all?

The code looks like it should be adding the values. If the result isn't changing, I would make sure that "total" is not nil at run-time. (Set a breakpoint at the line that assigns a value for total.text and examine the value of "total")

If total is changing, but you're getting the wrong answer, check that the billTotal and tipAmount variables are not nil.

The most likely problem is that the outlets to these text fields is not set up in Interface Builder.

By the way, your code has memory leaks.

When you create a string with alloc/init, or initWithFormat, you have to release it when you are done with it. Better to use the stringWithFormat method, that creates an auto-released string:

Code:
	total.text = [NSString stringWithFormat:@"%2.f", c];


Methods with init, new, or copy in their name create objects with a retain count of 1, and you must release them when you are done with them. (You own them)

All other methods return objects that you do not own. You need to send them a retain message if you want to make sure they don't go away in the near future.

In the case of assigning a string to total.text, you want a temporary string that will go away automatically.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 03-31-2011, 02:23 PM   #11 (permalink)
Registered Member
 
sacha1996's Avatar
 
Join Date: Mar 2011
Posts: 415
sacha1996 is on a distinguished road
Default

I have a texfield and 2 labels.
In the texfield I set 100 , the tipAmount label is set to 5.00 so after the press on button the total label wil be 105. So it's changes

By the way , thanks for your tip , I've changed the code
sacha1996 is offline   Reply With Quote
Old 03-31-2011, 07:07 PM   #12 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sacha1996 View Post
I have a texfield and 2 labels.
In the texfield I set 100 , the tipAmount label is set to 5.00 so after the press on button the total label wil be 105. So it's changes

By the way , thanks for your tip , I've changed the code

Sigh...

TO REPEAT: WHAT HAPPENS WHEN YOU CLICK THE BUTTON OR OTHERWISE INVOKE THE METHOD? Does it not change the text? Does it display the wrong answer? And what happens when you set a breakpoint at the beginning of the method and step through the code?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
automatic, code, iphone, sdk, uibutton

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 371
12 members and 359 guests
condor304, dansparrow, dre, ilmman, LezB44, michelle, Objective Zero, samdanielblr, Sami Gh, shagor012, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:37 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0