 |
 |
Mouse in objective-c
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Is there a quick and easy tutorial on using the mouse in objective-c? All I am trying to do is get the click on a NSView... Thanks! 
|
|
|
| |
|
|
|
 |
|
 |
|
Banned
Join Date: Apr 2002
Location: -
Status:
Offline
|
|
Originally posted by djohnson:
Is there a quick and easy tutorial on using the mouse in objective-c? All I am trying to do is get the click on a NSView... Thanks!
Objective-Cee is totally unrelated to.. the mouse.. or NSViews... you're probably talking about Cocoa.
Make a subclass of NSView, override -(void)mouseDown  NSEvent *)event
there are 128973897912837 tutorials for this... namely "Scribble" at cocoadevcentral.com
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Yes I did mean cocoa... I was looking through cocoedevcenter.com... Of course if I had opened my eyes I would have seen SCribble. It was the very top line on that window...  I guess I need something to wake me up this morning. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ahh much better... I know what I was doing wrong now. I missed one simple connection. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok, how about this. When I detect a mouseDown in my NSView class, I need to call another class and have it do something. I have tried a connection to the other class, but it does not work... Any ideas?
|
|
|
| |
|
|
|
 |
|
 |
|
Banned
Join Date: Apr 2002
Location: -
Status:
Offline
|
|
You really need to a book man,
amazon.com is your best friend.
I explained this so many times I just don't feel like repeating myself again
either buy a book, search the forums or the mailing list archive ( http://cocoa.mamasam.com)
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Sorry about that... For those that care to learn, you will need to setup and call NSNotificationCenter. Make sure the object you want to call subscribes to the notification. Hope this helps someone else out there!
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Originally posted by ambush:
You really need to a book man,
amazon.com is your best friend.
Hehe, I'd echo that. This is the kind of basic functionality that beginner Cocoa books are geared towards teaching.
|
|
|
| |
|
|
|
 |
|
 |
|
Banned
Join Date: Apr 2002
Location: -
Status:
Offline
|
|
Originally posted by djohnson:
For those that care to learn, you will need to setup and call NSNotificationCenter. Make sure the object you want to call subscribes to the notification.
No.
Why are you claiming this?

|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Because that is how I got it to work... PM me if you have a better way... Oh and I have a cocoa book, not much good if you ask me. They covered the basic basics, that anyone with a CS degree can pick up in about 3 minutes flat. It never went over mouseDown or passing notifications between objects... I found that snipet of code on cocoadevcenter.com and am using it. It works just fine. Now if you care to explain why what I said wont work?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
Originally posted by djohnson:
Because that is how I got it to work... PM me if you have a better way... Oh and I have a cocoa book, not much good if you ask me. They covered the basic basics, that anyone with a CS degree can pick up in about 3 minutes flat. It never went over mouseDown or passing notifications between objects... I found that snipet of code on cocoadevcenter.com and am using it. It works just fine. Now if you care to explain why what I said wont work?
It'll work, but NSNotificationCenter is damned slow compared to a method call. As an example, in my program I pass of all of the mouseXXX methods to another object without using notifications. Just [toolName mouseXXX];
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Originally posted by djohnson:
Because that is how I got it to work... PM me if you have a better way... Oh and I have a cocoa book, not much good if you ask me. They covered the basic basics, that anyone with a CS degree can pick up in about 3 minutes flat. It never went over mouseDown or passing notifications between objects... I found that snipet of code on cocoadevcenter.com and am using it. It works just fine. Now if you care to explain why what I said wont work?
If you know the method you need to call in the other object, why aren't you just calling that method instead of posting a notification?
There are a lot of ways to achieve a desired result when you're programming. 'Anyone with a CS degree' knows that often the way you achieve that result is what matters.
Here's a good book, though I hear an update is coming out soon.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok... I know NSNotification is a little slow. In another program I wrote, I have IBOutlets between my objects, calling funcations and stuff. It works great and fast. I am testing out mouse stuff in a simple program now. I have 2 objects, a subclasses NSView and subclassed NSObject. I tried setting up the IBOutlet between the 2 objects, but they wouldnt talk to each other... All I need to do is let the NSObject class know when there is a mouse event in NSView class. I also need to pass the mouse coordinates over. No problem right?
I would prefer an internet based source, I dont want to buy a book.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
Originally posted by djohnson:
Ok... I know NSNotification is a little slow. In another program I wrote, I have IBOutlets between my objects, calling funcations and stuff. It works great and fast. I am testing out mouse stuff in a simple program now. I have 2 objects, a subclasses NSView and subclassed NSObject. I tried setting up the IBOutlet between the 2 objects, but they wouldnt talk to each other... All I need to do is let the NSObject class know when there is a mouse event in NSView class. I also need to pass the mouse coordinates over. No problem right?
I would prefer an internet based source, I dont want to buy a book.
Well, I don't see why IB needs to be involved. Just instantiate the NSObject subclass in your view subclass and use it like any other object.
<edit> This assumes that your object subclass doesn't have IBOutlets/Actions. If that's not true, then using an IBOutlet should work just as well as this, and I'm not sure why it doesn't. </edit>
Code:
@interface MyView:NSView
{
MyObject * obj;
}
@implementation MyView
{
- (void) awakeFromNib
{
obj = [[MyObject alloc]init];
}
- (void) mouseDown:(NSEvent *)e
{
[obj mouseDown:e];
}
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok I did try that earlier today. Of course I had a few other things not working as well that I have working now. Let me try that tonight or tomorrow and see if it works. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok... I did some reasearch on the new Cocoa books... Much better then the old one I have. So guess what? Time to talk to misses into letting me get a book 
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok... How about suggesting that people just use a NSButton with setTransparent:YES? This way works great with me now... Until I get a good definitive book, I will stay away from NSView's... Of course once I figured out NSButton would do exactly what I needed, it took me a total of 5 minutes including creating a new project, copying in my code, and compiling it. 
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2004
Location: Indianapolis, Indiana
Status:
Offline
|
|
I find it funny that people get ambushed by asking questions that "ADDICTED TO MACNN" members deem "basic", if its basic, then dont waste your time answering the question. What good is taking 5 minutes to respond to a posting with "Get a book".
It all comes down to my belief that most developers are total "holier than thou" pricks. Just read the comments on /. for proof of that. And remember one great thing about the internet is the basic communications, communications that you can not get from a book.
So lets go back to what Thumper's mother said children. If you have nothing nice to say, don't say anything at all...
But then again thats just my opinion. I could be wrong.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Originally posted by BrandonCorbin:
I find it funny that people get ambushed by asking questions that "ADDICTED TO MACNN" members deem "basic", if its basic, then dont waste your time answering the question. What good is taking 5 minutes to respond to a posting with "Get a book".
It all comes down to my belief that most developers are total "holier than thou" pricks. Just read the comments on /. for proof of that. And remember one great thing about the internet is the basic communications, communications that you can not get from a book.
Err, the fact that I've posted on MacNN for about three years has nothing to do with my suggestion to get a book. Actually, I've rarely posted in the developer forum in that time.
The Internet is great for communication, but that doesn't mean asking basic questions on a forum is the best way to find information. I don't see what's 'holier than thou' about suggesting that a beginner read a beginner Cocoa book. I feel those books are excellent resources and the best way to get a real grounding in Cocoa because they offer a comprehensive view of the technology. The guy who asked this question has asked several other questions on a similar level -- so personally I felt he would have an easier time if he got a book. Excuse me for trying to solve the real problem with some helpful advice.
A 'holier than thou' answer would be telling someone to go look at developer.apple.com, btw. And a 'holier than thou' poster would act like everyone owes him answers. Part of successful programming is being resourceful, and message boards are a resource not the resource, and certainly not a crutch.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Could have just been nice and put a link or maybe email a short amount of code... I will still get *another* book one of these days. So far all of the applications that I have written have been done without the assistance of a book. This will be the first one that actually needs a special type of input. I guess asking here was a mistake... Too bad, good thing I found www.idevgames.com and they helped out great. Thanks for sticking up for me BrandonCorbin!
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status:
Offline
|
|
in my experience books are useless for coding, but sample/open-source code is a gold mine. Often I don't even find what I was looking for, but just looking at something other than my own code gets me to think about the problem in a new way.
But if (when) you do ask for help, try to make it clear that you have done a little homework first. Programmers may not all be holier than thou, but they do resent being taken for granted. Ask your question, then follow it by saying what you've tried already and what you expect people to say. Show the people you want to help you that you are somewhat competent and you're not expecting them to do every little thing for you (also: don't expect them to do every little thing for you)
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Here is what I have tried so far:
1. Creating an IBOutlet in my mouseView and connecting it to my NSObject. Did not work.
2. Setup a NSNotificationCenter. Works great, but the speed is horrible.
3. Started over and made a NSButton, set all of the options. Works fine, but isn't really what I wanted to do. I need to get mouse coordinates and such.
4. Looking into giving my mouseView a delegate. I have only spent about an hour reading up on it, so I am sure I will figure it out a little better when I read on it at work.
Any other suggestions? Sorry I made the initial post so vague. I am not used to running into a block like this... Usually I figure out what I want to do really fast.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Originally posted by djohnson:
Could have just been nice and put a link or maybe email a short amount of code... I will still get *another* book one of these days. So far all of the applications that I have written have been done without the assistance of a book. This will be the first one that actually needs a special type of input. I guess asking here was a mistake... Too bad, good thing I found www.idevgames.com and they helped out great. Thanks for sticking up for me BrandonCorbin!
Sorry if it came across as not being nice. I meant nothing mean-spirited by it. ambush had already given you a tip regarding the code before me, all I did was follow it up with the suggestion that taking the time to read a programming book would free you from having to ask a lot of these questions. Invariably, if you are asking a few of these types of questions, you're going to end up asking a lot of them.
I'm curious now -- why do so many people view programming books in a negative light? This is not the first time I've encountered this phenomenon.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2004
Location: Indianapolis, Indiana
Status:
Offline
|
|
I for one like books. But its almost impossible for a book to cover every single topic. So most of them focus in on a specific area. Also I think that it has to do with peoples learning style.. Some people are killer book readers and can retain everything they read. Others are better by seeing a visual, or an audible.
I just got the Cocoa Development for Dummies, and it was a waste of 24 bucks. It covers the bare basics, I mean it helped some but I need to get something much more in-depth. Im just waiting for someone to come up with a book that lists every available class and EXAMPLES of them. Then all of the UNDOCUMENTED Features...
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
The 1 cocoa book I picked up was not that great. While getting my degree, I always viewed books as a waste. That taught lots of theory but not enough code to test the theory. People want the code so they can test out how things work. I am getting a book, the newer version of the one above, within a week or so. It looks like it should help out some. I think I am missing 1 line of code in my program to tie everything together.
I just figured out how to get the current position of the mouse. Now I need to send the coordinates to my subclassed NSObject so that it will display them. I am sure this is trivial and will probably look back at being exactly that, but for now I am stuck... 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Well it's true, not all books are created equal. However, I've read two books that very adequately cover this area (as any decent Cocoa book should): the 2nd edition O'Reilly book and the Hillegass book I linked to above. User reviews on amazon.com are often a good reference when deciding which books to purchase.
All you have to do to communicate with an object is send a message to the object. This is standard OOP, but the Obj-C specifics are also covered quite adequately in the above books. The purpose of a delegate is to have a special relationship between two objects, but it's not necessary for another object to be a delegate in order to communicate with it. If you are using a custom view subclass, there are a few ways you can pass references to other objects into it, but the best way depends on the relationship between the two objects. If you'd post some more details regarding the two objects you want to hook up, we can give you some more concrete pointers. For example, if you have an NSView subclass and a controller object, they are probably both instantiated in your nib, so you can use IB to hook them together.
That said, I just want to point out that you've spent days on a problem that both of those books cover in one chapter, probably in one example. That's why I thought having a book you can reference quickly would save you time.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Well in all honesty, I have learned how to use mouse inputs, how to make buttons not look like buttons, how to grab mouse coordinates, etc.
I have tried creating an outlet in the mouseView(subclasses NSView) and connected it to my Controller(subclasses NSObject). This has worked on previous applications. Of course those are subclassed NSObjects to subclasses NSObjects. From the mouseView it refuses to call the Controller class. I guess I could link in the code, but that will have to wait until tonight when I get off work or more than likely, tomorrow when I have the day off. I hope this helps you figure it out some... 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Well an NSView sublcass is also an NSObject subclass, it shouldn't make any difference... Do you get an error when you call the controller object? Have you stepped through the debugger to see what's happening? Can you post some of the relevant code from both classes?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
I have ran the debugger with it. I guess I should try stepping through it...  I dont get an error though. I am writing to NSLog when I call the things to make sure they get called. They never do... I will check that out tomorrow. Any other suggestions?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
I find it near impossible to help someone with a programming question without being able to see at least part of the code in question. Usually it's hard for the person asking the question to word it correctly, and equally hard for the person trying to answer the question to make sense of the question or answer in away that the questionner could make sense of. This is especially true when it's a basic idea.
Matt Fahrenbacher
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
You can find the code here: http://www.tfworld.com/code/mouse.sitx
Let me know what I am missing. When I call [contConn mouseIsDown]; from within the mouseDown function of mouseView, it will not call it. Check it out for yourself to see what I am talking about. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status:
Offline
|
|
I don't see a nib file in that archive
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status:
Offline
|
|
well I found two problems. I don't know which one solved it (or both maybe).
one, you have declared your NSView subclass's IBOutlet as an (id *) when it should be just (id). The compiler warned you about this but you ignored it. two, you created an extra instance of your subclass and connected its IBOutlet to your controller. What you want to do is delete that extra instance and connect the one in the window to the controller.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: Texas
Status:
Offline
|
|
Ok, maybe I *do* need that new cocoa book. Here I was thinking that you have to instantiate every class. Thanks for the help, I fixed it and now it works great! 
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Originally posted by djohnson:
Ok, maybe I *do* need that new cocoa book. Here I was thinking that you have to instantiate every class. Thanks for the help, I fixed it and now it works great!
... ahem ...
Anyway, glad your problem was fixed.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|