Prevent large offset when warping mouse cursor
I implemented the following custom view derived from NSView: #import WarpCursorView.h @interface WarpCursorView () - (CGPoint)centerOnScreen; @end @implementation WarpCursorView - (void)drawRect:(NSRect)dirtyRect { } - (void)mouseMoved:(NSEvent*)event { NSWindow* window = self.window; if([window isKeyWindow]){ ::NSLog(@Cursor moved with offset %f,%f, event.deltaX, event.deltaY); } } - (BOOL)acceptsFirstResponder { return YES; } - (BOOL)becomeFirstResponder { [NSCursor hide]; CGPoint center = [self centerOnScreen]; ::CGWarpMouseCursorPosition(center); ::CGAssociateMouseAndMouseCursorPosition(false); [self.window setAcceptsMouseMovedEvents:YES]; return YES; } - (BOOL)resignFirstResponder { ::CGAssociateMouseAndMouseCursorPosition(true); [NSCursor unhide]; return YES; } - (CGPoint)centerOnScreen { NSRect bounds = [self bounds]; NSPoint center = {bounds.size.width * 0.5f, bounds.size.height * 0.5f}; center = [self convertPoint:center toView:nil]; center = [[self window] convertBaseToScreen:center]; CGPoint result = {center.x, center.y}; return result; } @end The goal of this view is to place the mouse cursor in the view so the view will receieve all button input from the mouse, and to track the relative motion of the mouse as it moves. I placed this view in the Xib for an application, and built the application. I then placed the mouse cursor to the far right of the screen, launched the application and began moving the mouse cursor to the left. The program produced the following output: 2010-07-05 08:53:44.274 WarpCursor[12212:a0f] Cursor moved with offset -978.00,-9.00 2010-07-05 08:53:44.291 WarpCursor[12212:a0f] Cursor moved with offset -8.00,0.00 2010-07-05 08:53:44.307 WarpCursor[12212:a0f] Cursor moved with offset -8.00,-1.00 2010-07-05 08:53:44.324 WarpCursor[12212:a0f] Cursor moved with offset -11.00,-1.00 2010-07-05 08:53:44.341 WarpCursor[12212:a0f] Cursor moved with offset -5.00,0.00 2010 As can be seen, the first logged output contains a very large offset. This is not desired because the expectation is that the program should start tracking the mouse movement after the cursors has been placed inside the view. What can done to eliminate an initial large offset like this which can result if the mouse cursor is rather far away from where the view will be located when the application launches? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to eliminate field editor from NSTextField?
I finally figured out how to solve the problem. The problem was when trying to cancel and edit via the cancelOperation method that gets calls. The code as calling the abortEditing method on the NSTextField to cancel the text editing, and it also needed to call setEditable with a value of NO on the NSTextField to eliminate the field editor. On Mar 23, 2010, at 17:10 o'clock, Kyle Sluder wrote: On Tue, Mar 23, 2010 at 4:52 PM, Tron Thomas tron.tho...@verizon.net wrote: I tried that, and it did not work. The field editor remains sitting on top of the NSTextField. As I said, there are reasons views might refuse to resign first responder (in which case -makeFirstResponder: returns NO). Have you checked the conditions I've mentioned? --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
How to eliminate field editor from NSTextField?
I have an NSTextField control that will display a context menu when someone right clicks on it. The context menu contains an item that can allow for editing of the control's text. The expectation is that once the edit if complete the control will revert to its default behavior in terms of displaying its own context menu. However, this desired behavior after the edit has completed is not achieved because, when editing of the control begins, a field editor is assigned to the control and the editing takes place in that field editor. Once editing is complete, the field editor remains sitting on top of the control and any right clicks from the mouse are now set to the field editor which displays its own context menu for editing text rather than the context menu for the NSTextField. Methods related to changing the first responder for the window are ineffective because they do not change the location of the field editor. What can be done to eliminate the field editor when text editing has completed so the NSTextField can revert to its normal behavior until further editing is required? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to eliminate field editor from NSTextField?
I tried that, and it did not work. The field editor remains sitting on top of the NSTextField. On Mar 23, 2010, at 16:39 o'clock, Kyle Sluder wrote: On Tue, Mar 23, 2010 at 4:17 PM, Tron Thomas tron.tho...@verizon.net wrote: However, this desired behavior after the edit has completed is not achieved because, when editing of the control begins, a field editor is assigned to the control and the editing takes place in that field editor. Once editing is complete, the field editor remains sitting on top of the control and any right clicks from the mouse are now set to the field editor which displays its own context menu for editing text rather than the context menu for the NSTextField. That's because you never end editing. Methods related to changing the first responder for the window are ineffective because they do not change the location of the field editor. You end editing by sending the window -makeFirstResponder: with itself as an argument. For example: [theWindow setFirstResponder:theWindow]. This might fail if the field editor's value is invalid (for example, you have bound the text field to a key-value validated property, or you have attached a formatter to the field). --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Compile errors with Objective-C++ properties as STL/TR1 smart pointers
I'm running into a issue using STL/TR1 smart pointers with Objective-C++ properties that I think is a bug, and I wanted to get some feedback before I submitted any bug report. The following code: #import Cocoa/Cocoa.h #import tr1/memory class Value { public: explicit Value(int amount) : m_amount(amount){} int GetAmount() const { return m_amount; } private: int m_amount; }; typedef std::tr1::shared_ptrValue ValuePtr; @interface ValueUser : NSObject { @private ValuePtr* _valuePtr; } - (id)initWithValue:(ValuePtr)valuePtr; @property ValuePtr value; @end @implementation ValueUser - (id)initWithValue:(ValuePtr)valuePtr { self = [super init]; _valuePtr = new ValuePtr(valuePtr); return self; } - (void)setValue:(ValuePtr)valuePtr { *_valuePtr = valuePtr; } - (ValuePtr)value { return *_valuePtr; } - (void)dealloc { delete _valuePtr; [super dealloc]; } @end int main() { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; ValuePtr valuePtr(new Value(42)); ValueUser* valueUser = [[ValueUser alloc] initWithValue:valuePtr]; ::NSLog(@The value's amount is %d, valueUser.value-GetAmount()); [valueUser release]; [pool release]; return 0; } will produce these errors on the line in the main function containing the NSLog call: error: lvalue required as unary ‘’ operand error: base operand of ‘-’ has non-pointer type ‘ValuePtr’ If I replace the line with: ::NSLog(@The value's amount is %d, [valueUser value]-GetAmount()); Everything compiles successfully. What is the reason the '.' syntax will not work in this instance? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Compile errors with Objective-C++ properties as STL/TR1 smart pointers
I tried (valueUser.value)-GetAmount()) and I still get the same errors. Date: Sun, 27 Dec 2009 11:47:35 +1100 From: Brian Bruinewoudbr...@darknova.com Subject: Re: Compile errors with Objective-C++ properties as STL/TR1 smart pointers To: undisclosed-recipients: ; Cc: cocoa-dev@lists.apple.com Message-ID:bff7a0e2-640d-4407-bcde-38fc06046...@darknova.com Content-Type: text/plain; charset=windows-1252 It's probably just a parsing thing, have you tried: ::NSLog(@The value's amount is %d, (valueUser.value)-GetAmount()); ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Manipluate Cocoa Touch keyboard mode
I found the setting in Interface Builder. This works in that the keyboard initially appears in numeric mode. However, when someone presses the return key, the keyboard switches to letter mode. How can someone prevent this behavior? Yandy Ramirez wrote: numbers and punctuation in IB textfield properties for keyboard type -- yandy On Oct 17, 2009, at 8:53 PM, Tron Thomas wrote: When an UITextField becomes active in a Cocoa Touch application, the device automatically displays a keyboard for character input. The keyboard always appears in alphabet mode so letters can be typed. I have an application containing a singular UITextField that is intended for entering numeric data. I would like the keyboard to come up in numeric mode and stay in that mode unless the user click the button to toggle to letter mode, even if they press the return key. How can someone accomplish this keyboard behavior? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/yandy%40me.com This email sent to ya...@me.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Selecting UITextField text
How can someone programmatically select all the text in an UITextField control? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Manipluate Cocoa Touch keyboard mode
When an UITextField becomes active in a Cocoa Touch application, the device automatically displays a keyboard for character input. The keyboard always appears in alphabet mode so letters can be typed. I have an application containing a singular UITextField that is intended for entering numeric data. I would like the keyboard to come up in numeric mode and stay in that mode unless the user click the button to toggle to letter mode, even if they press the return key. How can someone accomplish this keyboard behavior? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Issues with NSWindowDelegate on multiple Mac OS X platforms
I recently got a MacBook Pro running Mac OS X 10.6.1 and Xcode 3.2 When I tried to build a project I was working on, I would get an error like this: warning: class 'MyClass' does not implement the 'NSWindowDelegate' protocol I was able to eliminate the warning by doing this: @interface MyClass : NSResponder NSWindowDelegate However when I go to build the same project on my PowerMac G5 running Mac OS X 10.5.8 with Xcode 3.1.3, I now get this compilation error: error: cannot find protocol declaration for 'NSWindowDelegate' What is needed so the project will build with no errors or warnings on both platforms? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Profiling a drag and drop operation
I have a program whereby when I drag and drop a certain file onto the application's window, the application will take a few seconds before it displays the file contents. An older program that this newer program was based on and uses a lot of the same code, will display the same file data in about a second. What could allow me to determine why the new program is much slower when trying to deal with this dropped file? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSCFArray autoreleased with no pool in place
Greg Parker wrote: On May 14, 2009, at 9:58 PM, Tron Thomas wrote: That did the trick. The stack trace allowed me to track down the cause of the error. Because Objective-C++ does not have a concept of class static variables I had an NSArray defined in an anonymous namespace in the implementation module that was initializing its contents. I changed the code so the array is initially set to nil in the anonymous namespace, and then assign it to an NSArray instance the init method of the class using the array if the array had not been initialize. I'm guessing the code you just wrote is not thread-safe - if two threads are simultaneously the first to call -init then things might go wrong. If you need thread-safe initialization, the +initialize or +load methods are usually better. Currently the code is not multi-threaded, so there no concern at the moment for worrying about thread-safe initialization. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSCFArray autoreleased with no pool in place
Greg Parker wrote: On May 14, 2009, at 9:58 PM, Tron Thomas wrote: That did the trick. The stack trace allowed me to track down the cause of the error. Because Objective-C++ does not have a concept of class static variables I had an NSArray defined in an anonymous namespace in the implementation module that was initializing its contents. I changed the code so the array is initially set to nil in the anonymous namespace, and then assign it to an NSArray instance the init method of the class using the array if the array had not been initialize. I'm guessing the code you just wrote is not thread-safe - if two threads are simultaneously the first to call -init then things might go wrong. If you need thread-safe initialization, the +initialize or +load methods are usually better. I looked into trying to use +initialize or +load to set the values of the array, as I thought it might be a better approach than using the -init function. When I used +load, I get the same problem I had before the array is getting setting before the main function executes and creates an auto-release pool. When I use +initialize the program crashes when it tries to access the array later on in the program life cycle. It seems like the array becomes corrupt. For some reason the debugger thinks it is an NSCFNumber instance instead of an array. What might be going wrong that is preventing +initialize from successfully creating the array? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Cocoa-dev Digest, Vol 6, Issue 742
On Fri, May 15, 2009 at 11:00 PM, Tron Thomas tron.tho...@verizon.net wrote: Greg Parker wrote: On May 14, 2009, at 9:58 PM, Tron Thomas wrote: That did the trick. Â The stack trace allowed me to track down the cause of the error. Â Because Objective-C++ does not have a concept of class static variables I had an NSArray defined in an anonymous namespace in the implementation module that was initializing its contents. Â I changed the code so the array is initially set to nil in the anonymous namespace, and then assign it to an NSArray instance the init method of the class using the array if the array had not been initialize. I'm guessing the code you just wrote is not thread-safe - if two threads are simultaneously the first to call -init then things might go wrong. If you need thread-safe initialization, the +initialize or +load methods are usually better. I looked into trying to use +initialize or +load to set the values of the array, as I thought it might be a better approach than using the -init function. When I used +load, I get the same problem I had before the array is getting setting before the main function executes and creates an auto-release pool. When I use +initialize the program crashes when it tries to access the array later on in the program life cycle. Â It seems like the array becomes corrupt. Â For some reason the debugger thinks it is an NSCFNumber instance instead of an array. What might be going wrong that is preventing +initialize from successfully creating the array? Post your code. I could guess at what's going on, but what's the point? If you post your code, then certainty is likely to occur. Any time you (or anyone else) starts *describing* what your code does, stop, open your editor, select the code in question, and copy/paste it into your e-mail. It's easier for you, and it's easier for everyone else. Mike I fixed the problem. It turns out I was forgetting to retain the array. I'm not sure why it worked before when I was just creating it in the anonymous name space and the -init function. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
NSCFArray autoreleased with no pool in place
I am writing a Cocoa application that links to many frameworks. For some reason when I debug the program the following message appears in the debug output: *** _NSAutoreleaseNoPool(): Object 0x409660 of class NSCFArray autoreleased with no pool in place - just leaking This message is occurring before the main function for the application is executed. Suspicions are that something in one of the frameworks that gets loaded is causing the problem. What can be done to track down where this problem is occurring and fix it? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Can't create windows with framework window nib
I found that if I derive a class from NSWindowController and use that in the frame work object then things work. It seems kind of silly to derive a class just for the purpose the inherited class doesn't implement anything above what NSWindowController offers, and is just simple a wrapper to define the class as a new type. Why does something like this work? What is the best way to solve this problem? On Feb 19, 2009, at 8:01 o'clock, Tron Thomas wrote: I am trying to write a framework that contains a window nib which can be used to create a window on demand by the application linking into the framework. The framework originally contained a singleton object was defined similar to this: @interface FrameworkObject : NSWindowController { // instance members ... } + (FrameworkObject*)sharedFrameworkObject; // declared messages ... @end The FrameworkObject was set as the owner of the window nib contained in the framework. The object responded to the init message like this: - (id)init { self = [super initWithWindowNibName:@FrameworkWindowNib]; if(nil == self){ return nil; } // initialize instance members ... return self; } This worked fine. The object would load the window nib and create the window. I then decided that I did not want this object to be an NSWindowController, and I also wanted the ability to create multiple windows. So, I changed the window nib owner to NSWindowController and then changed the class interface for FrameworkObject to something like: @interface FrameworkObject : NSObject { @private NSMutableArray* _controllers; // other instance members ... } + (FrameworkObject*)sharedFrameworkObject; // declared messages ... @end The init message response was now: - (id)init { self = [super init]; if(nil == self){ return nil; } _controllers = [[NSMutableArray alloc] init]; // initialize other instance members ... return self; } When the object needed to display a window it would try to do the following: - (void)showWindow { NSWindowController* controller = [[NSWindowController alloc] initWithWindowNibName:@FrameworkWindowNib]; NSWindow* window = [controller window]; [window setDelegate:self]; [_controllers addObject:[controller autorelease]]; } However this fails at runtime with an error like the following: -[NSWindowController loadWindow]: failed to load window nib file 'FrameworkWindowNib' It is very puzzling why when FrameworkObject was derived from NSWindowController it could successfully find and load the window nib, and when FrameworkObject is change to create an NSWindowController, that NSWindowController cannot find the window nib. What is needed so the framework can use its window nib to create multiple windows? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Can't create windows with framework window nib
I am trying to write a framework that contains a window nib which can be used to create a window on demand by the application linking into the framework. The framework originally contained a singleton object was defined similar to this: @interface FrameworkObject : NSWindowController { // instance members ... } + (FrameworkObject*)sharedFrameworkObject; // declared messages ... @end The FrameworkObject was set as the owner of the window nib contained in the framework. The object responded to the init message like this: - (id)init { self = [super initWithWindowNibName:@FrameworkWindowNib]; if(nil == self){ return nil; } // initialize instance members ... return self; } This worked fine. The object would load the window nib and create the window. I then decided that I did not want this object to be an NSWindowController, and I also wanted the ability to create multiple windows. So, I changed the window nib owner to NSWindowController and then changed the class interface for FrameworkObject to something like: @interface FrameworkObject : NSObject { @private NSMutableArray* _controllers; // other instance members ... } + (FrameworkObject*)sharedFrameworkObject; // declared messages ... @end The init message response was now: - (id)init { self = [super init]; if(nil == self){ return nil; } _controllers = [[NSMutableArray alloc] init]; // initialize other instance members ... return self; } When the object needed to display a window it would try to do the following: - (void)showWindow { NSWindowController* controller = [[NSWindowController alloc] initWithWindowNibName:@FrameworkWindowNib]; NSWindow* window = [controller window]; [window setDelegate:self]; [_controllers addObject:[controller autorelease]]; } However this fails at runtime with an error like the following: -[NSWindowController loadWindow]: failed to load window nib file 'FrameworkWindowNib' It is very puzzling why when FrameworkObject was derived from NSWindowController it could successfully find and load the window nib, and when FrameworkObject is change to create an NSWindowController, that NSWindowController cannot find the window nib. What is needed so the framework can use its window nib to create multiple windows? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: performSelector:withObject fails with class object
Greg Parker wrote: On Feb 5, 2009, at 9:50 PM, Tron Thomas wrote: I have a couple of classes that are delcared like this: #import objc/objc.h #import objc/Object.h @interface SomeClass : Object { @private // Instance data members ... } + (SomeClass*)instanceFromData:(id)data; // Other methods ... @end Class Object is dead. Don't use it. Use NSObject instead. In yet another class, I have this function: + (id)createInstanceForClass: (const char*)className withData:(id)data { id metaClass = ::objc_getMetaClass(className); if(nil == metaClass){ return nil; } unsigned int count; Method* method = ::class_copyMethodList(metaClass, count); for(unsigned int index = 0; index count; ++index){ std::clog ::sel_getName(::method_getName(method[index])) std::endl; } return [metaClass performSelector:@selector(instanceFromData:) withObject:data]; } When the middle section of the function containing the class_copyMethodList is simply meant for verification, and prints: instanceFromData when the class name for either of the first two classes is provided. This seems to indicate the proper class object is being used and the expectation is that the call to performSelector:withObject: should succeed. Your introspection of the metaclass's method list shows what messages the class object will respond to. But then you send instanceFromData: to the metaclass object, not the class object. The metaclass doesn't respond to instanceFromData:, so it blows up. (Instances respond to messages from their class's method list. Classes respond to messages from their metaclass's method list. Metaclasses respond to messages from the metaclass's metaclass's method list, which is the root metaclass's method list. If you don't understand that third sentence, don't worry; in practice you only care about the first two.) If you want to call the class method +[SomeClass instanceFromData:], do this instead: const char *className = SomeClass; id cls = ::objc_getClass(className); return [cls performSelector:@selector(instanceFromData:) withObject:data]; Or you can skip performSelector:withData: entirely. It's intended for code where the selector is not known at compile time. Since you know the selector, just use it directly: const char *className = SomeClass; id cls = ::objc_getClass(className); return [cls instanceFromData:data]; However, when the performSelector:withObject: is executed, output like the following is logged, and the program traps in the debugger: *** NSInvocation: warning: object 0x1b0dc of class 'Object' does not implement methodSignatureForSelector: -- trouble ahead *** NSInvocation: warning: object 0x1b0dc of class 'Object' does not implement doesNotRecognizeSelector: -- abort These ugly errors arise because you're using class Object instead of class NSObject. Class Object is dead. I thought about trying NSObject after I posted the mailing list. The main reason I went with Object originally was because I just wanted to write something quick and simple. I did not want to worry about linking to the entire Cocoa framework and have to create an auto-release pool. However, I just configured everything to use garbage collection and that helped to keep things simple enough to still use NSObject. I originally was trying to use objc_getClass instead of objc_getMetaClass. It did not seem to be working. Maybe this was because I was hitting the NSInvocation warnings which would have happened anyway, since I was using Object instead of NSObject. Anyway, I switched to using NSClassFromString as suggested by Ken and everything is working now. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
performSelector:withObject fails with class object
I have a couple of classes that are delcared like this: #import objc/objc.h #import objc/Object.h @interface SomeClass : Object { @private // Instance data members ... } + (SomeClass*)instanceFromData:(id)data; // Other methods ... @end @interface SomeOtherClass : Object { @private // Instance data members ... } + (SomeOtherClass*)instanceFromData:(id)data; // Other methods ... @end In yet another class, I have this function: + (id)createInstanceForClass: (const char*)className withData:(id)data { id metaClass = ::objc_getMetaClass(className); if(nil == metaClass){ return nil; } unsigned int count; Method* method = ::class_copyMethodList(metaClass, count); for(unsigned int index = 0; index count; ++index){ std::clog ::sel_getName(::method_getName(method[index])) std::endl; } return [metaClass performSelector:@selector(instanceFromData:) withObject:data]; } When the middle section of the function containing the class_copyMethodList is simply meant for verification, and prints: instanceFromData when the class name for either of the first two classes is provided. This seems to indicate the proper class object is being used and the expectation is that the call to performSelector:withObject: should succeed. However, when the performSelector:withObject: is executed, output like the following is logged, and the program traps in the debugger: *** NSInvocation: warning: object 0x1b0dc of class 'Object' does not implement methodSignatureForSelector: -- trouble ahead *** NSInvocation: warning: object 0x1b0dc of class 'Object' does not implement doesNotRecognizeSelector: -- abort What is needed to make this code work as expected? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: How to use a button to update an application with a text field value
I attempted to try out what you were suggesting to see how well I understood it. I got a compiler warning that said my controller class may not respond to -selection. Also, in the case where a controller might have selector that take multiple arguments, I'm not sure how I could provide the arguments. Suppose the selector wanted access to the text field, I'm not sure how I could specify that object as an argument in the button's binding. Ron Lue-Sang wrote: Hmmm... In no particular order (sorry) - Even if the user hasn't typed into the textfield, the property that the textfield is bound /to/ will have a value that matches. This means that if I have a textField bound to MyArrayController.selection.name http://MyArrayController.selection.name. When I want that name, I just do [MyArrayController valueForKeyPath:@selection.name http://selection.name]. That will match the value that is in the textField. The textField is reflecting that value. - You can use the target/argument bindings for this case if you want. This has the advantage that your action method won't have to do the commitEditing step itself. Also you won't have to expose the method as an IBAction or anything. The target binding invokes the commitEditing (or commitEditing with delegate) method on the the bound-to object, which in this case is your NSController. NSControllers implement the whole set of NSEditor and NSEditorRegistration methods. That's why this all works. If you bind to an object that doesn't implement the full set of methods, you have to do more work. - To establish a target binding, the to object is just the controller (in this case). The keypath can be self. The selector is an option in the binding editor in IB. If you method is - (void)doWork; type doWork (without the quotes) into the selector field in the binding editor. If your method takes arguments, you'll have to change the selector. Instead of doWork, you'd have doWorkWithObject: - note the colon. Or doWorkWithObject:usingNeighborsHedgeClippers:returnWhenDone: For each argument you have, you'll need to bind the argument binding to provide… an argument. If you've followed what I've outlined here, you can just implement doWork like this - (void)doWork { NSString *name = [[self selection] valueForKeyPath:@name]; // note, won't matter whether user's typed yet NSLog(@here's the name! %@, name); } Make sense? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
How to use a button to update an application with a text field value
I am writing a Cocoa application that contains an NSTextField control. Things are such that when a user types a value into this control and hits enter, a binding will update an NSController instance in the application with the value contained in the text field. This is more elegant that having to rely on creating an IBOutlet in the controller class and establishing a connection between the outlet and an action in the text field control to accomplish the same thing. However, the application also contains a button that when clicked is also supposed to provide the controller with the contents of the text field. Obviously this could be done be create the appropriate outlets and actions and connecting them up in Interface Builder. However, this kind of defeats the whole purpose of using the binding mentioned earlier. It would be desirable to avoid creating outlets and actions for the button functionality as well. Perhaps something could be done using the argument and action bindings in the button to affect the desired result. It is not obvious this could accomplished. One thing I found was that if I implemented an action in my controller and connected it to the button, the controller could then call the commitEditing method in response to that action. This could accomplish the same thing as pressing enter in the text field control. This only works though when the text field has been edited. If the text had not been edited, the user could still expect the button to cause the update. It would not be clear to them why the button wouldn't do anything when no editing had taken place. One thought I had to make this relationship clear was to disable the button initially and then enable it only after the text field had been edited. I found I could do this by using the objectDidBeginEditing: method. By responding to that method I could use a binding that would affect the enabled state of the button. In spite of this, what I also find is it that after implementing the objectDidBeginEditing: method, the action for the button that calls commitEditing no longer works. For some reason the control thinks that no editors need to commit any editing. If I remove the implementation for objectDidBeginEditing: things work fine. When I add it back in, things fail. What can help me to implement the functionality I'm trying to develop? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: How to use a button to update an application with a text field value
The way things are now, when things work right, (i.e. when objectDidBeginEditing: is not implemented) the call to commitEditing will cause the text field's action to fire which will use the binding between the text field value and the NSController's property to cause the update to occur with no outlet/action interaction needed. For what you are suggesting, I believe the controller would require an IBOutlet instance variable that referred to the text field, so it could send the stringValue message to that control and get its result. If I'm going to take this approach, I might as well abandon the binding all together. However, the binding works really slick without having to add anything extra to the class. I'd let to see if an elegant solution that only needs the bindings can be found for this problem. Graham Cox wrote: Why not just grab the field's -stringValue: after you call commitEditing? In other words pull the value from the field instead of trying to provoke it into pushing its value somewhere. Much easier, no? hth, Graham On 6 Aug 2008, at 1:45 pm, Tron Thomas wrote: What can help me to implement the functionality I'm trying to develop? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: How to use a button to update an application with a text field value
The program is a simple game. The player answers questions by typing something in and then either pressing enter or clicking on the button to commit their answer. Although not likely it is, possible the user may try to use the previous information as the answer to the current question, so they might simply click the button to resubmit an answer without editing anything. I am not calling the super method for objectDidBeginEditing. I thought it worked like a delegate method. Perhaps this is where the problem lies. I can try calling [super objectDidBeginEditing:editor], and see how that works. Ken Thomases wrote: On Aug 5, 2008, at 10:45 PM, Tron Thomas wrote: One thing I found was that if I implemented an action in my controller and connected it to the button, the controller could then call the commitEditing method in response to that action. This could accomplish the same thing as pressing enter in the text field control. Yes, that's what -commitEditing is for. This only works though when the text field has been edited. If the text had not been edited, the user could still expect the button to cause the update. It would not be clear to them why the button wouldn't do anything when no editing had taken place. I don't understand. If no editing is in progress, then the value in the model and the value displayed in the text field should already be in sync. What is it that you want to happen when the user presses the button? If there's something that needs to happen above and beyond the text field's value being pushed to the model, then... just make it happen in the action method (after the -commitEditing). One thought I had to make this relationship clear was to disable the button initially and then enable it only after the text field had been edited. I found I could do this by using the objectDidBeginEditing: method. By responding to that method I could use a binding that would affect the enabled state of the button. In spite of this, what I also find is it that after implementing the objectDidBeginEditing: method, the action for the button that calls commitEditing no longer works. For some reason the control thinks that no editors need to commit any editing. If I remove the implementation for objectDidBeginEditing: things work fine. When I add it back in, things fail. Sounds like your override of -objectDidBeginEditing: is not calling through to super. You're effectively replacing the implementation rather than extending it. That said, it sounds to me like you're approaching things in a weird way. Cheers, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Cocoa binding always produces zero for bound value
Ken Thomases wrote: On Mar 23, 2008, at 8:18 PM, Tron Thomas wrote: At this point, the compiler only knows that object is an id. It doesn't know the specific class. So, of any of the value methods it might know about, it doesn't know which specific one is meant. Normally that wouldn't matter, but when you're dealing with float returns (or struct returns) the generated code would be different. In other words, the compiler is generating the code here as though it were invoking a value method that returns an int. What is actually being invoked is a method which returns a float. The mismatch causes the problem. The problem is even though the value is updated to some non-zero value, the observed value is always zero. I have tried stepping through the code and verified that when I step into [AnotherObject value] method from the call in [SomeObject observeValueForKeyPath:ofObject:change:context:], the _value instance member is non-zero. Yet when I return to the calling method, the local value variable gets set to zero. This does not make sense. What would allow the SomeObject instance to get the proper value for AnotherObject when observing it? There are two approaches to fixing this: 1) Use a method name which won't be ambiguous about its return type, such as floatValue. It's not the name that actually matters, it's that all of the floatValue methods that the compiler might know about at that point have a return type of float -- because what perverse soul would create a floatValue method that returned something other than float -- so there's no chance of confusion. 2) Cast object to the specific class that you know it to be (i.e. AnotherObject*). You would have to make sure you've imported AnotherObject.h, of course. Cheers, Ken Using a type cast solved the problem. Thanks Ken. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]