0

別のクラスから ViewController プロパティにアクセスするにはどうすればよいですか?
(ViewController は XCode によって作成されました)

これは ViewController.h のコードです:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface ViewController : UIViewController{
    AppDelegate *appDelegate; //il delegate disponibile per tutta la classe
    int numPag; //indice array (sulle pagine html da visualizzare)

    NSString *path;
    NSURL *baseURL;

    NSString *file;
    NSString *contentFile;

}


- (IBAction)sottrai:(id)sender;

- (IBAction)aggiungi:(id)sender;



@property (strong, nonatomic) IBOutlet UILabel *valore;
@property (strong, nonatomic) IBOutlet UIWebView *web;

@end

ありがとう!

[編集]

私はXcode 4.3.2を使用していますが、AppDelegate.mのコードは次のようなものを見つけられません:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

ここで、メソッド didFinishLaunchingWithOptions:(into AppDelegate.m) に次のコードを挿入します。

    viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];

[viewController.web loadHTMLString:contentFile baseURL:baseURL];  

ファイル AppDelegate.h に変数 viewController を追加します。

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
    NSArray *pageInItalian;
    NSArray *pageInEnglish;
    UIViewController *viewController; 

}

@property (strong, nonatomic) NSArray *pageInLanguage;
@property (strong, nonatomic) UIWindow *window;



@end

私はそれを正しくしましたか?エラーは次のとおりです: プロパティ 'web' がタイプ 'UIViewController *' のオブジェクトに見つかりません

4

1 に答える 1

2

この投稿はあなたの質問に答えるかもしれないと思います

別のクラスから変数にアクセスするにはどうすればよいですか?

外部クラスから (@property と @synthesize を使用して) 変数にアクセスできるようにする必要があり、外部クラスは ViewController のインスタンスを認識する必要があります。

[編集]

AppDelegate.m には、次のような行が必要です。

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

これは、ViewController がインスタンス化される場所です。そこから、ViewController のすべてのプロパティにアクセスできます。

[編集]

実装の最初に以下を追加する必要があります

@implementation ViewController

@synthesize web;
于 2012-05-17T09:14:24.783 に答える