Development of the first iOS applet-click the button text color
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="color: #000000; font-size: 18px;"> <strong>1, title: Achieve click button switch text corresponding color</strong></span></p></p><p><p></p></p><p><p><span style="font-size: 18px;"><strong>2, the analysis of the general development steps are</strong></span></p></p>• Add the required UI elements: 3 buttons (UIButton), a text label (UILabel) • Monitor click events for 3 Buttons • Change the text color of the label<span style="font-size: 18px;"><span style="font-size: 18px;"><strong>3. Concrete Implementation Steps</strong></span></span><span style="font-size: 15px;"><span style="font-size: 15px;">3.1 In the storyboard file, drag and drop the corresponding control, the layout of the above display page effect</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">3.2 Then use the "circle" button on the right side of Xcode to split the code area into "main.storyboard" and "viewcontroller.m" files, Two can hold down the control key, do not let go, drag the above element to " VIEWCONTROLLER.M "file, after dragging remember to name the appropriate, let people at a glance. </span></span><span style="font-size: 15px;"><span style="font-size: 15px;">1. (circle button, can be divided into code area of two files)</span></span><span style="font-size: 15px;"><span style="font-size: 15px;"><span style="font-size: 15px;">2. When you hold down the control key, don't let go, drag the "label control" in the storyboard view to the "viewcontroller" file, the dialog box pops up and</span></span></span><span style="font-size: 15px;"><span style="font-size: 15px;"><span style="font-size: 15px;"></span></span></span><span style="font-size: 15px;"><span style="font-size: 15px;"><span style="font-size: 15px;">you choose outlet, then you need to fill in the Name property of the control, and finally click the "Connect" button, which will be generated in the "viewcontroller.m" file</span></span></span><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">@property (weak, nonatomic) iboutlet UILabel *label;</pre></pre><p><p>This will allow you to get the Label object in the viewcontroller.m, and the next step is to change the color of the label object just by clicking on the corresponding Button.</p></p><p><p>3. similarly, You can drag and drop the above three buttons to viewcontroller.m, however, Note that when you need to set the Select action Drop-down option, because you need to be bound button events,</p></p><p><p>Name: is the type of the Listener button event, type: if you want to select the actual types of the control, for example, I listen button click event, You can choose the UIButton type, event: control event, Touch up inside button click to raise the event monitoring, The code will then be generated as on the right:</p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">-(ibaction) changecolor: (UIButton *) sender;</pre></pre><p><p>4. finally, We can write the code to handle the button click event, One thing you can do before this is to set the Tag property of three buttons to the property bar on the right of the storyboard to modify their respective 0,1,2, so the advantage is that, We can let the 3 buttons are dragged to what method, associated with the same method changecolor:, so that you can achieve a certain packaging ideas, centralized processing button click Color Logic. The premise of this is that we can tell which button in the three button is clicked by setting the property tag of the Button.</p></p><pre class="brush:objc;collapse:true;;gutter:true;"><pre class="brush:objc;collapse:true;;gutter:true;">-(ibaction) changecolor: (UIButton *) sender { self.label.textAlignment = uitextalignmentcenter; Switch (sender.tag) {case 0: //red self.label.textColor = [uicolor redcolor]; break; Case 1: //green self.label.textColor = [uicolor greencolor]; break; Case 2: //blue self.label.textColor = [uicolor bluecolor]; break; Default: Break ; }}</pre></pre><p><p>5. This completes the discoloration of the applet, command + R can be Run. Click on the "green" button, the effect is as Follows:</p></p><p><p></p></p><p><p>Development of the first iOS applet-click the button text color</p></p></span>
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