There are no active ads.

Advertisement

Introduction to iOS Development: Buttons, Actions, and Alerts

How do you develop apps for iOS?  It’s a common question that many computer enthusiasts ask, and one we here at TechnoBuffalo hope to answer with this series of posts.

Learning how to develop software can be one of the most intimidating prospects for the average computer user, and with the growing saturation of applications in mobile marketplaces, it is becoming increasingly difficult to get your work noticed. That’s what this series is for, helping you learn iOS development from a conceptual perspective. No prior knowledge of computer programming will be necessary. Over the coming weeks, we’ll examine the iPhone’s ability to deliver immersive, intuitive content, a unique opportunity for both developers and consumers.

Today, we’ll be diving into the worlds of buttons and simple boxes that allow for user interaction. We’ll be programming an application that has four buttons that are connected to a local notification system.

1.  Open Xcode 4, head to File > New Project and select View-Based Application. This will be the template that we are using in today’s tutorial and will automatically set you up with a view controller so you can manage the view and a nib file that contains it.

2.  Entitle the project Button and save it in a safe place. Take a second to examine what the template provides us with – an app delegate, interface file, and view controller.

3.  Head on over to ButtonViewController.h, the initialization file of the application. We will be creating the variables that will later be clarified in Interface Builder. To do so, make sure that the file’s code appears like that which I have written below.

4.  Next, we have to work with ButtonViewController.xib, the interface file that determines what users see. Feel free to change the background color to whatever floats your boat. You will, however, need to open up the object library, which is accessible through the Utilities drop-down of the View menu. You will need to drag out a label, changing the text to say, “What is your favorite mobile operating system?” Next, pull four rounded rectangular buttons onto the interface, naming them to appropriately represent the code.

5.  Next, we have to connect the code we wrote in the initialization file of the view controller to the interface file. Hold down the Control button on your keyboard and drag from File’s Owner to each button, releasing your mouse on the appropriate option.

6.  Unfortunately, there is no way to assign actions in bulk, so we have to go through each button individually. Go to each button’s attributes and then go into the connections pane. Hold Control and drag from TouchUpInside to File’s Owner to connect the code. Repeat for each button.

7.  We need to poke around inside the view controller’s implementation file, ButtonViewController.m. We need to synthesize the elements that we created in the header file, so add the bit of code below.

8.  The next excerpt of code has two strings inside of it. One dictates the response to any user’s interaction with the buttons and another gives users the ability to see alerts upon pressing the buttons. Copy the code as written below.

9.  Now that we have finished that, there is nothing left to do but deallocate the button elements that we synthesized earlier and view your glorious creation in the iOS Simulator, granted that there are no compilation errors.

Questions?

If you have any questions about the topics discussed in this lesson, feel free to voice them in the comments below. We will do our best to ensure that you have a relatively painless experience developing for iOS. Stay tuned for the next installment of this series, which is released weekly.

To read previous installments from this series, check the links below.


Jack McGrath

Rooted in his childhood obsession with dismantling and reassembling gizmos and gadgets around the house, Jack McGrath's knowledge of programming,...

Advertisement