CPDatePicker - open at dateValue not today's month - cappuccino

Can you programatically trigger the buttons on a CPDatePicker graphical calendar? I'm setting the date object fine, but the calendar initially displays the current month, with today's day in blue, rather than the dateValue month. Clicking the little circle button between the month arrow stepper buttons switches the display to the dateValue. I'm doing this in code not IB. I have trawled CP and NS documentation but am stuck!

The implementation for CPDatePicker uses a Control _CPDatePickerCalendar for display purposes in the mode I think you're asking about. This control appears to set its view to display whatever date was selected:
- (void)setDateValue:(CPDate)aDateValue
{
var dateValue = [aDateValue copy];
[dateValue _dateWithTimeZone:[_datePicker timeZone]];
[_monthView setMonthForDate:dateValue];
[_headerView setMonthForDate:[_monthView monthDate]];
[self setNeedsLayout];
[self setNeedsDisplay:YES]; // <-- makes cappuccino redraw the calendar
}
This is called from the CPDatePicker when its' dateValue property is written to. So I would believe think that what you want to accomplish is default behaviour.
Perhaps the CPCalendar is not added as subView to a CPWindow when you set the date and therefor the view isn't updated at time of setting the value?

Related

Stop UIPickerViewAnimation

Is there a way to instantly stop the animation of rolling UIPIckerView or UIDatePicker on button click.
Look into - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
Since Picker can only show limited number of results at a time. You can implement an algorithm to determine what is in the middle of the result when this method is called. It gets called every time a new option appears (just like tableView viewForIndexPath). So scrolling up or down will call this method.
Beware, you would need to add padding options to first few choices and last few choices just so that it works correctly. This will get you the effect you want. Be warned, Apple might not agree with this method as it doesnt act like what Apple intended picker to do.
Let me know if it works.
If you look at the UIPickerView class reference, you'll see:
selectRow:inComponent:animated:
Selects a row in a specified component
of the picker view.
- (void)selectRow:(NSInteger)row
inComponent:(NSInteger)component
animated:(BOOL)animated
You could call this method when your button is pushed, sending NO for the animated parameter, to instantly select a particular row.

UIPicker and Text and URL

I'm trying to build an app that will display stats for a certain game (I'd rather not say which one). I have most of the app completed, but I have run into a problem with my "Players" page, which is the primary view that loads after viewDidLoad. I've attached a screenshot of how it looks.
(DARN IT. I'm too new to post a pic. Available on request.)
It took some work, but I finally got my UIPickerView into place, and customized the initial options for it. You can't tell from the screenshot, but the picker loads with my player name, and one other. This will change before release.
My problem is that I want users to be able to enter their gamer name(s) in a text field, and have them always selectable from the picker after that. When they select a name in the picker, I need that action to send a string, a URL, with the player name appended to it, to every other view so that all navigation subsequent to choosing a player will show that player's data. This will allow me to load the rest of my server side graphics with ease.
Also, player names must be removable from the picker.
I'm not so lazy as to simply ask for the code, but I can't seem to figure out what components to use, or even find a tutorial that speaks to what I'm doing precisely.
OK what i understand is you want to do two operations on the picker value select.
send the url with the player name.
delete the player name..(if user wants).
Now for this you can add two buttons and as you know the value that are shown in the picker is basically you can store them in am array and load it again on picker.
so now you added two buttons one is for sending data and other is for delete.
on sendData button you can do like this to select the value from picker
NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView; //this your picker, if you defined it already dont do it.
row = [repeatPickerView selectedRowInComponent:0];
self.strPrintRepeat = [repeatPickerData objectAtIndex:row];
now in the "strPrintRepeat" you have the selected value. you can do what ever you want with it.
and on deleting the player name..you can do something like this.
NSInteger row;
NSMutableArray *repeatPickerData; //this should be mutable
UIPickerView *repeatPickerView; //this your picker, if you defined it already dont do it.
row = [repeatPickerView selectedRowInComponent:0];
[repeatePickerData removeObjectAtIndex:row];
now at this point you have the player name removed.
Then you can use your UiPicker delegates to reload the picker with the updated data..
Hope that works..:) enjoy

Can I put a NSDatePicker object in a cell of NSTableView?

Right now I am just using a NSTextCell with a NSDateFormatter, I just wanted to know if this was possible, perhaps by using the .
Thanks in advance!
not without making a bunch of hacking... Apple doesn't provide the date picker cell for the table for some reason. I would keep the date formatter or make it so when the cell is clicked, it pops open the graphical date picker.
I created a menu in the xib file, set the outlet for the cell's menu property to the menu, created a subclass of nsdatepicker, overrode the mouseDown:, and made it display the menu when clicked.

Changing tooltips in a custom view

I've got a sample project at:
http://ericgorr.net/cocoadev/tooltip.zip
What I would like to do is define a single tooltip rect for an entire view but be able to change the tooltip as the cursor moves inside of the view.
Is there a way to do that? Is there a way to force it to hide the current tooltip and display a new one while calling view:stringForToolTip:point:userData:?
I could create my own window that simulates a real tooltip, but wanted to make sure there was nothing built-in that would support this.
Check the MAAttachedWindow sample project:
http://mattgemmell.com/source/
Great start for creating custom tooltips.
NSView has specific handlers for mouse events.
Simply change the NSView (tooltip) based on these events.
I added some snippets to get you started.
- (void)mouseMove:(NSEvent *)theEvent {
NSPoint mousePositionInWindow = [theEvent locationInWindow];
}
- (void)mouseDown:(NSEvent *)theEvent {
}
- (void)mouseDragged:(NSEvent *)theEvent {
}
- (void)mouseUp:(NSEvent *)theEvent {
}
Response to comment:
Once I struggled with exactly the same problem: One view with continuous tooltip updates showing the cursor position and some additional information. I got it never working with the native tooltips. Finally i came up with the solution above, which is easy to implement and made it even look better.
Instead of using the separate window, you can also draw the custom tooltip inside the NSView itself, in relation to the cursor position. You can also put an extra NSView on top of the existing NSView to show the custom tooltips.
I don't like the native tooltip behavior. Apparently they have build-in time-delays which cannot be changed, for example: The cursor has to be on one position for some time to show the tooltip for the first time. Once the first tooltip showed up, the next will show with much less delay, but it's still quite annoying.
Of cours, you can always show the info in a label located near the view, which is really easy to implement. But that is no real answer to your question :)

Given an IBOutlet to an NSTextField that is bound to some NSDate, how can I programmatically set a new date?

I have a nice chunk of reuable code I'm trying to edit and keep clean of the details. I'd like to be able to edit the NSDate some NSTextField is bound to. The only thing I have is the IBOutlet to the text field. Is this possible?
I suppose if you knew the date format that the textField is using you could:
NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];
[myformatter setDateFormat:textFieldDateFormat];
textField.text = [myformatter stringFromDate:yourdate];
If you don't know the format then you could try parsing the testField.text to figure it out, but I that's way past my pay grade.
I assume you tried directly setting the object value of the text field.
If that didn't work, use infoForBinding: to find out what property of what object it's bound to, then use setValue:forKeyPath: to set the value at that property of that object.
Setting the objectValue for the NSTextField by itself does not get down to the model.
I'll try to explain what I'm doing. In my app I started with a simple textfield that has an NSDateFormatter attached to it. It was bound to an NSDate and all was well. Later I wanted to add a popup calendar next to the text field so a user could click their date.
http://media.clickablebliss.com/billable/interface_experiments/new_date_picker.png
Now rather than hard coding this behavior I tried to be generic with the solution. I ended up writing a custom helper object. I would connect the helper to the textfield and the button on screen and then set the action of the button to openPopup: in the helper. When the user popped up a calendar view my code would bind the calendar view to the same object that the textfield was bound to with the following code:
// Set the binding of the date picker in the panel to the same binding as the controlToBeHelped
NSDictionary *controlToBeHelpedBindings = [controlToBeHelped infoForBinding:#"value"];
[popupPanelDatePicker bind:#"value"
toObject:[controlToBeHelpedBindings valueForKey:NSObservedObjectKey]
withKeyPath:[controlToBeHelpedBindings valueForKey:NSObservedKeyPathKey]
options:nil];
This works great in my one project. In my new project I'd like to reuse this code with a twist. This time the NSDate can be nil. I'd like to agument my code so that if the user clicks on the button to pick a date when the date is currently nil I first set it to now and then show the popup.
Thus, I need to edit the NSDate this control is bound to via an IBOutlet. Thanks.
EDIT: Thanks to Peter I think I got this working:
if ([[controlToBeHelpedBindings valueForKey:NSObservedObjectKey] valueForKeyPath:[controlToBeHelpedBindings valueForKey:NSObservedKeyPathKey]] == nil) {
[[controlToBeHelpedBindings valueForKey:NSObservedObjectKey] setValue:[NSDate date] forKeyPath:[controlToBeHelpedBindings valueForKey:NSObservedKeyPathKey]];
}

Resources