-1

これは私を夢中にさせています。私はクラスを持っています:

@interface qanda : NSObject

@property (nonatomic, copy) NSString *quote;
@property (nonatomic, copy) NSString *author;

@end

そして、はい、私は他のファイルでそれらを合成しました。

そして、Viewdidloadファイルでいくつかのオブジェクトを宣言しました。

- (void)viewDidLoad
{
    qanda *qanda1 = [[qanda alloc] init];
    qanda1.quote = @"All our dreams can come true – if we have the courage to pursue          them. ";
    qanda1.author = @"Walt Disney";
}

ViewDidLoadファイルの短い抜粋があります。

ただし、このオブジェクトの文字列にアクセスしようとすると、エラーが発生し、理由がわかりません。

self.quote.text = qanda.quote;`

(ちなみに引用はアウトレットです)私が得るエラーは次のとおりです:「宣言されていない識別子'qanda1'の使用;あなたは'qanda'を意味しましたか?

4

1 に答える 1

1

From what I see here, your qanda *qanda1 is limited to the viewDidLoad method. Once that method returns, qanda1 no longer exists.

In your view controller's header file, declare a property for qanda1.

@class Qanda;    
@interface MyViewController : UIViewController
    .
    .
    .
@property Qanda *qanda1;
@end

In the implementation file "MyViewController.m):

#import "Qanda.h"

@implementation MyViewController
.
.
.
-(void)viewDidLoad {
    Qanda *qanda1 = [[Qanda alloc] init];
    qanda1.quote = @"All our dreams can come true – if we have the courage to pursue          them. ";
    qanda1.author = @"Walt Disney";
}

.
.
.
@end

This way, you have access to qanda1 throughout the lifetime of MyViewController. You can now perform your self.quote.text = qanda1.quote; anytime after viewDidLoad has been called.

I would recommend on reading up on variable scope (here is a good starting point on SO) so that you get complete understanding of what is happening here.

UPDATE

As mentioned in comments to your question, following some basic naming conventions might help to distinguish between instance variables and class names. (for Objective C, but most languages follow the same, if not similar patterns).

Following the usual conventions, your "qanda" class would look like this:

@interface Qanda : NSObject

@property (nonatomic, copy) NSString *quote;
@property (nonatomic, copy) NSString *author;

@end
于 2013-02-17T19:09:47.073 に答える