[Cocoa]_[初级]_[使用类对象管理动态创建的控件,并获取控件存储的值]

本文介绍了一种通过创建类来管理动态生成的界面控件的方法,包括按钮、文本框等,并展示了如何通过数组存储这些控件以便于后续操作,如获取控件的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:我们在界面布局的时候,使用代码动态创建一些控件,如NSButton、NSTextField、NSComboBox,进行界面布局。我们布局好控件之后,如何更好的获取控件的值,一般的做法是把布局在NSView里面的所有subViews数组里面存放的控件进行遍历,查找到该类控件的时候,获取该控件的值。不过这个是很低效和很麻烦的做法,不可以采取。我们可以在动态创建控件的时候,使用一个类对创建的控件进行管理,把控件赋值给一个id类型,并把该对象存入一个数组,我们要获取控件存放的值的时候,遍历该数组,并用枚举变量进行区分控件的类别。具体用例子进行说明。

MqjMainView.h

#import <Cocoa/Cocoa.h>

@interface MqjMainView : NSView

@end


MqjMainView.m

#import "MqjMainView.h"

@implementation MqjMainView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}
-(BOOL) isFlipped
{
    return YES;
}
-(void) drawRect:(NSRect)dirtyRect
{
    [[NSColor colorWithCalibratedRed: 205/255.0 green:185/255.0 blue:255/255.0 alpha:1] setFill];
    NSRectFill(dirtyRect);
    
}

@end


ProcessControlObject.h

#import <Foundation/Foundation.h>

enum KPropertyEditType
{
    kPhoneEditType,
    kEmailEditType,
    kImEditType
};

@interface UiProperty : NSObject
{
    NSString *propertyId;
    NSString *mimeType;
    NSString *type;
    NSString *typeInt;
    NSString *value;
    
    NSUInteger operateType;
    
}
@property (readwrite,copy)  NSString *propertyId;
@property (readwrite,copy) NSString *mimeType;
@property (readwrite,copy) NSString *type;
@property (readwrite,copy) NSString *typeInt;
@property (readwrite,copy) NSString *value;
@property (readwrite,assign) NSUInteger operateType;

@end


@interface ProcessControlObject : NSObject
{
    NSUInteger controlId_;
    NSUInteger controlType_;
    
    UiProperty *prop;
    
    id view1_;
    id view2_;
    id view3_;
}

@property (readwrite,assign) NSUInteger controlId_;
@property (readwrite,assign) NSUInteger controlType_;

@property (readwrite,assign) UiProperty *prop;

@property (readwrite,assign) id view1_;
@property (readwrite,assign) id view2_;
@property (readwrite,assign) id view3_;

@end


ProcessControlObject.m

#import "ProcessControlObject.h"

@implementation UiProperty
@synthesize propertyId,mimeType,type,typeInt,value,operateType;

-(id) init
{
    self =[super init];
    mimeType =@"";
    propertyId =@"0";
    typeInt =0;
    type =@"";
    value =@"";
    
    return self;
}
@end

@implementation ProcessControlObject
@synthesize controlId_,controlType_,prop;
@synthesize view1_,view2_,view3_;
-(id) init
{
    self =[super init];
    controlId_ =0;
    controlType_ =0;
    
    prop =nil;
    view1_ = nil;
    view2_ = nil;
    view3_ = nil;
    
    return self;
}

@end


MqjControlDelegate.h

#import <Cocoa/Cocoa.h>
@class MqjMainView;

@interface MqjControlDelegate : NSObject
{
    IBOutlet MqjMainView *mainView;
    
    NSMutableArray *proAarray;
    CGFloat buttonOffset;
    
    NSMutableArray *controlsArray;
}
-(IBAction)onSave:(id)sender;

@end


MqjControlDelegate.m

#import "MqjControlDelegate.h"
#import "ProcessControlObject.h"

@implementation MqjControlDelegate

-(void) awakeFromNib
{
    proAarray =[NSMutableArray new];
    controlsArray =[NSMutableArray new];
    
    buttonOffset =15.0;
    [self doLoadData];
    [self doSetControlPosition];
    
}
-(void) doLoadData
{
    for (NSUInteger i = 0; i< 7; ++i)
    {
        
        UiProperty *property = [UiProperty new];
        if (i<3)
        {
            [property setMimeType:@"Phone Number"];
            [property setType:@"Home"];
            [property setValue:[NSString stringWithFormat:@"1894576%ld",i]];
            
            
        }
        
        else if (i<=5)
        {
            [property setMimeType:@"Email"];
            [property setType:@"Work"];
            [property setValue:[NSString stringWithFormat:@"1894576%ld@163.com",i]];
        }
        if (i==6) {
            [property setMimeType:@"IM"];
            [property setType:@"Blog"];
            [property setValue:[NSString stringWithFormat:@"59468284%ld",i]];
            
        }
        // [property setOperate:kOperateModifyType];
        [property setPropertyId:[NSString stringWithFormat:@"%ld",i]];
        [proAarray addObject:property];
    }

}

-(void) doSetControlPosition
{
    NSPoint p =NSMakePoint([mainView frame].origin.x-16.0, 0);
    NSString *mimeType =@"";
    for(UiProperty *property in proAarray)
    {
       
        if(![property.mimeType isEqualToString:mimeType])
        {
            
            mimeType =property.mimeType;
           
            NSTextField *text =[self doAddFiledLabel:mimeType withPoint:p];
            [mainView addSubview:text];
            p.y = p.y  + buttonOffset;
            
        }
        
        [self doAddControl:property withPoint:p];
        p.y+=30.0;
    }

}

-(void) doAddControl:(UiProperty*)property withPoint:(NSPoint)p
{

    ProcessControlObject *pco =[ProcessControlObject new];
    [pco setControlId_:0];
    
    if ([property.mimeType isEqualToString:@"Phone Number"])
    {
        [pco setControlType_:kPhoneEditType];
    }
    else if ([property.mimeType isEqualToString:@"Email"])
    {
        [pco setControlType_:kEmailEditType];
    }
    else
    {
        [pco setControlType_:kImEditType];
    }
    NSComboBox *comboBox = [self doAddComboBox:NSMakeRect(p.x, p.y+4, 100, 27) andData:property];
    NSPoint point = p;
    point.x = point.x+115;
    
    NSTextField *field =[self doAddTextFiled:property.value withPoint:point];
    
    
    [mainView addSubview:comboBox];
    [mainView addSubview:field];
    [pco setProp:property];
    [pco setView1_:comboBox];
    [pco setView2_:field];
    [controlsArray addObject:pco];
}

-(NSComboBox*) doAddComboBox:(NSRect) rect andData:(id) data
{
    NSComboBox *comboBox =[[NSComboBox alloc] initWithFrame:rect];
    [comboBox setButtonBordered:YES];
    [comboBox setUsesDataSource:NO];
    
    UiProperty *property = data;
    
    NSArray *array = nil;
    //判断属性是什么类型,对菜单进行填充
    if ([property.mimeType isEqualToString:@"Phone Number"]) {
        NSString *str = @"";
        str =@"Custom;Home;Mobile;Work;Other;Callback";
        
        array =[str componentsSeparatedByString:NSLocalizedString(@";",nil)];
    }
    else if ([property.mimeType isEqualToString:@"Email"])
    {
        NSString *str =@"";
        str =@"Custom;Home;Work;Other;Mobile";
        array =[str componentsSeparatedByString:NSLocalizedString(@";",nil)];
    }
    else if ([property.mimeType isEqualToString:@"IM"])
    {
        NSString *str =@" ";
        str =@"aim;mas;yahoo;skype;qq;google_talk;icq";
        
        array =[str componentsSeparatedByString:NSLocalizedString(@";",nil)];
    }
    else if ([property.mimeType isEqualToString:@"Address"])
    {
        NSString *str =@" ";
        
        str =@"Custom;Home;Work;Other";
        array =[str componentsSeparatedByString:NSLocalizedString(@";",nil)];
    }
    //设置comboBox的下拉列表选项
    [comboBox addItemsWithObjectValues:array];
    
    [comboBox selectItemAtIndex:0];
    
    return comboBox;
    
}

-(NSTextField *) doAddTextFiled:(NSString *) title withPoint:(NSPoint) p
{
    NSTextField *filed =[[NSTextField alloc] initWithFrame:NSMakeRect(p.x, p.y, 170, 30)];
    [filed setBordered:YES];
    [filed setEditable:YES];
    [filed setBezeled:YES];
    [filed setBezelStyle:NSTextFieldSquareBezel];
    
    [filed setStringValue:title];
    [filed setTranslatesAutoresizingMaskIntoConstraints:YES];
    
    
    [filed setAction:@selector(sendsActionOnEndEditing)];
    //[editContactView addSubview:filed];
    return filed;
    
}
-(NSTextField*) doAddFiledLabel:(NSString *) title withPoint:(NSPoint) p
{
    NSTextField *mimeTypeFiled =[[NSTextField alloc] initWithFrame:NSMakeRect(p.x, p.y, 200, 24)];
    [mimeTypeFiled setStringValue:title];
    [mimeTypeFiled setFont:[NSFont fontWithName:@"Arial" size:13]];
    [mimeTypeFiled setTextColor:[NSColor controlTextColor]];
    [mimeTypeFiled setBackgroundColor:[NSColor controlColor]];
    [mimeTypeFiled setBordered:NO];
    [mimeTypeFiled setEditable:NO];
    
    return mimeTypeFiled;
}

-(IBAction)onSave:(id)sender
{
    for (ProcessControlObject *pco in controlsArray)
    {
        switch (pco.controlType_) {
            case kPhoneEditType:
            {
                [self ProgressProperty:pco];
                break;
            }
            case kEmailEditType:
            {
                [self ProgressProperty:pco];
                break;
            }
            case kImEditType:
            {
                [self ProgressProperty:pco];
                break;
            }
        }
    }
}

-(void)ProgressProperty:(ProcessControlObject *)pco
{
    NSComboBox *comboBox =(NSComboBox*)pco.view1_;
    NSString *title =[comboBox stringValue];
    
    NSTextField *text =(NSTextField*)pco.view2_;
    NSString *textStr =[text stringValue];
    NSLog(@"title:%@,text:%@",title,textStr);
}


@end



运行结果:


获取值结果:



 项目代码:

http://download.youkuaiyun.com/detail/moqj_123/9232309

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值