2013-09-25 92 views
-1

今天我面臨着一個新的問題:從其他類中調用SetNeedsDisplay

我有一個視圖控制器(的NSView的子類)和其他類(NSObject的子類),它通過一個IBAction爲,嘗試回調的viewController使用SetNeedsDisplay:YES重繪其視圖。

重繪視圖(ViewController.m)的方法是:

- (void) redrawView { 
[self setNeedsDisplay:YES] 
} 
// With an NSLog i see that the method is called ! 
// Instead of self i tried even an outlet connected to the custom view. 

}

我在做什麼通過我的其他類調用視圖控制器的方法是:

1 )#import "ViewController.h"

2)進入IBAction我做了一個新的ViewController爲:

ViewController *newIstanceOfViewController = [[ViewController alloc] init] 

3)[newIstanceOfViewController redrawView]

日誌告訴我,setNeedsDisplay被稱爲,但沒有的drawRect!爲什麼?我忘了初始化或子查看內容?由於


原代碼HERE(方法由於語言,相同的語法的不同的名稱)

// Controller.h 

#import <Cocoa/Cocoa.h> 

@interface Controller : NSView { 
    IBOutlet NSView *tastierinoView; 
} 
- (void) aggiornaTastierinoView; 

@end 


// Controller.m 

#import "Controller.h" 

@implementation Controller 

//Contatore numero volte disegno view 
int contatore = 0; 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
     tastierinoView = [[NSView alloc] init]; 

    } 
    return self; 
} 

- (void) aggiornaTastierinoView { //THIS IS THE METHOD TO CALL REDRAW 
    [tastierinoView setNeedsDisplay:YES]; 
    NSLog(@"Chiamo setneedsDisplay\n\n"); 
} 

- (void)drawRect:(NSRect)dirtyRect 
{ 
    contatore++; 
    NSLog(@"La view è stata disegnata %d volte\n\n",contatore); 
    [super drawRect:dirtyRect]; 

    // Drawing code here. 
    NSBezierPath* percorso = [NSBezierPath bezierPath]; 
    [[NSColor cyanColor] set]; 

    [percorso moveToPoint:NSMakePoint(150, 150)]; 
    [percorso lineToPoint:NSMakePoint(250, 150)]; 

    [percorso setLineWidth:5.0]; 
    [percorso stroke]; 

} 

@end 

// ManipolatorePin.h 

#import <Foundation/Foundation.h> 

@interface ManipolatorePin : NSObject { 
    IBOutlet NSWindow *finestraPrincipaleWindow; 
    IBOutlet NSTextField *codiceTextField; 
    NSArray *coordinate_x; 
    NSArray *coordinate_y; 
    //L'array sotto riportato serve, tramite un ciclo for, a salvare il codice pin spezzato in singoli numeri che corrisponderanno al punto. 
    NSMutableArray *numeroAllaIndexArray; 
} 

- (IBAction)aggiornaTastierino:(id)sender; 

@end 

// ManipolatorePin.m 


#import "ManipolatorePin.h" 
#import "Controller.h" 

@implementation ManipolatorePin 



- (void)awakeFromNib { 
    coordinate_x = [[NSArray alloc] initWithObjects:@"150",@"50",@"150",@"250",@"50",@"150",@"250",@"50",@"150",@"250", nil]; 
    coordinate_y = [[NSArray alloc] initWithObjects:@"50",@"150",@"150",@"150",@"250",@"250",@"250",@"350",@"350",@"350", nil]; 


    numeroAllaIndexArray = [[NSMutableArray alloc] init]; 

    NSLog(@"Array coordinate iniziallizato.\nTest:(%@) -> deve risultare \"50\"\n\n",[coordinate_x objectAtIndex:4]); 
} 

- (IBAction)aggiornaTastierino:(id)sender { 
    NSString *codiceString = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"%@",[codiceTextField stringValue]]]; 
    NSLog(@"codiceTextField = %@", codiceString); 

    int lunghezzaCodiceString; 
    NSLog(@"Il codice risulta essere composto da (%d) numeri\n\n",[codiceString length]); 

    //Svuoto array 
    [numeroAllaIndexArray removeAllObjects]; 

    for (lunghezzaCodiceString = 0; lunghezzaCodiceString < [codiceString length]; lunghezzaCodiceString++) { 
     //Compilo array (Ci provo ahah) 
     NSString *carattereDelCodiceStringInEsame = [[NSString alloc] init]; 
      carattereDelCodiceStringInEsame = [codiceString substringWithRange:NSMakeRange(lunghezzaCodiceString,1)]; 

     NSLog(@"Aggiungo il numero (%@) all'array 'numeroAllaIndexArray'",carattereDelCodiceStringInEsame); 

      [numeroAllaIndexArray addObject:carattereDelCodiceStringInEsame]; 
    } 

    //DEBUG - DA QUI IN POI E' CANCELLABILE 
    NSLog(@"\n\nCiclo for termitato\nProcesso concluso con successo\n\n\nContenuto array:"); 
    int conteggioArray; 
    for (conteggioArray = 0; conteggioArray < [numeroAllaIndexArray count] ; conteggioArray++) { 
     NSLog(@"index (%d) -> (%@)",conteggioArray,[numeroAllaIndexArray objectAtIndex:conteggioArray]); 
     NSLog(@"\n\n"); 
    } 
    //FINE DEBUG 

    ///////////////HERE THE INSTANCE TO CALL THE CONTROLLER 
    Controller *istanzaGestionaleController = [[Controller alloc] init]; 
    [istanzaGestionaleController aggiornaTastierinoView]; 


} 

@end 

回答

2

這聽起來像你的ViewController的兩個不相關的實例。您需要在屏幕上實際有視圖的實例上調用redrawView。

在你的類(這不是NSViewController),創建這樣一個屬性:

@property (nonatomic, readwrite, weak) NSViewController *vc; 

當類實例化:

YourClass *yourInstance = [[YourClass alloc] init]; 

分配VC的價值:

yourInstance.vc = self; // self is your NSViewController instance 

然後,當您要觸發重繪時,將命令傳遞給其他類中的self.vc秒。

+0

這將是解決它的最佳方法? –

+0

更新了一些示例代碼。 – bneely

+0

謝謝,這項工作可可嗎?我不在iOS上。我相信myIstance.vc會給我一個。錯誤 –