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.
- (IBAction)myAction1:(id)sender {
- int kakaku = [[motone text] intValue];
- switch([waribiki selectedSegmentIndex]){
- case 0:
- kakakukakaku = kakaku * (1.f - 0.2f);
- break;
- case 1:
- kakakukakaku = kakaku * (1.f - 0.3f);
- break;
- case 2:
- kakakukakaku = kakaku * (1.f - 0.4f);
- break;
- case 3:
- kakakukakaku = kakaku * (1.f - 0.5f);
- break;
- default:
- break;
- }
- [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:
- 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%.
- [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:
- 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:
- switch([waribiki selectedSegmentIndex]){
- case 0:
- kakakukakaku = kakaku * (1.f - 0.2f);
- break;
- case 1:
- kakakukakaku = kakaku * (1.f - 0.3f);
- break;
- case 2:
- kakakukakaku = kakaku * (1.f - 0.4f);
- break;
- case 3:
- kakakukakaku = kakaku * (1.f - 0.5f);
- break;
- default:
- break;
- }
Next, we use the Outlet "kekka" of the Label control to calculate the discounted price "kakaku 」.
- [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.