我在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];
}
你能有什么建议吗?
答案 0 :(得分:2)
NSArray *viewsToRemove = [[guiView subviews] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject isKindOfClass:[NSTextField class]];
}]];
[viewsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];
答案 1 :(得分:1)
在循环浏览超级视图的子视图时,通过在子视图上调用removeFromSuperview
,您正在修改-[NSView subviews]
,而这是您无法做到的。
但是,假设您的代码有效。您似乎正在尝试删除所有子视图(包括NSMatrix
而不仅仅是文本字段)。如果您想删除所有子视图,那么您可以轻松地调用:
[[guiView subviews] 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]
的数组,所以它不会崩溃或挂在那个部分上。
答案 2 :(得分:0)
我从来没有用OSX做过,但可能会出现以下情况?
NSArray *viewsToRemove = [guiView subviews];
for (NSView *v in viewsToRemove) {
if ([v isKindOfClass:[NSTextField class]]) {
[v removeFromSuperview];
}
}