2013-02-09 49 views
2

我剛剛開始在objective-c中編程,並且「使用未聲明的標識符'uneImage';您的意思是'_uneImage'?」。很多帖子都在談論這個問題,但我還沒有找到解決方案。iphone xCode使用未聲明的標識符

.H:



    #import 


    @interface ViewController : UIViewController 
    { 
     UIImagePickerController *picker; 
    } 

    @property (weak, nonatomic) IBOutlet UIImageView *uneImage; 


    - (IBAction)album:(id)sender; 
    @end 

.M



    #import "ViewController.h" 

    @interface ViewController() 

    @end 

    @implementation ViewController 

    - (IBAction)album:(id)sender 
    { 
     picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [self presentViewController:picker animated:YES completion:nil]; 
    } 

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    { 
     uneImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     [picker dismissViewControllerAnimated:YES completion:nil]; 
    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

    @end 

回答

5

當你定義一個屬性xyz,默認情況下它的名字轉化爲_xyz命名其支持變量。您可以用@synthesize name;甚至@synthesize name = someOtherName;覆蓋它,但不再需要使用@synthesize

屬性本身在外部是可見的,但它不會像變量一樣在範圍中引入非限定名稱。換句話說,如果沒有在self前加前綴,你就不能使用該屬性,但是你可以使用它的後備變量。

爲了使長話短說,與

_uneImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

self.uneImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

,使其工作更換

uneImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

0

您正在訪問的屬性,所以你需要把 「自我」,在變量前面:

self.uneImage.image = .... 
2

您沒有名爲uneImage的實例變量。您有一個具有該名稱的屬性和一個名爲_uneImage的自動合成實例變量。

因此改變:

uneImage.image = ... 

要麼:

self.uneImage.image = ... // use the property 

或:

_uneImage.image = ... // use the generated instance variable