Create a radio button matrix in Cocoa - objective-c

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)

Related

Sizing ButtonCells in NSMatrix

I am trying to implement a Radiogroup with cocoa and used the example provided by apple Using Radio Buttons
The following screenshot shows the problem I have. Even if the NSMatrix containing the cells have a NSRect large enough the cells themselves are not wide enough to display the titles.
How can I correct this?
NSButtonCell *prototype = [[[NSButtonCell alloc] init]autorelease];
[prototype setButtonType:NSRadioButton];
[prototype setBordered:YES];//only for testing
_view = [[[NSMatrix alloc] initWithFrame:rect
mode:NSRadioModeMatrix
prototype:(NSCell *)prototype
numberOfRows:3
numberOfColumns:1]autorelease];
NSArray *cellArray = [_view cells];
for (std::size_t index = 0; index < 3; ++index)
{
[[cellArray objectAtIndex:index] setTitle:#"a title"];
}
Use setCellSize of matrix.
NSSize size = [_view cellSize];
size.width = 400;
[_view setCellSize:size];

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.

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]

Save multiple custom UIImageViews

I was recently examining the project "Demo Photo Board" found here.
This is a simple demonstration of adding UIImageViews to the screen that have UIGestureRecognizers added to them...allowing the user to manipulate the various UIImageViews.
I add the view like so:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageview.userInteractionEnabled = YES;
[imageview setImage:image];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[imageview addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[imageview addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageview addGestureRecognizer:panRecognizer];
[self.view addSubview:imageview]; }
I can even save the view like so:
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:imageview] forKey:#"imageViewSaved"];
Now my question: The following method saves only the last imageview added to the screen. Anyone know how to save all of the imageviews that are on the screen...if the user adds more than one?
I really don't think you want to be saving the entire UIImageView object to the user defaults. Instead, try saving a list of UIImage objects, which will be much more lightweight. (And which make much more sense to serialize.)
You can do this like so:
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSArray *savedArray = [defs objectForKey:#"SAVED_IMAGES"];
// Create a mutable version in case the serialized copy is an NSArray
NSMutableArray *mutableImages = savedArray ? [NSMutableArray arrayWithArray:savedArray] : [NSMutableArray array];
[mutableImages addObject:image];
[defs setObject:mutableImages forKey:#"SAVED_IMAGES"];
[defs save];
To distinguish the UIImageViews on the screen you can do the following.
Define the start ImageView Tag number
#define kImageViewTag 3000
Define an instance var for number of images added to the screen.
NSUInteger numberOfImage = 0
Whenever you create a new UIImageView, increase the count and add it with the kImageViewTag and assign it to the tag property.
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageview.userInteractionEnabled = YES;
[imageview setImage:image];
numberOfImage += 1;
imageView.tag = kImageViewTag + numberOfImage;
Whenever you enumerate the screen subviews, if the class is UIImageView with the tag >= kImageViewTag, you know those are the images added to the screen from the UIImagePickerController.

How can I get a value from UITextField in UIAlertView?

I have Used in cocos2d game engine. this is my code:
-(void)timed1: (id)sender {
UIAlertView *dialog = [[[UIAlertView alloc] init] retain];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Time:"];
[dialog setMessage:#" "];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[dialog addSubview:nameField];
[nameField setBackgroundColor:[UIColor whiteColor]];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0);
[dialog setTransform: moveUp];
[dialog addButtonWithTitle:#"Done"];
[dialog addButtonWithTitle:#"Cancel"];
nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
nameField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//timeStatus is a int type global variable //
timeStatus =[nameField.text intValue]; //** this line not working**//
[nameField release];
[dialog show];
}
The reason you cannot get a value is that the function will exit BEFORE the user types anything! When you call [dialog show] the dialog is displayed to the user where they may then enter text.
You should wait until the user selects a button, which will trigger your delegate function, and THEN access the text field property.
Note to do this you will either need to retain a pointer to the UITextField object you added (easiest), or find it again amongst the subviews and cast it.
Release nameField later, and get the text property from there.