-3

我在 XIB 中创建了 NSView,然后添加了动态的多个 NSTextField,然后将 NSVIew 添加到 NSScrollView。但是当我改变 TextField 的数量时,它是循环的。我想在添加新的 NSTextField 之前清除所有旧的 NStextfield。我添加了清除功能,但它不起作用,我的应用程序挂起。

guiview refer to NSView (in XIB)

这是我的代码:

 -(void) createTextDynamic : (int) number
{
    for (NSView *subview in [guiView subviews]) { // function to clear all NStextfield but not work
        [subview removeFromSuperview];
    }
    guiView = [[NSView alloc] init];
    float heightView =(8*25 +50)+ (25+30) * number;
    NSPoint pointToScrollTo = NSMakePoint( 400, 0);  // Any point you like.
    [[ScrollView contentView] scrollToPoint: pointToScrollTo];
    [ScrollView reflectScrolledClipView: [ScrollView contentView]];
    guiView.frame = NSMakeRect(0, 0, 400, heightView);
    float label_Y = heightView - 25;
    float textfield_Y = heightView - 25;
    for(int i=1; i<=number;i++)
    {
        NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
        [ssid setStringValue:[NSString stringWithFormat:@"SSID %d :",i]];
        [ssid setSelectable:NO];
        [ssid setEditable:NO];
        [ssid setBordered:NO];
        [ssid setDrawsBackground:NO];
        [ssid setAutoresizingMask:NSViewWidthSizable];
        [guiView addSubview:ssid];
        label_Y -=30;
        [ssid release];

        NSTextField *key = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
        [key setStringValue:@"KEY :"];
        [key setSelectable:NO];
        [key setEditable:NO];
        [key setBordered:NO];
        [key setDrawsBackground:NO];
        [key setAutoresizingMask:NSViewWidthSizable];
        [guiView addSubview:key];
        label_Y -=30;
        [key release];

        ssidtxt = [[NSTextField alloc] initWithFrame:NSMakeRect (200,textfield_Y,200,25)];
        [ssidtxt setBezelStyle:NSTextFieldSquareBezel];
        ssidtxt.tag=i;
         [ssidtxt setAutoresizingMask:NSViewWidthSizable];

        [guiView addSubview:ssidtxt];
        textfield_Y -=30;
        [ssidtxt release];

        keytxt = [[NSTextField alloc] initWithFrame:NSMakeRect (200,textfield_Y,200,25)];
        [keytxt setBezelStyle:NSTextFieldSquareBezel];
        keytxt.tag=100+i;
        [keytxt setAutoresizingMask:NSViewWidthSizable];

        [guiView addSubview:keytxt];
        textfield_Y -=30;
        [keytxt release];

    }
    startLbl_Y = label_Y;
    NSTextField *serverpath = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
    [serverpath setStringValue:@"Server Path :"];
    [serverpath setSelectable:NO];
    [serverpath setEditable:NO];
    [serverpath setBordered:NO];
    [serverpath setDrawsBackground:NO];
    [guiView addSubview:serverpath];
    [serverpath release];

    startText_Y = textfield_Y;
    servertxt = [[NSTextField alloc] initWithFrame:NSMakeRect (200,textfield_Y,200,25)];
    [servertxt setBezelStyle:NSTextFieldSquareBezel];

    [guiView addSubview:servertxt];
    [servertxt release];

    label_Y-=30;
    NSTextField *username = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y ,150,25)];
    [username setStringValue:@"User Name :"];
    [username setSelectable:NO];
    [username setEditable:NO];
    [username setBordered:NO];
    [username setDrawsBackground:NO];
    [guiView addSubview:username];
    [username release];

    textfield_Y -=30;
    usertxt = [[NSTextField alloc] initWithFrame:NSMakeRect (200,textfield_Y,200,25)];
    [usertxt setBezelStyle:NSTextFieldSquareBezel];

    [guiView addSubview:usertxt];
    [usertxt release];
      label_Y-=30;
    NSTextField *key = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
    [key setStringValue:@"KEY :"];
    [key setSelectable:NO];
    [key setEditable:NO];
    [key setBordered:NO];
    [key setDrawsBackground:NO];
    [guiView addSubview:key];
    [key release];
     textfield_Y -=30;
    keytxt = [[NSTextField alloc] initWithFrame:NSMakeRect (200,textfield_Y,200,25)];
    [keytxt setBezelStyle:NSTextFieldSquareBezel];

    [guiView addSubview:keytxt];
    [keytxt release];

      label_Y-=30;
    NSTextField *buzz = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
    [buzz setStringValue:@"Buzzer Mode :"];
    [buzz setSelectable:NO];
    [buzz setEditable:NO];
    [buzz setBordered:NO];
    [buzz setDrawsBackground:NO];
    [guiView addSubview:buzz];
    [buzz release];
    textfield_Y -=60;
    prototype= [[NSButtonCell alloc] init];
    [prototype setTitle:@"Normal"];
    [prototype setButtonType:NSRadioButton];
    NSRect matrixRect = NSMakeRect(200, textfield_Y, 150, 50);
    NSMatrix *myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
                                                    mode:NSRadioModeMatrix
                                               prototype:(NSCell *)prototype
                                            numberOfRows:2
                                         numberOfColumns:1];
    [myMatrix setAction:@selector(radioButtonClicked:)];
    [myMatrix setTarget:self];
    NSArray *cellArray = [myMatrix cells];
    [[cellArray objectAtIndex:0] setTag:0];
    [[cellArray objectAtIndex:1] setTitle:@"Mute"];
    [[cellArray objectAtIndex:1] setTag:1];

    [guiView addSubview:myMatrix];
        [prototype release];
        [myMatrix release];

        [ScrollView  setDocumentView :guiView];
    }

你有什么建议吗?

4

3 回答 3

2
NSArray *viewsToRemove = [[guiView subviews] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return [evaluatedObject isKindOfClass:[NSTextField class]];
    }]];

[viewsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];
于 2013-08-20T07:24:24.290 回答
1

通过在循环通过超级视图的子视图时调用removeFromSuperview子视图,您正在修改-[NSView subviews]您不能做的事情。

但是,假设您的代码有效。看起来您正在尝试删除所有子视图(包括NSMatrix而不仅仅是文本字段)。如果要删除所有子视图,则可以轻松调用:

[[guiView 子视图] makeObjectsPerformSelector:@selector(removeFromSuperview)];

如果您仍在寻找NSTextfield对象,那么您可以调用:

NSArray *viewsToRemove = [[guiView subviews] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return [evaluatedObject isKindOfClass:[NSTextField class]];
    }]];

[viewsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];

indexesOfObjectsPassingTest:方法(我认为这个方法比filteredArrayUsingPredicate:但我不完全确定):

NSIndexSet *indexesToRemove = [[guiView subviews] indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isKindOfClass:[NSTextField class]];
    }];

NSArray *viewsToRemove = [[guiView subviews] objectsAtIndexes:indexesToRemove];

[viewsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];

如果您不喜欢选择器,那么您可以简单地遍历viewsToRemove数组并调用removeFromSuperview. 因为你正在循环一个不同的数组[guiView subviews],所以它不应该在那个部分崩溃或挂起。

于 2013-07-28T06:29:05.727 回答
0

我从来没有用 OSX 做过,但可能是这样的?

NSArray *viewsToRemove = [guiView subviews];
for (NSView *v in viewsToRemove) {
    if ([v isKindOfClass:[NSTextField class]]) {
        [v removeFromSuperview];
    }
}
于 2013-07-28T04:10:34.680 回答