Set NSPopUpButton at first launch - objective-c

I have a NSPopUpButton but at first time launch this does not set correctly the first value. I have set awakeFromNib but the NSPopUpMenu is empty. Only the second time and the next it works correctly.
Thanks in advance.
-(IBAction)chancepesoalert:(id)sender{
int selection = [(NSPopUpButton *)sender indexOfSelectedItem];
NSNumber *valore = [NSNumber numberWithUnsignedLongLong:(30*1000*1000)];
if (selection == 0) {
valore = [NSNumber numberWithUnsignedLongLong:(30*1000*1000)];
NSLog(#"Selezionato 0");
}
if (selection == 1){
valore = [NSNumber numberWithUnsignedLongLong:(50*1000*1000)];
NSLog(#"Selezionato 1");
}
if (selection == 2){
valore = [NSNumber numberWithUnsignedLongLong:(75*1000*1000)];
NSLog(#"Selezionato 2");
}
if (selection == 3){
valore = [NSNumber numberWithUnsignedLongLong:(100*1000*1000)];
NSLog(#"Selezionato 3");
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:valore forKey:#"SetPesoAlert"];
[userDefaults synchronize];
}
-(void)awakeFromNib {
unsigned long long value = [[[NSUserDefaults standardUserDefaults] objectForKey:#"SetPesoAlert"] unsignedLongValue];
int index;
if (value == (30*1000*1000)) {
index =0;
}
if(value == (50*1000*1000)) {
index =1;
}
if(value == (75*1000*1000)) {
index =2;
}
if(value == (100*1000*1000)) {
index =3;
}
[pesoalert selectItemAtIndex:index];
}

I sounds like you need to use registerDefaults (you might not need to do this however, since the operating system will pick default values and 0 for an index is what it will pick I think). This allows you to set up default values for the first time an app is run, but if the user changes a default, that new default will be used the next time the app is run (but you need to read those defaults at start up -- I don't see any defaults reading in the code you posted).
There is however, an even easier way to do this using bindings. When I do popups, I use an array to supply the values to the popup menu. In IB, I delete the menu items that you get by default, and then bind the popup's content binding to, for instance, App Delegate.data (data is the name of my array). Then I bind the Selected Index to the Shared User Defaults Controller with a Model Key Path of whatever (it doesn't matter what you call it, this is a name that the controller uses, it's not a property in your code). When you start the app for the first time it defaults to index=0, so you will get whatever is the first item on your list, and any changes the user makes will be remembered on the next startup.

Related

Extend selection in NSCollectionView with Shift key

I recently reviewed one of my Applications that I released a year ago.
And I see that nowadays the NSCollectionView inside it has lost the selection functioning such as SHIFT + Select now it behaving as CMD + Select.
(Secondary issue: I am also not getting a selection rectangle when dragging with the mouse.)
Obviously I want this feature back, where using shift would expand the selection from the previously clicked cell to the shift-clicked cell.
What have I done:
//NSCollectionView * _picturesGridView; //is my iVar
//In initialization I have set my _picturesGridView as follows
//Initializations etc are omitted -- (only the selection related code is here)
[_picturesGridView setSelectable:YES];
[_picturesGridView setAllowsMultipleSelection:YES];
Question: Is there an easy way to get back this functionality? I don't see anything related in documentation and I couldn't find any solution on the internet.
Sub Question: If there is no easy way to achieve that -> Should I go ahead and create my own FancyPrefix##CollectionViewClass and to reimplement this feature as I wish -- Or is it better to go over the existing NSCollectionView and force it to behave as I wish?
Sub Note: Well if I will find myself reimplementing it it will be light weight class that will just comply to my own needs -- I mean I will not mimic the entire NSCollectionView class.
P.S.
I am able to select an item by clicking on it I am able to select multiple items only with CMD+Click or SHIFT+Click but the latter behaves exactly as CMD+Click which I don't want as well.
As for the mouse Selection Rectangle - I didn't override any Mouse events. It is not clear why I don't have this functionality.
Per Apple's documentation, shouldSelectItemsAtIndexPaths: is the event that only gets invoked when the user interacts with the view, but not when the selection is modified by one's own code. Therefore, this approach avoids side effects that may occur when using didSelectItemsAt:.
It works as follows:
It needs to remember the previously clicked-on item. This code assumes that the NSCollectionView is actually a subclass called CustomCollectionView that has a property #property (strong) NSIndexPath * _Nullable lastClickedIndexPath;
It only remembers the last clicked-on item when it was a simple selection click, but not, for instance, a drag-selection with more than one selected item.
If it detects a second single-selection click with the Shift key down, it selects all the items in the range from the last click to the current click.
If an item gets deselected, we clear the last clicked-on item in order to simulate better the intended behavior.
- (NSSet<NSIndexPath *> *)collectionView:(CustomCollectionView *)collectionView shouldDeselectItemsAtIndexPaths:(NSSet<NSIndexPath*> *)indexPaths
{
collectionView.lastClickedIndexPath = nil;
return indexPaths;
}
- (NSSet<NSIndexPath *> *)collectionView:(CustomCollectionView *)collectionView shouldSelectItemsAtIndexPaths:(NSSet<NSIndexPath*> *)indexPaths
{
if (indexPaths.count != 1) {
// If it's not a single cell selection, then we also don't want to remember the last click position
collectionView.lastClickedIndexPath = nil;
return indexPaths;
}
NSIndexPath *clickedIndexPath = indexPaths.anyObject; // now there is only one selected item
NSIndexPath *prevIndexPath = collectionView.lastClickedIndexPath;
collectionView.lastClickedIndexPath = clickedIndexPath; // remember last click
BOOL shiftKeyDown = (NSEvent.modifierFlags & NSEventModifierFlagShift) != 0;
if (NOT shiftKeyDown || prevIndexPath == nil) {
// not an extension click
return indexPaths;
}
NSMutableSet<NSIndexPath*> *newIndexPaths = [NSMutableSet set];
NSInteger startIndex = [prevIndexPath indexAtPosition:1];
NSInteger endIndex = [clickedIndexPath indexAtPosition:1];
if (startIndex > endIndex) {
// swap start and end position so that we can always count upwards
NSInteger tmp = endIndex;
endIndex = startIndex;
startIndex = tmp;
}
for (NSInteger index = startIndex; index <= endIndex; ++index) {
NSUInteger path[2] = {0, index};
[newIndexPaths addObject:[NSIndexPath indexPathWithIndexes:path length:2]];
}
return newIndexPaths;
}
This answer has been made "Community wiki" so that anyone may improve this code. Please do so if you find a bug or can make it behave better.
I hacked together a sample of doing this, starting with the sample code git#github.com:appcoda/PhotosApp.git, all my changes are in ViewController.swift.
I kept track of whether the shift was up or down by adding
var shiftDown = false
override func flagsChanged(with event: NSEvent) {
shiftDown = ((event.modifierFlags.rawValue >> 17) & 1) != 0
}
I added this instance variable to remember the last selection
var lastIdx: IndexPath.Element?
Then, to the already defined collectionView(_:didSelectItemsAt), I added
let thisIdx = indexPaths.first?[1]
if shiftDown, let thisIdx = thisIdx, let lastIdx = lastIdx {
let minItem = min(thisIdx, lastIdx)
let maxItem = max(thisIdx, lastIdx)
var additionalPaths = Set<IndexPath>()
for i in minItem...maxItem {
additionalPaths.insert(IndexPath(item: i, section: 0))
}
collectionView.selectItems(at: additionalPaths, scrollPosition: [])
}
lastIdx = thisIdx
This is just a start. There's probably bugs, especially around the saving of the lastIdx.
None of the answers appear to support sections or shift selecting across section boundaries. Here is my approach:
- (NSSet<NSIndexPath *> *)collectionView:(NSCollectionView *)collectionView shouldSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths
{
if ([shotCollectionView shiftIsDown])
{
// find the earliest and latest index path and make a set containing all inclusive indices
NSMutableSet* inclusiveSet = [NSMutableSet new];
__block NSIndexPath* earliestSelection = [NSIndexPath indexPathForItem:NSUIntegerMax inSection:NSUIntegerMax];
__block NSIndexPath* latestSelection = [NSIndexPath indexPathForItem:0 inSection:0];
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, BOOL * _Nonnull stop) {
NSComparisonResult earlyCompare = [obj compare:earliestSelection];
NSComparisonResult latestCompare = [obj compare:latestSelection];
if (earlyCompare == NSOrderedAscending)
{
earliestSelection = obj;
}
if (latestCompare == NSOrderedDescending)
{
latestSelection = obj;
}
}];
NSSet<NSIndexPath *>* currentSelectionPaths = [shotCollectionView selectionIndexPaths];
[currentSelectionPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, BOOL * _Nonnull stop) {
NSComparisonResult earlyCompare = [obj compare:earliestSelection];
NSComparisonResult latestCompare = [obj compare:latestSelection];
if (earlyCompare == NSOrderedAscending)
{
earliestSelection = obj;
}
if (latestCompare == NSOrderedDescending)
{
latestSelection = obj;
}
}];
NSUInteger earliestSection = [earliestSelection section];
NSUInteger earliestItem = [earliestSelection item];
NSUInteger latestSection = [latestSelection section];
NSUInteger latestItem = [latestSelection item];
for (NSUInteger section = earliestSection; section <= latestSection; section++)
{
NSUInteger sectionMin = (section == earliestSection) ? earliestItem : 0;
NSUInteger sectionMax = (section == latestSection) ? latestItem : [self collectionView:collectionView numberOfItemsInSection:section];
for (NSUInteger item = sectionMin; item <= sectionMax; item++)
{
NSIndexPath* path = [NSIndexPath indexPathForItem:item inSection:section];
[inclusiveSet addObject:path];
}
}
return inclusiveSet;
}
// Otherwise just pass through
return indexPaths;
}

Change the displayed unit instantly after user changes it using UISegmentedControl

I have a UISegmentedControl that handles the setting of the temperature unit (ºC/ºF).
It works correctly but if the user changes the setting, the displayed unit won't change (refresh itself) unless the user navigates to another View Controller and then back to the one in question.
I'm using
- (void)viewWillAppear:(BOOL)animated
{
//Gets user setting choice for Fahrenheit or Celsius
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
temperatureSetting = [[preferences valueForKey:#"temperature setting"]intValue];
//For getting the current value of temperature from the JSON data
NSInteger temp = [[conditions objectForKey:#"temperature"]intValue];
//For checking the user's setting to display either Fahrenheit or Celsius
if (temperatureSetting == 0) {
temp = (temp * 1.8) + 32;
temperatureLabel.text = [NSString stringWithFormat:#"%i°F", temp];
} else {
temperatureLabel.text = [NSString stringWithFormat: #"%iºC", temp];
}
}
- (IBAction)temperatureSelection:(UISegmentedControl *)sender {
//Used to save settings
NSUserDefaults *temperaturePreference = [NSUserDefaults standardUserDefaults];
//To check which segment is selected and save the value but also remember it
if ([sender selectedSegmentIndex] == 0) {
[temperaturePreference setInteger:0 forKey:#"temperature setting"];
[selectedSegment setInteger:0 forKey:#"segment setting"];
} else if ([sender selectedSegmentIndex] == 1) {
[temperaturePreference setInteger:1 forKey:#"temperature setting"];
[selectedSegment setInteger:1 forKey:#"segment setting"];
}
//To make sure the setting is saved
[temperaturePreference synchronize];
[selectedSegment synchronize];
}
How can I make it change the displayed unit instantly after the user changes it?
Thank You!
Move the code inside viewWillAppear: to a separate method. Call that method when the user changes the settings.
Try to add that line of code before the end of - (IBAction)temperatureSelection:(UISegmentedControl *)sender method.
- (IBAction)temperatureSelection:(UISegmentedControl *)sender{
//..etc
temperatureSetting = [[temperaturePreferences valueForKey:#"temperature setting"]intValue];
if (temperatureSetting == 0) {
temp = (temp * 1.8) + 32;
temperatureLabel.text = [NSString stringWithFormat:#"%i°F", temp];
} else {
temperatureLabel.text = [NSString stringWithFormat: #"%iºC", temp];
}
[self.view setNeedsDisplayInRect:temperatureLabel.frame];//probably is not needed it forces the view to redraw itself as it mark as dirty
}

Dynamically created textfield validation

I'm trying to validate dynamically created text fields. The total number of textfields may vary.
The idea is to populate the empty fields with string like player 1, player 2 etc.. Here is what I try
-(IBAction)validateTextFields:sender
{
self.howManyPlayers = 3;
int emptyFieldCounter = 1;
NSMutableArray *playersNames = [NSMutableArray arrayWithCapacity:self.howManyPlayers];
while (self.howManyPlayers > 1)
{
self.howManyPlayers--;
UITextField *tmp = (UITextField *) [self.view viewWithTag:self.howManyPlayers];
if (tmp.text == nil)
{
[tmp setText:[NSString stringWithFormat:#"Player %d", emptyFieldCounter]];
emptyFieldCounter++;
}
[playersNames addObject:tmp.text];
}
}
The problems is that if I touch the button which invoke validateTextFields method. The first and the second textfield are populated with text Player 1 and Player 2, but the third field is not populated.
I notice also that if I type a text let's say in the second field touch the button then remove the text and again touch the button that field is not populated with text Player X.
How to make all that things to work correctly ?
change your code for two lines like this:
while (self.howManyPlayers >= 1) //edited line
{
UITextField *tmp = (UITextField *) [self.view viewWithTag:self.howManyPlayers];
if (tmp.text == nil)
{
[tmp setText:[NSString stringWithFormat:#"Player %d", emptyFieldCounter]];
emptyFieldCounter++;
}
[playersNames addObject:tmp.text];
self.howManyPlayers--; // moved line
}
I forgot ur second question, so edited my answer.
For that try with this. Change if (tmp.text == nil) with if (tmp.text == nil || [tmp.txt isEqualToString:#""])
The reason only two fields are populated is that you are only going through the while loop twice. It should be
while (self.howManyPlayers >= 1)
You should also move the decrement to the end of your while loop
while (self.howManyPlayers >= 1)
{
// other code here
self.howManyPlayers--;
}
For the second part of your question, I think when you delete the text from the control, it stops being nil and now becomes an empty string. So you need to check for an empty string as well as nil in your code.
if (tmp.text == nil || [tmp.txt isEqualToString:#""])

Set NSButtonCell Checkbox value in NSTableView

I have NSTableView with two columns,one with Checkbox and another with NSString.
i want to define all the Checkbox to true
i try to do it with:
if (tableColumn == first) {
return YES;
}else if (tableColumn == second) {
NSString *country = [array objectAtIndex:row];
return [country lastPathComponent];
}
but it give me crash when i start the app.
It looks like your code is executing in tableView:objectValueForTableColumn:row:. In that case, the table view only accepts objects, not primitive values.
Try changing return YES to return [NSNumber numberWithBool:YES].

NSTokenField does not check token on blur

I have an NSTokenField which allows the user to select contacts (Just like in Mail.app). So the NSTextField is bound to an array in my model.recipient instance variable.
The user can now select an entry from the auto completion list e.g. Joe Bloggs: joe#blogs.com and as soon as he hits Enter the token (Joe Bloggs) is displayed and model.recipients now contains a BBContact entry.
Now if the user starts to type some keys (so the suggestions are shown) and then hits Tab instead of Enter the token with the value of the completion text (Joe Bloggs: joe#bloggs.com) is created and the NSTokenFieldDelegate methods did not get called, so that I could respond to this event. The model.recipient entry now contains an NSString instead of a BBContact entry.
Curiously the delegate method tokenField:shouldAddObjects:atIndex: does not get called, which is what I would expect when the user tabs out of the token field.
Pressing tab calls isValidObject on the delegate so return NO for NSTokenField in it however you want to return YES if there are no alphanumeric characters in it otherwise the user won't be able to focus away from the field (the string contains invisible unicode characters based on how many tokens exist)
The less fragile implementation i could come up with is:
- (BOOL)control:(NSControl *)control isValidObject:(id)token
{
if ([control isKindOfClass:[NSTokenField class]] && [token isKindOfClass:[NSString class]])
{
if ([token rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]].location == NSNotFound) return YES;
return NO;
}
return YES;
}
I was able to solve the problem using #valexa's suggestions. In case of a blur with TAB I have to go through all entries and look up my contact-objects for any strings.
- (BOOL)control:(NSControl *)control isValidObject:(id)token{
if ([control isKindOfClass:[NSTokenField class]] && [token isKindOfClass:[NSString class]])
{
NSTokenField *tf = (NSTokenField *)control;
if ([token rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]].location == NSNotFound){
return YES;
} else {
// We get here if the user Tabs away with an entry "pre-selected"
NSMutableArray *set = #[].mutableCopy;
for(NSObject *entry in tf.objectValue){
GSContact *c;
if([entry isKindOfClass:GSContact.class]){
c = (GSContact *)entry;
}
if([entry isKindOfClass:NSString.class]){
NSString *number = [[(NSString *)entry stringByReplacingOccurrencesOfString:#">" withString:#""]
componentsSeparatedByString:#"<"][1];
c = [self findContactByNumber:number];
}
if(c) [set addObject:c];
}
[control setObjectValue:set];
}
}
return YES;
}
This could be because the "enter" key might send the event of the token field to it's action where the "tab" key just adds text to it. You could try to set the -isContinuous property to YES and see if you get the desired results.