How to deselect radio button in cocoa - objective-c

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]

Related

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.

remove all NSTextField in NSVIew

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];
}
}

Create a radio button matrix in Cocoa

I want to create a matrix of 3 radio buttons in my OS X app. Does anyone know how to do this programatically?
NSButtonCell *prototype = [[NSButtonCell alloc] init];
[prototype setTitle:#"Watermelons"];
[prototype setButtonType:NSRadioButton];
NSRect matrixRect = NSMakeRect(20.0, 20.0, 125.0, 125.0);
NSMatrix *myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
mode:NSRadioModeMatrix
prototype:(NSCell *)prototype
numberOfRows:3
numberOfColumns:1];
[[[typeField window] contentView] addSubview:myMatrix];
NSArray *cellArray = [myMatrix cells];
[[cellArray objectAtIndex:0] setTitle:#"Apples"];
[[cellArray objectAtIndex:1] setTitle:#"Oranges"];
[[cellArray objectAtIndex:2] setTitle:#"Pears"];
[prototype release];
[myMatrix release];
code snippet from Using Radio Buttons in Apple's docs.
Note the usage of prototype NSButtonCell - that way we can tell NSMatrix that buttons of NSRadioButton type should be used (which cannot be done using just cell's class it seems)

ActionSheet in NavController with TabBarController - Exc_Bad_Access

I'm currently have a weird problem.
I have an actionsheet with pickerView in with two button (cancel, and Ok).
They work perfectly until i choose a value in that picker and i click on the first button of my TabBarController which contain my NavigationController.
My actionsheet is declared in my .h and has property (nonatomic, retain), i release it in my dealloc function.
I have an exc_bad_access if i release this object and i dont know why because i allocated it.
This is my function which create my Actionsheet if is nil, and build it.
- (IBAction)select_marque :(id)sender
{
if (!actionSheet_marque)
{
actionSheet_marque = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
}
[actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
picker_marque = [[UIPickerView alloc] initWithFrame: pickerFrame];
picker_marque.showsSelectionIndicator = YES;
picker_marque.dataSource = self;
picker_marque.delegate = self;
[actionSheet_marque addSubview: picker_marque];
[picker_marque release];
UILabel *lbl = [[UILabel alloc] initWithFrame: CGRectMake(110, 7.0f, 150.0f, 30.0f)];
lbl.text = #"Marque";
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
[actionSheet_marque addSubview:lbl];
[lbl release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Ok"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet_marque addSubview:closeButton];
[closeButton release];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(5, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor redColor];
cancelButton.tag = 1;
[cancelButton addTarget:self action:#selector(cancelActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet_marque addSubview:cancelButton];
[cancelButton release];
[actionSheet_marque showInView: self.navigationController.view];
[actionSheet_marque setBounds:CGRectMake(0, 0, 320, 485)];
}
I check also if the pointer is nil in dealloc method....
Thanks
If you have property of UIActionsheet then you should use self.actionSheet_marque when you allocate it. Try changing code like this
if (!actionSheet_marque)
{
self.actionSheet_marque = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
}
[self.actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
And in whole function except this line if (!actionSheet_marque)

Getting Memory allocation problem at UIBarButtonItem in Iphone sdk

Here I am getting memory allocation problem at UIBarButtonItem and the related code for that is:
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackOpaque;
[toolbar setFrame:CGRectMake(0, 350,320,20)];
[self.view addSubview:toolbar];
UIBarButtonItem* barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(categoryConfig:)] ;
rightBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(dialogOtherAction:)] ;
UIBarButtonItem* barItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(categoryConfig:)] ;
NSArray *items = [NSArray arrayWithObjects: barItem1,rightBarItem,barItem2, nil];
[barItem1 release];
[barItem2 release];
[rightBarItem release];
[toolbar setItems:items animated:NO];
after adding UIBarButtonItems into the array items I released them.even though its showing allocations at barbuttons.
can any help me for this?
Thank you,
Monish.
You allocated toolbar with toolbar = [UIToolbar new];, so you need to release it.