2013-10-04 40 views
0

我是Xcode的新手,並試圖找出更多關於xcode編碼的知識。@class和#import在另一個視圖控制器上,它是什麼意思?

所以,我想更多地瞭解模型(模型操作)客觀C.

我在PhotoViewController.h @class聲明和.M感到困惑文件

,你可能會看到如下,我已經導入Photo.h上appdelegate.m也PhotoViewController.m文件

目標從我的教程是PhotoViewController.m文件可以識別self.photo.filename

但是,爲何要添加@ Class和@pro PhotoViewController.h文件中的perty?

isnt #import命令已經夠用了嗎? @Class是什麼意思,爲什麼它也包含@property呢?

注:我試圖把註釋(//)上@class,但xcode的告訴我沒有發現照片屬性,當我把添加的註釋(//)財產

PhotoViewController.m文件也混淆了無法識別的照片屬性。

我不太明白,在同一時間使用@class和#進口的,再加上宣佈@property照片

這裏是Photo.m

#import "Photo.h" 

@implementation Photo 

-(id)init 
{ 
    self = [super init]; 
    return self; 
} 

@end 

照片。^h

#import <Foundation/Foundation.h> 

@interface Photo : NSObject 
@property (weak, atomic) NSString *title; 
@property (strong, nonatomic) NSString *detail; 
@property (strong, nonatomic) NSString *filename; 
@property (strong, nonatomic) NSString *thumbnail; 
@end 

Appdelegate.m

#import "AppDelegate.h" 
#import "FeedTableViewController.h" 
#import "ProfileViewController.h" 
#import "FavoritesViewController.h" 
#import "Photo.h" 


@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    Photo *photo= [[Photo alloc]init]; 
    photo.title = @"Demo Photo"; 
    photo.detail = @"This is a demo photo"; 
    photo.filename = @"demo.png"; 
    photo.thumbnail = @"demo-thumb.png"; 


    return YES; 
} 
@end 

PhotoViewController.h文件

#import <UIKit/UIKit.h> 
@class Photo; 
@interface PhotoViewController : UIViewController 

@property (weak, nonatomic) NSString *imageFileName; 
@property (weak, nonatomic) NSString *imageTitle; 
@property (strong, nonatomic) Photo *photo; 
@end 

PhotoViewController.m文件

#import "PhotoViewController.h" 
#import "UIImageView+AFNetworking.h" 
#import "Photo.h" 

@implementation PhotoViewController 

-(void)viewDidLoad { 
// self.title = self.imageTitle; 
    UIImageView *imageView = [[UIImageView alloc] init]; 

    [imageView setImageWithURL:[UIImage imageNamed:self.photo.filename]]; 
    imageView.frame = CGRectMake(10,10,300,300); 

    [self.view addSubview:imageView]; 

    UILabel *imageTitleLabel = [[UILabel alloc] init]; 
    imageTitleLabel.text = self.imageTitle; 
    imageTitleLabel.frame = CGRectMake(11,320,300,40); 

    [self.view addSubview:imageTitleLabel]; 
} 

@end 
+0

[@class vs. #import](http:// stackoverflow。com/questions/322597/class-vs-import) – Amar

+0

並且在這裏查看更多信息:http://stackoverflow.com/questions/7378510/headers-import-versus-class –

回答

1

@class Photo定義Photo階層的存在,以PhotoViewController.h讓你申報的照片財產。 照片屬性在PhotoViewController.m以後用來訪問實例變量的照片這樣self.photo[self photo]

你可以在你的PhotoViewController.h已經把#import "Photo.h"但它是清潔這樣:)

0

@property

它用於替換getter方法,每當你想獲得值時你都必須聲明該變量爲屬性,這樣就不必分別寫getter方法,

你sh還應該在Photo.m中實現@synthesize,@synthesize將作爲setter方法工作。

相關問題