2014-05-22 11 views
0

我試圖修改NSTextView對象的字符串內容。到目前爲止,我已經能夠使它與以下代碼一起工作。在IBAction方法之外更改NSTextView內容

.h文件中

#import "AppDelegate.h" 

@interface InterfaceManager : AppDelegate { 
    IBOutlet NSTextView *content; 
} 

-(IBAction)displayOutput:(id)sender; 

@property (assign) IBOutlet NSWindow *window; 

@end 

.m文件

#import "InterfaceManager.h" 

@implementation InterfaceManager 

-(IBAction)displayOutput:(id)sender { 
    [content setString:@"This is a string"]; 
} 

@end 

所有工作完全正常和不正是我想要的東西。


但是我需要能夠在void方法中做同樣的事情。這是我嘗試過的。

.h文件中

#import "AppDelegate.h" 

@interface InterfaceManager : AppDelegate { 
    IBOutlet NSTextView *content; 
} 

-(void)displayOutput:(NSString *)string; 

@property (assign) IBOutlet NSWindow *window; 

@end 

.m文件

#import "InterfaceManager.h" 

@implementation InterfaceManager 

-(void)displayOutput:(NSString *)string { 
    [content setString:string]; 
} 

@end 

,它似乎並沒有工作。

我在做什麼錯誤,以及如何修復它使其工作?

在此先感謝! :)

回答

0

如果我的理解正確,當您按下按鈕時設置文本,但是您還想設置文本而無需用戶按下按鈕。如果是這種情況,那麼你不需要一個「無效方法」,你可以使用任何方法來做到這一點。例如,當我第一次使用此代碼進入屏幕時,我在我的選項屏幕中設置了客戶端名稱。

- (void)viewDidLoad { 

    // All games allow a client and rewards 
    if (self.currentClient) { 
     self.clientInput.text = self.currentClient; 
    } 
} 

如果你想有一個單獨的方法來設置該字符串,我想你可以使用這樣的事情:

-(void)myContentSettingMethod { 
    [self.content setString:@"This is a string"]; 
} 

,然後調用它,只要你想設置的內容。