NSPopUpButton所选项目的标题未选中

时间:2013-11-18 14:22:17

标签: objective-c cocoa

那是代码: TheViewer.h

//
//  TheViewer.h
//  The Writer
//
//  Created by Imac on 17/11/13.
//  Copyright (c) 2013 Imac. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TheViewer : NSObject
{

    IBOutlet NSTextField *textField;
    IBOutlet NSTextField *viewer;
    IBOutlet NSColor *textColor;
    IBOutlet NSColorWell *well;
    IBOutlet NSPopUpButton *popup;

}

- (IBAction)sendText:(id)sender;
- (IBAction)deleteText:(id)sender;
- (IBAction)settingColor:(id)sender;


@end

这里是TheViewer.m

//
//  TheViewer.m
//  The Writer
//
//  Created by Imac on 17/11/13.
//  Copyright (c) 2013 Imac. All rights reserved.
//

#import "TheViewer.h"

@implementation TheViewer


- (IBAction)sendText:(id)sender
{

    NSString *testo = [textField stringValue];
    [viewer setStringValue:testo];
    NSString *theItem = [NSString stringWithFormat:@"",[popup titleOfSelectedItem]];

    if([theItem isEqualToString:@"Red"])
    {
        [viewer setTextColor:[NSColor redColor]];
    }
    if([theItem isEqualToString:@"Blue"])
    {
        [viewer setTextColor:[NSColor redColor]];
    }

    if([theItem isEqualToString:@"Yellow"])
    {
        [viewer setTextColor:[NSColor redColor]];
    }


}

- (IBAction)deleteText:(id)sender
{
    [viewer setStringValue:@""];
}

- (IBAction)settingColor:(id)sender
{
    [viewer setTextColor:sender];
}

@end

它应该使用用户选择的颜色为文本着色但是......它不起作用!有人可以告诉我为什么吗?

我也试过了colorweel,但我没有理解它是如何工作的。我知道,我知道,我只是一个菜鸟。

1 个答案:

答案 0 :(得分:3)

我相信你的问题在这里:

 NSString *theItem = [NSString stringWithFormat:@"",[popup titleOfSelectedItem]];

将其更改为:

 NSString *theItem = [NSString stringWithFormat:@"%@",[popup titleOfSelectedItem]];

您忘记输入%@,因为没有匹配。