预期标识符或'('编译Objective-C时

时间:2014-01-12 18:42:35

标签: objective-c

所以我正在尝试制作这款相机应用程序,但每当我尝试运行它时,它会显示“预期标识符或'('。我得到其中两个错误。其中一个正确 - (IBAction)takePhoto:(UIButton *)sender;以及- (IBAction)selectPhoto:(UIButton *)sender;下的#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 感谢

{{1}}

2 个答案:

答案 0 :(得分:5)

@interface(通常是.h文件)仅包含类接口。它不应该包含类实现(方法内容)。这是在.m文件中(具体来说,是@ implementation)。所以你需要把它移到那里。

答案 1 :(得分:0)

如果您需要从课堂外调用您的方法,您必须将他们的签名放在.h文件中,如下所示:

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

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

@end 

然后在.m文件中你应该有这样的东西(我在私人界面中离开该属性,所以你看它应该是什么样子)

@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

你应该注意.m文件中的内容:

  • 方法名称后面没有分号
  • 私有接口的声明(您忘记了@end并使用了.h文件@interface声明)
    • 当你使用属性时也不要使用驼峰盒,因为你会感到困惑;使用较低的驼峰案例:
  

@property(强,非原子)IBOutlet UIImageView * ImageView;

应该是

  

@property(强,非原子)IBOutlet UIImageView * imageView;

不要忘记使用XCode自动完成功能来帮助您提高效率。