IPhone Development Action and Objective-C learning basics

Source: Internet
Author: User

IPhoneDevelopment Action andObjective-CThe basics of learning are the content to be introduced in this article. Let's take a look at the content. Let's analyze the previous contactObjective-CCode.

 
 
  1. (IBAction)myAction1:(id)sender {  
  2. int kakaku = [[motone text] intValue];  
  3. switch([waribiki selectedSegmentIndex]){  
  4. case 0:  
  5. kakakukakaku = kakaku * (1.f - 0.2f);  
  6. break;  
  7. case 1:  
  8. kakakukakaku = kakaku * (1.f - 0.3f);  
  9. break;  
  10. case 2:  
  11. kakakukakaku = kakaku * (1.f - 0.4f);  
  12. break;  
  13. case 3:  
  14. kakakukakaku = kakaku * (1.f - 0.5f);  
  15. break;  
  16. default:  
  17. break;  
  18. }  
  19. [kekka setText:[NSString stringWithFormat:@"%d", kakaku]];  

The code here is the Action when Segmented Control is used to change the discount rate. Let's start with the initial code.

Retrieve Value

Obtain the input value in the Text Field control.

First, we take out the original price of the product you entered. As mentioned above, the control values are obtained through Outlet. The Outlet of Text Field is "motone", and its code is as follows:

 
 
  1. int kakaku = [[motone text] intValue]; 

The right side of the equal sign is the way to get the value through Outlet. Assign the value to the variable kakaku.

Get value in Segmented Control

Next, we get the set value from the Outlet "waribiki" of the Segmented Control. Here, we retrieve the "button number pressed" in the Segmented Control 」. Different buttons correspond to different discount rates. For example, the leftmost button is 20%.

 
 
  1. [waribiki selectedSegmentIndex] 

The leftmost index is 0, and the rightmost index is 3.

Objective-C syntax

Friends who have been familiar with C programming may soon be able to adaptObjective-CAfter all, it is a language evolved from the C language (a large number of them inherit the idea of the Smalltalk language ). Let's take the two controls above as an example to learn their basic syntax.

Objective-C function call

Function calling uses [] to enclose the implementation code. The function call object is called a receiver, which can be understood as an object instance ).

The Outlet control configured with Interface Builder is the receiver. The receiver and function (called message in Objective-C) are separated by spaces. The whole covered by [] is the callback function call.

You can also use such a function call form as nested. For example, when you obtain the input value from the Text Field control, you first execute "text" in "motone" to retrieve the specific Text object, then execute the "intValue" message as the receiver. In this way, the final result is an integer value.

Calculation and representation results

Next we will calculate the discount price. The discount rate obtained by the original price "kakaku" and Segmented Control must be calculated based on the discounted price. For example, when the leftmost 20% is selected, the calculation formula is as follows:

 
 
  1. kakakukakaku = kakaku * (1.f - 0.2f); 

"F" after the decimal number indicates "float 」. If you use an integer as a decimal number, use "1.f" instead of" 1f 」.

Different discount rates require different calculation methods. Here the switch syntax is used to differentiate various situations:

 
 
  1. switch([waribiki selectedSegmentIndex]){  
  2. case 0:  
  3. kakakukakaku = kakaku * (1.f - 0.2f);  
  4. break;  
  5. case 1:  
  6. kakakukakaku = kakaku * (1.f - 0.3f);  
  7. break;  
  8. case 2:  
  9. kakakukakaku = kakaku * (1.f - 0.4f);  
  10. break;  
  11. case 3:  
  12. kakakukakaku = kakaku * (1.f - 0.5f);  
  13. break;  
  14. default:  
  15. break;  

Next, we use the Outlet "kekka" of the Label control to calculate the discounted price "kakaku 」.

 
 
  1. [kekka setText:[NSString stringWithFormat:@"%d", kakaku]]; 

Different from the function call above, parameters are added here.

Objective-CFunction call

The parameter of the "setText" function is a string, and the "kakaku" variable is an integer. Therefore, we first convert the integer to the string type "NSString 」. Here we use the string format function "stringWithFormat" of "NSString 」. The Outlet "kekka" of the Label control is expressed by the specific value.

In the future, we will learn more deeply.Objective-CVarious syntaxes and ideas.

Summary:IPhoneDevelopment Action andObjective-CI hope this article will help you. For more information, see edit recommendations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.