我刚学习以下初学教程书,我有这段代码:
- (void) presentImagePickerUsingCamera:(BOOL)useCamera
{
UIImagePickerController *cameraUI = [UIImagePickerController new];
cameraUI.sourceType = (useCamera? UIImagePickerControllerSourceTypeCamera:
UIImagePickerControllerSourceTypePhotoLibrary);
cameraUI.mediaTypes = @[(NSString*)kUTTypeImage];
cameraUI.delegate = self;
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[self presentingViewController:cameraUI];
}
这是界面:
#import <UIKit/UIKit.h>
#import "MyWhatsit.h"
@interface MSDetailViewController : UIViewController <UISplitViewControllerDelegate,
UIActionSheetDelegate,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
@property (strong,nonatomic) MyWhatsit* detailItem;
@property (weak,nonatomic) IBOutlet UITextField *nameField;
@property (weak,nonatomic) IBOutlet UITextField *locationField;
@property (weak,nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)changeDetail:(id)sender;
- (IBAction)chooseImage:(id)sender;
- (void) presentImagePickerUsingCamera:(BOOL)useCamera;
@end
给我错误:
code/MSDetailViewController.m:89:11: No visible @interface for 'MSDetailViewController' declares the selector 'presentingViewController:'
我确实试图找到像here
这样的答案但没有任何帮助,我在这里做错了什么?
答案 0 :(得分:1)
那是因为这一行:
[self presentingViewController:cameraUI];
没有这样的方法。 您正在尝试设置显示视图控制器的控制器。它没有setter,因为它是readonly(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/presentingViewController)
如果你想设置它的值,@ property会给你这个(在变量名称之前设置)[self setSomeInstanceVariable: @"Something"];
作为setter。
所以,例如:
@property(nonatomic, strong)NSString *foo;
给你
[self foo]; //getter
[self setFoo:@"bar"]; // setter
如果要呈现视图控制器,则应使用以下方法:
presentViewControllerAnimated:completion
答案 1 :(得分:0)
提出方法是:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
所以在你的情况下:
[self presentViewController:cameraUI animated:YES completion:nil];