如何从一个客观的c类访问一个viewcontroller的UI元素?
在我的应用程序中,我有一些视图控制器和一个目标c类,我如何访问一个视图控制器的UI元素来改变它们在客观类中的值。如何从一个客观的c类访问一个viewcontroller的UI元素?
为了进一步解释,我有一个UILable *实验室在firstviewcontroller和刚刚导入
#import "firstViewController.h"
在我customclass.m文件,我试图在客观的方法来做这样的C类
[email protected]"example";
(我知道它不是正确的方法,但我试图解释我在做什么)
任何人都可以告诉我我该怎么做?
您需要做的第一件事是获取您想要操作的视图控制器的引用。做到这一点并让其他类可用的一种方法是将引用存储在您在customclass.m中实例化的单例对象中。具体而言,这是该方法的工作原理:
定义Singleton对象:
#import <Foundation/Foundation.h>
@interface MySingleton : NSObject {
MyViewController *myViewController;
}
@propery (nonatomic. retain) MyViewController *myViewController;
+(MySingleton*)sharedMySingleton;
@implementation MySingleton
@synthesize myViewController;
static MySingleton* _sharedMySingleton = nil;
+(MySingleton*)sharedMySingleton
{
@synchronized([MySingleton class])
{
if (!_sharedMySingleton)
[[self alloc] init];
return _sharedMySingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([MySingleton class])
{
NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
// initialize stuff here
}
return self;
}
@end
等。无论您实例化myViewController你这样做:
#import "MySingleton.h"
MySingleton *sinlgeton = [MySingleton sharedMySingleton];
singleton.myViewController = myViewController;
现在您的CustomClass.m你这样做:
#import "MySingleton.h"
MySingleton *sinlgeton = [MySingleton sharedMySingleton];
singleton.myViewController.lab.text = @"example"
正如我所说,这是一个方法来做到这一点。然而,你很可能不想访问和更改自定义类中的视图控制器属性,所以你应该重新考虑你为什么要这么做。
与大多数编程任务一样,有几种解决方案,它取决于具体情况。
您可以让FirstViewController拥有MyCustomClass的实例,然后传递它自己。
@interface FirstViewController : UIViewController {
MyCustomClass *customClass_;
}
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
customClass_ = [[MyCustomClass alloc] init]
customClass_.firstViewController = self;
}
@end
@interface MyCustomClass : NSObject {
FirstViewController *firstViewController;
}
@property (nonatomic, assign) FirstViewController *firstViewController;
@end
这样MyCustomClass可以访问FirstViewController的属性。如果你不想让MyCustomClass访问所有的FirstViewController,你可以使用MyCustomClass应该知道的属性。
@interface FirstViewController : UIViewController {
MyCustomClass *customClass_;
UILabel *lab;
}
@property (nonatomic, retain) IBOutlet UILabel *lab;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
customClass_ = [[MyCustomClass alloc] init]
customClass_.lab = self.lab;
}
@end
@interface MyCustomClass : NSObject {
UILabel *lab;
}
@property (nonatomic, retain) IBOutlet UILabel *lab;
@end
很可能是这样的情况,它没有意义的FirstViewController是MyCustomClass的所有者。也许它是有权访问这两个实例的AppDelegate或某个父控制器。
@interface RandomParentClass : NSObject {
FirstViewController *firstViewController;
MyCustomClass *customClass:
}
@end
@implementation RandomParentClass
- (void)setUpController {
firstViewController = [[FirstViewController alloc] init];
customClass = [[MyCustomClass alloc] init];
customClass.lab = firstViewController.lab;
// or
customClass.firstViewController = firstViewController;
}
@end
感谢回复,我在我的单身的应用程序有一些方法(以asihttprequest相关),因为我开始请求为asychronus并在requestfinished委托我需要一些信息的生命过程中如何去喜欢它在step1中,step1通过或step1failed我想显示它们在视图控制器之一。我试图像分配一些变量的变化,然后使用共享方法将它们提交给我的视图控制器,但它没有即时抓取数据,因为我不知道需要多长时间才能请求完成删除操作并执行下一步操作,我认为使用而当(1) – appleDE 2010-11-08 16:26:41
继续:..当这些变量改变并在这里显示时触发,但我认为它的简单和肮脏的想法,这就是为什么我在其他地方尝试。在稳定性或速度方面从singleton类访问UI元素有什么问题吗? – appleDE 2010-11-08 16:28:30