我在 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];
}
你有什么建议吗?