2010-11-08 36 views
1

在我的應用程序中,我有一些視圖控制器和一個目標c類,我如何訪問一個視圖控制器的UI元素來改變它們在客觀類中的值。如何從一個客觀的c類訪問一個viewcontroller的UI元素?

爲了進一步解釋,我有一個UILable *實驗室在firstviewcontroller和剛剛導入

#import "firstViewController.h"

在我customclass.m文件,我試圖在客觀的方法來做這樣的C類

[email protected]"example"; 

(我知道它不是正確的方法,但我試圖解釋我在做什麼)

任何人都可以告訴我我該怎麼做?

回答

0

您需要做的第一件事是獲取您想要操作的視圖控制器的引用。做到這一點並讓其他類可用的一種方法是將引用存儲在您在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" 

正如我所說,這是一個方法來做到這一點。然而,你很可能不想訪問和更改自定義類中的視圖控制器屬性,所以你應該重新考慮你爲什麼要這麼做。

+0

感謝回覆,我在我的單身的應用程序有一些方法(以asihttprequest相關),因爲我開始請求爲asychronus並在requestfinished委託我需要一些信息的生命過程中如何去喜歡它在step1中,step1通過或step1failed我想顯示它們在視圖控制器之一。我試圖像分配一些變量的變化,然後使用共享方法將它們提交給我的視圖控制器,但它沒有即時抓取數據,因爲我不知道需要多長時間才能請求完成刪除操作並執行下一步操作,我認爲使用而當(1) – appleDE 2010-11-08 16:26:41

+0

繼續:..當這些變量改變並在這裏顯示時觸發,但我認爲它的簡單和骯髒的想法,這就是爲什麼我在其他地方嘗試。在穩定性或速度方面從singleton類訪問UI元素有什麼問題嗎? – appleDE 2010-11-08 16:28:30

0

與大多數編程任務一樣,有幾種解決方案,它取決於具體情況。

您可以讓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 
相關問題