Showing a popup message to a user in iOS is not very difficult. The code below shows a popup message with 1 button. If the user presses the button the popup message disappears. No code is executed when the uses pushes the button. (For an example with delegates look at the post: “Popup message in iOS with delegate”)
1 2 3 4 5 6 7
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Alert title" message:@"Alert body" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [message show]; [message release];
Some more explanation:
- Line 1: a UIAlertview is allocated. We alloc-ed the UIAlertView, therefore we are responsible for releasing it. (See the post on memory management for more info)
- Line 1 and 2: The title and body of the popup message is set here.
- Line 3: There is no “delegate” in this example, just 1 button to press therefore no response to this 1 button.
- Line 4: The title of the button that makes the popup message disappear is set here
- Line 5: There are no other buttons.
- Line 6: Show the popup message!
- Line 7: remember line 1, we are responsible for releasing the UIAlertView, that’s just what we do here.
With this code example the popup should look like: