访问另一个类的属性 [英] access to property of another class

查看:30
本文介绍了访问另一个类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从另一个类访问 ViewController 属性?
(ViewController 是由 XCode 创建的)

How can I access to ViewController property from another class ?
(the ViewController was created by XCode)

这是ViewController.h的代码:

this is code of 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 的代码没有找到类似的东西:

I use Xcode 4.3.2 and the code of AppDelegate.m not find something like:

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

现在,我在方法 didFinishLaunchingWithOptions 中加入:(进入 AppDelegate.m)这段代码:

Now, I put in the method didFinishLaunchingWithOptions:(into AppDelegate.m) this code:

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

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

在文件 AppDelegate.h 中添加变量 viewController:

adding in the file AppDelegate.h the variable 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

我做对了吗?错误是:在UIViewController *"类型的对象上找不到属性web"

I did it correctly? the error is: Property 'web' not found on object of type 'UIViewController *'

推荐答案

我想这篇文章可能会回答你的问题

I think this post might answer your question

如何从另一个类访问变量?

您需要使您的变量可以从外部类(使用@property 和@synthesize)访问,然后您的外部类需要知道您的 ViewController 实例

You need to make your variable accessible from foreign classes (with @property and @synthesize) and then your foreign class need to know your instance of ViewController

在你的 AppDelegate.m 中,必须有这样一行:

In your AppDelegate.m, there must be a line like this one :

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

这是您的 ViewController 实例化的地方.从那里,您可以访问 ViewController 的每个属性.

This is where your ViewController is instantiate. From there, you can access every property of your ViewController.

您需要在实现的开头添加以下内容

You need to add the following at the beginning of your implementation

@implementation ViewController

@synthesize web;

这篇关于访问另一个类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆