remove all NSTextField in NSVIew - objective-c

I created NSView in XIB and then add dynamic muitple NSTextField, then add NSVIew to NSScrollView. But when I chane number of TextField, it is loop. I want to clear all old NStextfield before add new NSTextField. I had add clear function but it not work, my app hangs.
guiview refer to NSView (in XIB)
This is my code:
-(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];
}
Can you have any suggestions?

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

By calling removeFromSuperview on a subview while looping through the superview's subviews, you're modifying -[NSView subviews] which you can't do.
But let's say that your code works. It looks like you're trying to remove all subviews (including the NSMatrix and not just the text fields). If you want to remove all subviews, then you can easily just call:
[[guiView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
If you're still looking for just the NSTextfield objects, then you can call either:
NSArray *viewsToRemove = [[guiView subviews] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject isKindOfClass:[NSTextField class]];
}]];
[viewsToRemove makeObjectsPerformSelector:#selector(removeFromSuperview)];
Or the indexesOfObjectsPassingTest: approach (I think this one is faster than filteredArrayUsingPredicate: but I'm not entirely sure):
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)];
If you don't like selectors, then you can simply loop through the viewsToRemove array and call removeFromSuperview. Because you're looping through a different array than [guiView subviews], it should not crash or hang on that part.

I have never done it with OSX, but maybe something like the following?
NSArray *viewsToRemove = [guiView subviews];
for (NSView *v in viewsToRemove) {
if ([v isKindOfClass:[NSTextField class]]) {
[v removeFromSuperview];
}
}

Related

UIPickerView with UIActionSheet IOS 8 stop working

Showing a UIPickerView with UIActionSheet in iOS8 not showing
-(void)showPicker{
/************************ FIXED please contact me on nfsarmento#hotmail.com if you need help to fix******//
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
switch((uint)currentDelegate){
case 0:{
pickerView.dataSource = propertyDelegate;
pickerView.delegate = propertyDelegate;
[pickerView selectRow:[propertyDelegate index] inComponent:0 animated:NO];
break;
}
case 1:{
pickerView.dataSource = regionDelegate;
pickerView.delegate = regionDelegate;
[pickerView selectRow:[regionDelegate index] inComponent:0 animated:NO];
break;
}
case 2:{
pickerView.dataSource = townDelegate;
pickerView.delegate = townDelegate;
[pickerView selectRow:[townDelegate index] inComponent:0 animated:NO];
break;
}
case 3:{
pickerView.dataSource = bedDelegate;
pickerView.delegate = bedDelegate;
[pickerView selectRow:[bedDelegate index] inComponent:0 animated:NO];
break;
}
case 4:{
pickerView.dataSource = bathDelegate;
pickerView.delegate = bathDelegate;
[pickerView selectRow:[bathDelegate index] inComponent:0 animated:NO];
break;
}
case 5:{
pickerView.dataSource = priceDelegate;
pickerView.delegate = priceDelegate;
[pickerView selectRow:[priceDelegate index1] inComponent:0 animated:NO];
[pickerView selectRow:[priceDelegate index2] inComponent:1 animated:NO];
break;
}
case 6:{
pickerView.dataSource = currencyDelegate;
pickerView.delegate = currencyDelegate;
[pickerView selectRow:[currencyDelegate index] inComponent:0 animated:NO];
break;
}
}
[actionSheet addSubview:pickerView];
UISegmentedControl *previousButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Previous", nil)]];
previousButton.momentary = YES;
previousButton.frame = CGRectMake(5.0f, 7.0f, 70.0f, 30.0f);
previousButton.segmentedControlStyle = UISegmentedControlStyleBar;
previousButton.tintColor = [UIColor blackColor];
UISegmentedControl *nextButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Next", nil)]];
nextButton.momentary = YES;
nextButton.frame = CGRectMake(80.0f, 7.0f, 70.0f, 30.0f);
nextButton.segmentedControlStyle = UISegmentedControlStyleBar;
nextButton.tintColor = [UIColor blackColor];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Done", nil)]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor colorWithRed:0.35f green:0.55f blue:1.5f alpha:1.0f];
[closeButton addTarget:self action:#selector(hidePicker) forControlEvents:UIControlEventValueChanged];
[previousButton addTarget:self action:#selector(previousPicker) forControlEvents:UIControlEventValueChanged];
[nextButton addTarget:self action:#selector(nextPicker) forControlEvents:UIControlEventValueChanged];
if(currentDelegate > 0)[actionSheet addSubview:previousButton];
if(currentDelegate < 6)[actionSheet addSubview:nextButton];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void) nextPicker{
[self hidePicker];
currentDelegate++;
[self showPicker];
}
-(void) previousPicker{
[self hidePicker];
currentDelegate--;
[self showPicker];
}
-(void)hidePicker{
switch ((uint)currentDelegate) {
case 0:{
[tf_type setText: [propertyDelegate.values objectAtIndex: [propertyDelegate index]]];
_appDelegate.int_typeV = [propertyDelegate index];
break;
}
case 1:{
[tf_region setText: [regionDelegate.values objectAtIndex: [regionDelegate index]]];
_appDelegate.int_regionV = [regionDelegate index];
//Reset Town dropdown when a region is picked
_appDelegate.int_townV = 0;
townDelegate = [[TownDelegate alloc] init];
[self.tf_town setText: [townDelegate.values objectAtIndex: [_appDelegate int_townV]]];
break;
}
case 2:{
[tf_town setText: [townDelegate.values objectAtIndex: [townDelegate index]]];
_appDelegate.int_townV = [townDelegate index];
break;
}
case 3:{
[tf_numBed setText: [bedDelegate.values objectAtIndex: [bedDelegate index]]];
_appDelegate.int_numBedV = [bedDelegate index];
break;
}
case 4:{
[tf_numBath setText: [bathDelegate.values objectAtIndex: [bathDelegate index]]];
_appDelegate.int_numBathV = [bathDelegate index];
break;
}
case 5:{
NSString * priceString = [[NSString alloc] initWithFormat:#"%# - %#",
[priceDelegate.values objectAtIndex: [priceDelegate index1]],
[priceDelegate.values2 objectAtIndex: [priceDelegate index2]]];
[tf_price setText:priceString];
_appDelegate.int_minPriceV = [priceDelegate index1];
_appDelegate.int_maxPriceV = [priceDelegate index2];
break;
}
case 6:{
[tf_currency setText: [currencyDelegate.values objectAtIndex: [currencyDelegate index]]];
_appDelegate.int_currencyV = [currencyDelegate index];
break;
}
}
[popoverController dismissPopoverAnimated:YES];
popoverController = nil;
[self dismissActionSheet];
}
My picker view
#import "PropertyTypeDelegate.h"
#implementation PropertyTypeDelegate
#synthesize values;
-(id)init{
self = [super init];
[self loadData];
return self;
}
-(int)index{ return index;}
-(void) loadData{
NSArray* array = [[NSArray alloc] initWithObjects:
NSLocalizedString(#"No Preference", nil),
NSLocalizedString(#"Villa", nil),
NSLocalizedString(#"Town House", nil),
NSLocalizedString(#"Apartment", nil),
NSLocalizedString(#"Retail", nil),
NSLocalizedString(#"Labour Camp", nil),
NSLocalizedString(#"Office", nil),
NSLocalizedString(#"Warehouse", nil),
NSLocalizedString(#"Land Residential", nil),
NSLocalizedString(#"Hotel apartment", nil),
NSLocalizedString(#"Residential Building", nil),
nil];
self.values = array;
index = 0;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [values count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{ return [values objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *) thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
index = (int) (NSInteger)row;
}
#end
The link provided above actually refers to Apple Doc where it has removed adding subview to UIActionSheet. In your code, you are doing similar thing by adding UIPickerView into UIActionSheet. So in iOS8 onwards, even if the view is added to UIActionSheet, the view returned is actually nil while displaying.
For this purpose you can use ActionSheetPicker-3.0.
Actually, it's not UIActionSheet anymore. But looks exactly the same, and that's why it works on iOS8.
Do let me know if this answers your query!

NSScrollView has space at the bottom and scrollbar always at bottom in Cocoa

I created a NSScrollView and add custom NSVIew in it but when run, it always has a space at bottom and the scroll bar always at bottom. Now, i want to scrollview fit with custom view and scrollbar always at top. This is my code:
float label_Y;
float textfield_Y;
float heightView;
-(void) createTableWiFi: (int) number
{
heightView = (25 + 30) * number;
guiView = [[NSView alloc] initWithFrame:NSMakeRect(0,0,600, heightView)];
label_Y = heightView;
textfield_Y = heightView ;
[[ScrollView verticalScroller] setFloatValue:0.0];
[[ScrollView contentView] scrollToPoint:NSMakePoint(0.0, heightView)];
int i;
for(i=1; i<=number;i++)
{
ssidtxt = [[NSTextField alloc] initWithFrame:NSMakeRect (50,textfield_Y,130,25)];
[ssidtxt setBezelStyle:NSTextFieldSquareBezel];
ssidtxt.tag=i;
[ssidtxt setAutoresizingMask:NSViewWidthSizable];
[guiView addSubview:ssidtxt];
[ssidtxt release];
keytxt = [[KSPasswordField alloc] initWithFrame:NSMakeRect (200,textfield_Y,130,25)];
[guiView addSubview:keytxt];
[keytxt release];
textfield_Y -=30;
}
prototype= [[NSButtonCell alloc] init];
[prototype setTitle:#""];
[prototype setButtonType:NSRadioButton];
NSRect matrixRect = NSMakeRect(500, textfield_Y +25 , 50,25 *(i-1));
myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
mode:NSRadioModeMatrix
prototype:(NSCell *)prototype
numberOfRows:i-1
numberOfColumns:1];
[myMatrix setCellSize:NSMakeSize(50, 30)];
[myMatrix setAction:#selector(radioButtonClicked:)];
NSArray *cellArray = [myMatrix cells];
[guiView addSubview:myMatrix];
[ScrollView setDocumentView :guiView];
[[ScrollView verticalScroller] setFloatValue:0.0];
[[ScrollView contentView] scrollToPoint:NSMakePoint(0.0, heightView)];
[guiView release];
}
Welcome any suggestions. Thanks in advance
Don't do this.
[[ScrollView verticalScroller] setFloatValue:0.0];
You might be forcing it to cause some inconsistencies. If your scrollbar position is not updating(which I am not quite sure if that is your problem) use
[ScrollView reflectScrolledClipView:[ScrollView contentView]];
Also please read Cocoa coding guidelines. Its a relatively short read but very worthwhile.

Get value of NSTextfield by tag number in Cocoa

I have a problem : How can i do If i want get value of NSTextField by tag number?
I create NSTextField dynamically and set tag for it by this code :
for(int i=0; i<number;i++)
{
NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
[ssid setStringValue:[NSString stringWithFormat:#"SSID %d :",i +1]];
[ssid setSelectable:NO];
ssid.tag = i;
[ssid setEditable:NO];
[ssid setBordered:NO];
[ssid setDrawsBackground:NO];
[ssid setAutoresizingMask:NSViewWidthSizable];
[contentView addSubview:ssid];
label_Y -=30;
[ssid release];
NSTextField *ssid2 = [[NSTextField alloc] initWithFrame:NSMakeRect (10,label_Y,150,25)];
[ssid2 setStringValue:[NSString stringWithFormat:#"SSID %d :",i +1]];
[ssid2 setSelectable:NO];
ssid2.tag = i;
[ssid2 setEditable:NO];
[ssid2 setBordered:NO];
[ssid2 setDrawsBackground:NO];
[ssid2 setAutoresizingMask:NSViewWidthSizable];
[contentView addSubview:ssid2];
label_Y -=30;
[ssid2 release];
}
And then i want get value of each NSTextField But i don't know how to get value of NStextfield by tag number? Thanks
Let's say the tag is 7.
[contentView viewWithTag:7]
That returns the tag, if a subview (including the view itself, contentView in this case) has this tag or nil if none is found. You are in charge with making sure to have the tags unique. For that you should never ever use 0 as tag value because 0 is the default value [contentView viewWithTag:0] would return contentView unless you set its tag to something else.

NSScrollView not scroll and size contentView not correct in Cocoa

I have a problem: I create 1 NSScollView in XIB. Then i create 20 NSTextFile programmically and add them to ContentView of ScrollView. But when add mutiple NStextfield, scrollview not scroll and many NStextfield has been removed, not show all NSTextField.
This is my code:
IBOutlet NSView *contentView;
IBOutlet NSScrollView *ScrollView;
Function createDynamictextField:
-(void) createLabelDynamic : (int) number andX: (int) x andY : (int) y
{
for(int i=1; i<=number;i++)
{
NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (x,y,150,100)];
[ssid setStringValue:[NSString stringWithFormat:#"SSID %d :",i]];
[ssid setSelectable:NO];
[ssid setEditable:NO];
[ssid setBordered:NO];
[ssid setDrawsBackground:NO];
[ssid setAutoresizingMask:NSViewWidthSizable];
[contentView addSubview:ssid];
y -=30;
[ssid release];
}
[ScrollView setDocumentView :contentView];
}
And if less NStextField, example is 2 NSTextFiels, position of NSTextField is not correct. It seem not autoresize contentView fit ScrollView. Can you help me?
try like this:
-(void) createLabelDynamic : (int) number andX: (int) x andY : (int) y
{
for(int i=1; i<=number;i++)
{
NSTextField *ssid = [[NSTextField alloc] initWithFrame:NSMakeRect (x,y,150,30)];
[ssid setStringValue:[NSString stringWithFormat:#"SSID %d :",i]];
[ssid setSelectable:NO];
[ssid setEditable:NO];
[ssid setBordered:NO];
[ssid setDrawsBackground:NO];
[ssid setAutoresizingMask:NSViewWidthSizable];
[contentView addSubview:ssid];
y -=30;
[ssid release];
}
contentView.frame = NSMakeRect(0, 0, 150, y + number * 30);
[ScrollView setDocumentView :contentView];
}

How to deselect radio button in cocoa

I'm a newbie. I have a problem with NSMatrix.
I created mutiple NSMatrix but i want first loading, they not checked.
I used this code to created them but it always checked.
prototype= [[NSButtonCell alloc] init];
[prototype setTitle:#""];
[prototype setButtonType:NSRadioButton];
NSRect matrixRect = NSMakeRect(400, textfield_Y, 50, 20);
myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
mode:NSRadioModeMatrix
prototype:(NSCell *)prototype
numberOfRows:1
numberOfColumns:1];
[myMatrix setTag:300+i];
//[myMatrix setAction:#selector(radioButtonClicked:)];
[myMatrix setTarget:self];
NSArray *cellArray = [myMatrix cells];
[[cellArray objectAtIndex:0] setTag:0];
[guiView addSubview:myMatrix];
[prototype release];
[myMatrix release];
Any ideas? Thanks a lot
On an NSButtonCell you would use the setState method of NSCell:
[prototype setState:NSOffState]