Right now I'm working through Udacity's iOS Developer Nanodegree program. We're using Swift 2.0 and so far I'm really liking the program. I am currently working through the second course (UI Kit Fundamentals I) lesson 3 challenge app. This challenge app is working with UITextField Delegates. The challenge app consists of three textfields. The first textfield is a zip code field with a character limit of 5 and a numeric keyboard. The second field is a money field that contains a dollar sign at the left that always shows 2 places after the decimal. The text is filled in from the right and is appended onto the amount. So for example if you already entered $1.05 and then enter 3 the new value is $10.53. The final textfield is paired to a switch control. When the switch is on the textfield is locked and cannot be edited, when the switch is off then the textfield can be edited. I started working on the first textfield so that will be the focus of this post. The first thing I did was change the keyboard to numeric only.
To change the on screen keyboard first open up your storyboard and selected the text field. Next expand your Utilities Panel (if hidden) and go to the Attributes Inspector. From there you can change the Keyboard Type (left screen shot). Now there are a number of other options for keyboard type, as you can see in the right screenshot, but for this project I chose Number Pad.
In .Net all string objects are also character arrays. This was the case until Swift 2.0, read Apple String Reference before for more information about why that was changed. The String object in Swift 2.0 now exposes a characters property that is the characters array. When you're working with the UITextFieldDelegate the function includes the textfield object, the range and the incoming string. Since I wanted to limit the number of characters to 5 I first needed to get the number of characters in the string. In the first screenshot the text (the local variable text) property of the textfield would be "1234" with a character count of 4. Now if I enter 5 the string parameter will have a value of 5. So I can simple add the existing length (4) and the length of the new string (1) and check if the new length will be less than or equal to my limit of 5. Which it is in this case so the method returns true and the new string is added to the existing string. If I already had 5 characters then the method would return false and not allow the new string to be appended.
Now this is a very simple use case. I didn't even touch the range object and I didn't do any kind of string manipulation. I just limited the number of characters. One thing you will notice if you were to run this code in the simulator is that even though the on screen keyboard is showing only numbers you can use your physical keyboard to enter non numeric characters. Since the on screen keyboard is limited to numbers I decided not to worry about it. I hope this was helpful to you. Happy Coding.
Here's a scenario that I ran into frequently when I was working in web forms. Let's say that you have form with some controls on it. Now let's say that you need to reset that form based on some user action or an event. Let's assume this is the markup of your aspx page. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> Now inside your reset method you could go over each textbox one at a time and reset the text, like this, but I don't like that approach. protected void btnReset_OnClick(object sender, EventArgs e) First I think it takes too much repetitive typing and second you have to modify the method if you add another textbox. Now if you look closely there is another problem with this approach. I missed that zip code textbox. It is pretty easy to miss a single textbox. Now imagine if your page also had other controls like checkboxes or radio button groups. You can see that the reset method would quickly grow to be a large method. Let's see if we can find a better way. What I did was to create a static helper class and inside that class create a static method that will return all of the children controls of a given type. Here is the code for the helper class. public static List<T> GetControls<T>(Control root) Now you can change your reset method to simply call the helper method. In addition you don't have to change your reset method every time you add a new textbox. Let's see the GetControls<T> method in action. protected void btnReset_OnClick(object sender, EventArgs e) Much better. You don't have to worry about forgetting a control or changing your reset every time you change your aspx page. In this example I'm pointing the GetControls method at the root Page. You could just as easily point it at another control, like a table or a div, if you only wanted to clear a subset of the controls. Hope that is helpful for you and as always, Happy Coding.
To display the page numbers in Xcode (currently I'm using Xcode 7.0.1) simply go to Xcode > Preferences > Text Editing. Then check the box for Line numbers. It is that simple.If you want to be able to collapse a function, so you only see the signature, then check the box for Code folding ribbon. Personally, I don't know why this option isn't checked by default. The option to highlight a section of code when you hover over that area in the left side bar (the bar that you click on to collapse or expand a function) is Focus code blocks on hover. Personally, I find it really annoying when the code highlights so I have that option unchecked. Happy Coding.
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|