Tutorial: Simple iOS App: Introduction - Setting Up Xcode, Basic Methods, Basic Setup
Tutorial: Simple iOS App: Introduction - Setting Up Xcode, Basic Methods, Basic Setup
By the iOS team: Eli Evans, Molly Flaccavento, Dana Germano, Lenny Magill,
Cristiane Maia, Katie Pritchard
The final product of this tutorial is a basic multi-view app that allows the
user to take a picture and attach it in an email.
Now to add a Button, find and drag the Rounded Rect Button from
the Objects Library (same list where Label was found) onto the View
Controller screen below the label.
To change the text on the button, double click it and change the text
of the button to the text you want on the button. For this tutorial, we
If you click Run now, the button should appear and though it is
clickable, nothing will happen. We need to link the button to an
action so something happens when it is clicked. To do this, it is
helpful to open the dual-screen editor, which you do by clicking the
tuxedo-like button in the upper right corner. This should open two
windows, so make sure the storyboard is open in one window, and
the ViewController.m open in the other.
Now, hold down the control button and use the mouse to click on the
Cam button and drag into the ViewController.m file. This should
open a dialog that asks about Connection, name, type, etc.
The connection should be Action, and fill in the Name field with
openCam then hit Connect. This should create an empty function
that looks like the following:
- (IBAction)openCam:(id)sender {
}
If you save and Run at this point, and press the Cam button, the
message Cam button pressed should appear in the bottom
console section of Xcode.
Now we will add a new class via File >> New >> File... Select
Objective-C class and name the class
ImagePickerAppViewController. Make sure it is a subclass of
UIViewController, then hit Next, and Create the file in the current
project. This should have created an
ImagePickerAppViewController.m file and an
ImagePickerAppViewController.h file. Now to add this new screen
to the storyboard, drag a View Controller onto the storyboard from
Object Library (bottom right of window). Select the new View
Controller and choose the Identity Inspector tab from the top of the
right sidebar.
#import "ImagePickerAppViewController.h"
We will let the image picker know that it is needed through the use
of a button, so we need to have a way of handling the click of the
button to communicate the event.
This will generate setters and getters for the property we just
declared in the header file. Next, create a new action in the body of
the file (immediately before the @end tag):
-(IBAction) buttonClicked
{
myPicker = [[UIImagePickerController alloc] init];
myPicker.delegate = self;
if([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
myPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}else{
myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentViewController:myPicker animated:YES completion:NULL];
}
This action will check to make sure the application has a camera
and choose the proper source for the image picker once our button
has been clicked. Once the button has been clicked, we will want to
give the user the option to cancel out of this screen, so we need to
make sure a cancel method is implemented. We can use
imagePickerControllerDidCancel.
- (void) imagePickerControllerDidCancel:(UIImagePickerController *) picker {
[[picker parentViewController] dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *) picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[picker parentViewController] dismissViewControllerAnimated:YES
completion:NULL];
}
[emailUI setSubject:@"picture"];
// Attaches the image to the email
[emailUI addAttachmentData:data mimeType:@"image/jpeg" fileName:@"pic"];
[self presentViewController:emailUI animated:YES completion:NULL];
[emailUI setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
}else{
// If the phone cannot send mail
UIAlertView *noEmail = [[UIAlertView alloc] initWithTitle:@"no email"
message:@"there is not e-mail address associated with this phone" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] ;
[noEmail show];
}
}