Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 UI Alert View iPhone/iPad, iOS Development Tutorial.

Similar presentations


Presentation on theme: "1 UI Alert View iPhone/iPad, iOS Development Tutorial."— Presentation transcript:

1 1 UI Alert View iPhone/iPad, iOS Development Tutorial

2 2 What is the UIAlertView? The UIAlertView class allows us to present a simple interface to alert the user that something has happened. It also allows us to respond in different ways, depending on which button in the alert view is touched. If we want more than the default cancel button on an alert, we must adopt the UIAlertViewDelegate protocol.

3 3 Open a new “Single View” app Start Xcode, choose “Create a new Xcode project,” select the Single View Application template, and click Next. Select “Use Auto Reference Counting” Don’t select the storyboard. Don’t really need it for this application.

4 4 In the.xib or Storyboard Add a button to the user interface that will bring up the alert view when pressed. Open the library and drag a UIButton to the view. Change the title of the button to “Change Color…” and (if desired) change the view’s background color as well. When you are done, the interface should look something like this:

5 5 Completed View

6 6 Click on “Editor” (tuxedo icon) Right mouse click and select “TouchDown” action. CONTROL + Drag the action to: ViewController.h Label the button “btnTouch” The code looks like this: - (IBAction)btnTouched:(UIButton *)sender;

7 7 ViewController.m Open ViewController.m and add the implementation of the btnTouched method: - (IBAction)btnTouched:(UIButton *)sender { UIAlertView *colorAlert = [[UIAlertView alloc] initWithTitle:@"Change Color…" message:@"Choose a Color" delegate:self cancelButtonTitle:@"None" otherButtonTitles:@"Red", @"Yellow", @"Blue", nil]; [colorAlert show]; }

8 8 ViewController.m The title will appear at the top of the alert view, with the message just below it. Next the buttons will be displayed, with the three “otherButtonTitles” buttons displayed first, followed by the “cancelButtonTitle.” The delegate parameter defines the object that will handle the button touches, in this case, “self,” meaning ViewController.

9 9 Wire this method up to the Change Color… button in interface builder

10 10 Test the App If we run the application now, we’ll see the alert view when we click the button, but nothing will happen after clicking an alert button aside from the alert being dismissed. In order to handle touches on other alert buttons, we must implement one of the delegate methods of UIAlertViewDelegate: (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex. The first parameter of this method will be the alertView on which a button is touched, and the second will be the index of the touched button.

11 11 Modify - ViewController.m - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 1: //red self.view.backgroundColor = [UIColor redColor]; break; case 2: //yellow self.view.backgroundColor = [UIColor yellowColor]; break; case 3: //blue self.view.backgroundColor = [UIColor blueColor]; break; default: break; } }

12 12 Button Behavior The buttonIndex will be 0 if the cancel button is pressed, and incremental values starting at 1 for the otherButtonTitles array. In this case, we’re just changing the color of the ViewController’s view.

13 13 Program Output

14 14 Summary The UIAlertView is a “modal” view (meaning that it blocks touch events from bubbling up to it’s parent view), It doesn’t hang the UIThread, which makes it a very good control for simple choices.


Download ppt "1 UI Alert View iPhone/iPad, iOS Development Tutorial."

Similar presentations


Ads by Google