簡體   English   中英

cocoa 以編程方式向窗口添加文本

[英]cocoa add text to window programmatically

我試圖在不使用 Xcode 的情況下用 Objective-c 中的一些非常基本的 Cocoa 編程來弄濕我的腳。 這主要用於學習目的,而不是用於現實世界的應用程序開發。

我使用以下源代碼創建了一個“hello world”程序:

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    NSApplication* app = [NSApplication sharedApplication];
    id window =
         [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 400)
                                            styleMask:NSWindowStyleMaskTitled
                                                 backing:NSBackingStoreBuffered
                                                   defer:NO];
    [window setTitle:@"Hello world"];
    [window makeKeyAndOrderFront:nil];
    [window center];


    NSText* t = [[NSText alloc] initWithFrame:NSMakeRect(20,20,100,100)];
    [t setString:@"test"];

    [window addSubview:t];

    [app run];

    return 0;
}

在上面的示例中,我嘗試向窗口添加一些簡單的文本“Test”。 但是,一旦我啟動編譯的應用程序,我就會得到:

-[NSWindow addSubview:]: unrecognized selector sent to instance 0x7fb8e0611270
2017-06-24 14:34:02.777 lala[39810:1252869] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSWindow addSubview:]: unrecognized selector sent to instance 0x7fb8e0611270'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff93bcf2cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffa89da48d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff93c50f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff93b41755 ___forwarding___ + 1061
    4   CoreFoundation                      0x00007fff93b412a8 _CF_forwarding_prep_0 + 120
    5   lala                                0x0000000103b0feab main + 651
    6   libdyld.dylib                       0x00007fffa92bf235 start + 1
    7   ???                                 0x0000000000000001 0x0 + 1
)

為什么我的應用程序崩潰[NSWindow addSubview:]: unrecognised selector sent to instance 如何以編程方式向窗口添加文本?

嘗試使用 NSTextField 而不是 NSText:

NSRect frameRect = NSMakeRect(20,20,100,100)
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubView:myTextField];

此外,您需要制作基於 AppDelegate 的應用程序或基於 Storyboard 的應用程序,以便您可以使用 NSView 來顯示您的對象。

來到這里尋找將 NSTextfield 添加到視圖的解決方案,但它證實了我已經嘗試使用的內容。 相反,我嘗試使用“setContentView”,但文本填充了視圖,而我需要它作為子視圖工作。 所以這是我的代碼:

NSTextField *yourLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width , height * 1.0/3.0)];
    yourLabel.editable = false;
    yourLabel.bezeled = true;
    [yourLabel setTextColor:[NSColor blackColor]];
    [yourLabel setBackgroundColor:[NSColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.5]];
    [yourLabel setFont:[NSFont fontWithName:@"GurmukhiMN-Bold" size:(height/24)]];
    yourLabel.stringValue = [NSString stringWithFormat:@"The Second Output Is Operational"];
    yourLabel.alignment = NSTextAlignmentCenter;
    [self.window.contentView addSubview:yourLabel];

主要區別在於我將消息發送到哪里。 似乎這是添加子視圖的最佳方法,而其他方法會導致競爭條件或崩潰。

為了完整起見,我之前得到了寬度和高度(它們在整個視圖中使用,所以抓住它們一次):

height = self.window.frame.size.height;
width = self.window.frame.size.width;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM