-3

So I am trying to make this camera app but every time I try an run it, it says " Expected Identifier or '('. I get two of these error's. One of them right under - (IBAction)takePhoto:(UIButton *)sender; and one under - (IBAction)selectPhoto:(UIButton *)sender; Thanks

#import <UIKit/UIKit.h>

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *ImageView;


- (IBAction)takePhoto:(UIButton *)sender;
{
 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType =UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)selectPhoto:(UIButton *)sender;
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}
@end
marko
  • 9,029
  • 4
  • 30
  • 46
  • 6
    I think you need to step back and find a good basic Objective-C tutorial. You need to learn what goes in a .h file and what goes in the .m file. – rmaddy Jan 12 '14 at 18:56
  • What on this good earth are you doing? Why is your implementation in what I can only assume is your `.h` file? Have you done any tutorials at all? – Popeye Jan 12 '14 at 19:04
  • Basically you've got two declarations, and directly under them sets of statements enclosed in `{}` and not attached to anything. The declarations ended with the `;` characters. And, as others have indicated, unless you really know what you're doing the declarations should stay in the .h and the method bodies in the .m (with suitable method headers -- essentially the declarations sans `;` -- preceding the bodies, and with the appropriate @implementation/@end statements surrounding the whole mess). – Hot Licks Jan 12 '14 at 20:40

2 Answers2

5

The @interface (generally the .h file) holds only the class interface. It shouldn't contain the class implementation (the method contents). That goes in the .m file (specifically, the @ implementation). So you need to move it there.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • The `.h` and `.m` can both have an `@interface`. This issue isn't `.h` versus `.m` so much as it is `@interface` versus `@implementation`. – nhgrif Jan 12 '14 at 18:46
  • @nhgrif yes you are right. But as per code its .h file. Wain is correct. – Tirth Jan 12 '14 at 18:47
  • @nhgrif In this case the `@interface` is not a class extension. – zaph Jan 12 '14 at 18:47
  • 1
    @Reformer My comment was mostly to point out that the questioner needed to do more than simply copy & paste over from `.h` to `.m`. He'd see the same errors if this code were in the `@interface` of the `.m` file. It has to be in the `@implementation`. – nhgrif Jan 12 '14 at 18:59
0

If you need to call your methods from outside your class you have to place their signatures in the .h file, like so :

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

- (IBAction)takePhoto:(UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;

@end 

and then in your .m file you should have something like this (I am leaving the property in the private interface so you see how it should look like)

@interface APPViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *ImageView;

@end

- (IBAction)takePhoto:(UIButton *)sender
{
 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType =UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)selectPhoto:(UIButton *)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}
@end

The things that you should notice in the .m file:

  • there are no semicolons after the names of the methods
  • the declaration of a private interface (you forgot the @end and used the .h file @interface declaration)
    • also when you are using properties never use camel case because you will get confused; use lower camel case :

@property (strong, nonatomic) IBOutlet UIImageView *ImageView;

should be

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

Do not forget to use XCode autocompletion to help you out and be more productive.

Tibor Udvari
  • 2,932
  • 3
  • 23
  • 39
  • Why would you declare an outlet property in a class extension? There's no point to a private outlet. – rmaddy Jan 12 '14 at 19:57
  • [Some among us seem to believe outlets and actions should be private](http://stackoverflow.com/questions/17852287/should-i-put-ibactions-in-the-header-file-or-not/17852479#comment30170732_17852479), @rmaddy. They are of course wrong wrong wrong. :) – jscs Jan 12 '14 at 20:04
  • Well my reasoning is that XCode does not give me autocompletion for the properties if I'm referencing the view controller from somewhere else. I didn't inspect his code carefully, but myself, as a general rule I do not put properties in the .h files unless I need reference those in other places. – Tibor Udvari Jan 12 '14 at 20:22