How To Create Simple Game With A Custom Picker In iPhone?

We are going to have a little fun with the fifth content view. We’re going to see how to add image data to a picker, write a little game that uses a picker with five components.

Step 1: Create a new project from Xcode and select view-base application template.

Step 2: Adding the following code to the controller Header file.




Step 3: Double click .xib file to open the file in Interface Builder. Add a label, a picker, and a button. Give the button a title of spin. Next select label , and use the Fonts palette (Alt+T)to make the label’s text nice and big. After getting the text the way you want it, delete the word Label from it , since we don’t want any text displayed until the first time the user wins. After that, make all the connections to outlets and actions. You need to connect the file’s owner’s picker outlet to the picker view, the file’s owner’s winLabel outlet to the label,and the button’s touch up inside event to the spin action. There one additional thing that you need to do. Select the picker, and bring up the attributes inspector. You need to uncheck and checkbox labeled User Interacface Enabled . Now save it, go back to Xcode.

Step 4: We need to add the images that we’ll be using in our game. We’ve included a set of six image files(seven.png, bar.png, crown.png, cherry.png, lemon.png, and apple.png) in your resources folder.

Step 5: We need to add following code to our controller file. First method is spin method fires when the user touches the Spin button . We first declare a few variables that will help us keep track of whether the user has won. Second method is viewDidLoad, first thing we do is load six different images. We do using this using a convenince method on the UIImage class class called imageNamed:



 Step 6: Now compile your application and you can play your application on Simulator. ( see  figure 1)

Figure 1: Our five component picker.

You can downloaded SourceCode from here CustomPicker 2