button available only when label is not nil - uitextfield

How can I force the user to insert data to the UITEXTFILED that otherwise the button will not work for him.
I thought about:
-(IBAction) signup:(id)sender{
if text.text==nil
///// return?? /////
?
tnx all

I would do like this, first in my ViewDidLoad():
editButton.enabled=NO;
[_myTextFieldaddTarget:self
action:#selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
Then i would add an event handler:
-(void) textFieldDidChange{
if (_myTextField.length == 0){
editbutton.enabled=NO;
}else{
editbutton.enabled=YES;
}
}

You can show an UIAlertView when the user push the button if the UITextField is empty :
if (_myTextField.lenght == 0)
// Display Alert

Related

Present a sheet from a menu item

I have a Cocoa app that calls a sheet from the main window. This works fine from an NSButton on the main window. However, when I call from a MainMenu item, the sheet shows as a separate window. Is this expected behavior, or just expected from me :) I have studied this question ..
I call the sheet with this code:
-(IBAction) showSettingsSheet:(id)sender {
NSLog(#"%s", __FUNCTION__);
settingsSheetController = [[SettingsSheetController alloc] initWithWindowNibName:#"SettingsSheet"];
[settingsSheetController beginSheetModalForWindow:self.window completionHandler:^(NSUInteger returnCode) {
if (returnCode == kSettingsSheetReturnedOk) {
NSLog(#"Settings Returned ok");
} else if (returnCode == kSettingsSheetReturnedCancel) {
NSLog(#"Settings Returned cancel");
} else {
//self.categoryDisplayString = #"Oops!";
NSLog(#" Ooops");
}
}];
}
UPDATE
This is how the Attributes Inspector is set:
Try like this, if you want to display sheet in a mainmenu then try the below steps:-
Uncheck the visible at launch option inside attribute inspector of window which you want to display as a sheet attached the screenshot as well

How to change the behavior of cancelbutton in the NSSearchField?

In Finder , i find that the cancel button disappear when the NSSearchfield get focus first time,but when i input something to the searchfield,the cancel button is there all the time unless i click it.I also create a nssearchfield manully and input some words.However,when I delete the word one by one until nothing,at last,the cancel button disappear.It looks like the default behavior of the nssearchfield,so how can i change the default behavior?
please help,thank you!
You can access the cancel button directly:
Swift
let searchField = NSSearchField()
// Change the button:
if let cell = searchField.cell as? NSSearchFieldCell,
let cancelButton = cell.cancelButtonCell {
// Here you can access all properties of a NSButtonCell
}
// Hide the button:
searchField.cell?.cancelButtonCell = nil
Objective C
NSSearchField* searchField = [NSSearchField new]
// Change the button:
NSSearchFieldCell* cell = searchField.cell
NSButtonCell* cancelButton = cell.cancelButtonCell
if (cancelButton) {
// Here you can access all properties of a NSButtonCell
}
// Hide the button:
searchField.cell.cancelButtonCell = nil

NSDatePicker action method not firing

Scenario:
The user is entering a date in an NSDatePicker in its textual form with no stepper (on OS X), and when they hit return on the keyboard, I'd like a message to be sent to the controller.
In an NSTextField, I would just hook the action up in Interface Builder, or set it from code, and when the user hits enter, the action message is sent to the target.
The date picker allows to set an action message and a target, but I can't get the action to fire. When I hit enter in the date picker, the action message does not get called.
Am I doing something wrong, or is there a workaround that I have to use? I would not be adverse to subclassing any of the classes involved, if that is what it takes.
An NSDatePicker will not handle or forward key events triggered by the Return or Enter key.
The solution is to subclass NSDatePicker to get the desired behavior in keyDown:
#import "datePickerClass.h"
#implementation datePickerClass
- (void)keyDown:(NSEvent *)theEvent{
unsigned short n = [theEvent keyCode];
if (n == 36 || n == 76) {
NSLog(#"Return key or Enter key");
// do your action
//
} else {
[super keyDown:theEvent];// normal behavior
}
}
#end
That's it.
Edit : also you can use NSCarriageReturnCharacter and NSEnterCharacter
NSString* const s = [theEvent charactersIgnoringModifiers];
unichar const key = [s characterAtIndex:0];
if (key == NSCarriageReturnCharacter || key == NSEnterCharacter) {

Binding selected radiobox to enabling checkbox in Cocoa

I haven't worked with Cocoa bindings a lot before, so I would need a little help here.
I have a radio button group ( NSMatrix ) with three buttons and one checkbox.
I want the checkbox to be enabled only when the last radio button is selected.
Found a tutorial online which advised to bind the selected tag property of the radio button group to the enabled property of the checkbox. The last radiobutton needs to have a tag of 1, the others would need to have a tag of 0.
This works great.
The problem is, that if the checkbox is checked and the radiobutton selection is changed, it stays checked although it isn't enabled. I would want that the box gets unchecked when it changes to the disabled state.
Any advise would be appreciated!
Thanks in advance!
Any way to achieve this without any code?
I doubt it's possible to do this without code.
I handle this in the model, using KVO. Code looks something like this:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([#"checkWithInProcess" isEqualToString:keyPath]) {
NSNumber *oldValue = [change objectForKey:NSKeyValueChangeOldKey];
NSNumber *newValue = [change objectForKey:NSKeyValueChangeNewKey];
BOOL oldValueAsBool = (oldValue != (id)[NSNull null]) && oldValue.boolValue;
BOOL newValueAsBool = (newValue != (id)[NSNull null]) && newValue.boolValue;
if (oldValueAsBool && !newValueAsBool) {
// Save the value
savedRecordValueWithInProcess = self.recordValueWithInProcess;
self.recordValueWithInProcess = nil;
} else if (!oldValueAsBool && newValueAsBool) {
// Restore the value or set it to the default
if (savedRecordValueWithInProcess)
self.recordValueWithInProcess = savedRecordValueWithInProcess;
else
self.recordValueWithInProcess = [NSNumber numberWithBool:NO];
savedRecordValueWithInProcess = nil;
}
}
}
And during initialization:
[self addObserver:self
forKeyPath:#"checkWithInProcess"
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
context:[Characteristic class]];

How to hide a divider of nssplitview?

Now I want to hide or show with my condition a divider when my app run. used this delegate method:
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if (A)
return YES;
else
return NO;
}
but it didn't work , why? How to used this method? Thank you very much!
Further to #carmin’s note, above, overriding the NSSplitView dividerThickness property is the only thing that worked for me (specifically, returning NSRectZero from the splitView:effectiveRect:forDrawnRect:ofDividerAtIndex: NSSplitView delegate method — as detailed here – didn’t work and resulted in floating dividers disjointed from the views themselves).
Here’s the code in Swift:
override var dividerThickness:CGFloat
{
get { return 0.0 }
}
The split view sends that message to its delegate, to ask the delegate whether it should hide that divider. So, be the delegate, and answer the split view's question.
Be sure to check out the documentation. It's possible that that message won't accomplish what you want it to. The documentation lists everything you can do by responding to that message.
You can overload NSSplitView-dividerThickness and return 0 to hide all of the dividers. You can overload NSSplitView-drawDividerInRect: to have individual control over the dividers (choosing to allow super to draw the divider or not). These choices work even when the subviews are visible.
Here's how to do it in Obj-C that doesn't involve subclassing. Make sure that you've got the SplitView delegate in IB connected.
Then in your delegate class:
-(NSRect)splitView:(NSSplitView *)splitView effectiveRect:(NSRect)proposedEffectiveRect forDrawnRect:(NSRect)drawnRect ofDividerAtIndex:(NSInteger)dividerIndex
{
if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50) //closed or almost closed
{
return NSZeroRect;
}
return proposedEffectiveRect;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50)
{
return YES;
}
return NO;
}
This will hide the divider when the split view is closed, but show it when it is open.
If you don't want them to be able to drag it even when its open, just cut out all the code in the first method and return only NSZeroRect. Do the same in the second method and only return YES.
For the sake of posterity, working with Swift you can call the delegate function splitView(_:effectiveRect:forDrawnRect:ofDividerAtIndex:) and just have it return an empty NSRect
override func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {
if dividerIndex == 1 {
return NSRect()
}
return super.splitView(splitView, effectiveRect: proposedEffectiveRect, forDrawnRect: drawnRect, ofDividerAt: dividerIndex)
}
Use this class in Custom class of NSSplitView:
class customSplitView: NSSplitView {
override var dividerThickness: CGFloat {
return 0
}
}
For me it worked!

Resources