REALLY simple question

Drarok

Registered
I've been all over the internet tonight (well... Google has on my behalf), and I can't for the life of me find out how to do the simplest of tasks in XCode. I'm really getting fed up.

Can anyone tell me a few things / share a simple Hello World Objective-C App?

I'm awaiting a book to come out, but that's in July! (Look on amazon for XCode).

So, yeah... what's the equivalent of a windows MessageBox() call?

Thanks in advance.
 
Additional: This doesn't do ANYTHING?!?!
[helloButton setTitle:mad:"OMFG!!!"];

helloButton is an NSButton linked to a button on the window (control-drag in the interface builder)
 
Drarok said:
So, yeah... what's the equivalent of a windows MessageBox() call?
int NSRunAlertPanel(NSString *title, NSString *msg, NSString *defaultButton, NSString *alternateButton, NSString *otherButton, ...)

title: Title of the message box, equivalent to Win32's second argument in MessageBox().

msg: duh :p

the last 3 args in NSRunAlertPanel (defaultButton, alternateButton, otherButton) are the 3 possible buttons in your alert box. They can be whatever you want, but if you pass "nil", you get the default, which I believe is one single "OK" button.
 
Drarok said:
I've been all over the internet tonight (well... Google has on my behalf), and I can't for the life of me find out how to do the simplest of tasks in XCode. I'm really getting fed up.

It's a completely different API - it takes some time to learn new ways of doing things. Honestly, the hardest thing about learning Cocoa is learning to look for the easy way of doing things. A Windows programmer will think, "well, to do that, I need to override this function and catch that type of event," etc. In Cocoa the answer is often, "draw a line from here to there - done."

So, yeah... what's the equivalent of a windows MessageBox() call?

Thanks in advance.

The equivalent of MessageBox in Cocoa is NSAlert.

Wade
 
Drarok said:
Additional: This doesn't do ANYTHING?!?!
[helloButton setTitle:mad:"OMFG!!!"];

helloButton is an NSButton linked to a button on the window (control-drag in the interface builder)

What did you do to have that code called? I'm guessing nothing, or it was called long before your window and button were ready for display.

What you need to do is put that code in a awakeFromNib method of your controller. Then it will be called after your controller object is unarchived from the nib file:

-(void) awakeFromNib
{

[helloButton setTitle:mad:"OMFG!!!"];
}

(This presumes of course that the controller object contains the outlet called "helloButton" and that outlet is connected to the button)

Wade
 
I put that in what I would call an onClick event. I created my own class, created an outlet called helloButton, then connected that to a button on my interface. I'll upload the files when I get home, so you can see how useless i've been ;)

Thanks everyone for responding, I'll have a go at all of this tonight (Won't be home for another 2 hours yet.
 
wadesworld said:
It's a completely different API - it takes some time to learn new ways of doing things. Honestly, the hardest thing about learning Cocoa is learning to look for the easy way of doing things. A Windows programmer will think, "well, to do that, I need to override this function and catch that type of event," etc. In Cocoa the answer is often, "draw a line from here to there - done."
......
Wade


I disagree, I don't have to override this and that and catch events... mainly because I don't use C/C++ anymore on windows. I'm currently using Delphi (.NET and VCL) and soon will be using MS VC#, which will be fun.

Are there any sites that are aimed at getting people like me up to speed on XCode?
 
Well, I found http://www.cocoadev.com/index.pl?HowToProgramInOSX to be invaluable when I was learning. It's not Windows-centric, but it covers a lot of basics. The hardest thing for me was figuring out Interface Builder's confounding behavior (I never would've figured out the whole control-drag thing on my own, for example, and that's, y'know, kindasorta important).

MacDevCenter has some good beginner-oriented articles I found helpful, too (with example projects, thank God). Personally I found Apple's built-in example projects far too complicated and confusing to be useful when I was beginning (although I find them useful now, a few months later).
 
I'll have a browse through those.

Turns out the outlet wasn't connected, that's why nothing was happening. (d'oh!).

But now I have a messagebox coming up, cool!

I was expecting it to be one of those roll-out ones, like in Safari (Bookmarks, Add Bookmark).

I really need to get right down to basics, methinks.
Code:
	int x = 0;
	x = NSRunAlertPanel(@"Title",@"msg",@"Default",@"Alternate",@"Other One");
	// Now how do I convert an int to a string?
 
ooooh and has anyone used the NSLevelIndicator ? I've set the maxValue and minValue, but setValue doesn't appear to apply?
 
There is no setValue method, but all subclassed of NSControl have a variety of set..Value methods, like setIntValue, setFloatValue, and setStringValue. They aren't all useful for every control, but every control will at least respond to them. For example, if you call for a text fields intValue, it will return whatever integer is represented in the field (if any). I've never used NSLevelIndicator, but I think setDoubleValue is what you're looking for (I imagine setIntValue and setFloatValue would work, too).

To convert an int to a string, you could do this:

NSString *s = [NSString stringWithFormat:mad:"%d",myInt];
(See the built-in help for more info on format strings.)

You could also make an NSNumber out of your int and then call NSNumber's -stringValue method.
 
Yeah, that's all the basic stuff I need to read about really. And loops! I dunno how to do a for loop! lol.
 
Seth Roby wrote some good articles about basic C stuff in the Cocoa context at MacDevCenter. His first article is "C is for Cocoa". Check out his stuff at http://www.oreillynet.com/articles/author/1254

Here's the gist of For loops:

int i;
for (i=1; i<=10; i++) {
//do stuff
}

That'll "do stuff" 10 times.

There are three statements in the for loop. The first defines the starting values of the variable you're using for the loop; the second defines the condition(s) that must be met each time through the loop to continue; the third is an operation to be performed after each iteration (generally just incrementing your int).

Personally, I miss the simplicity of BASIC-style loops. :( C's loops are definitely more flexible, though.
 
That's odd, I assumed it would be the same as C and it didn't work.
Also, if i send my "cocoa1.app" to anyone, it doesn't work, how useless is that? (I assume there's something that needs doing?)

error: 'for' loop initial declaration used outside C99 mode.

!!!

[edit!]
Looks like you can't declare the int in the loop, i was doing:
for (int x = 0; x<10; x++) {
//blah
}

tsk.

Still stumped on sending it to my mates tho :/
 
You need to set the active build style from "Development" to "Deployment" for it to work on other machines. The option is under the Project Menu. Had me pulling out my hair at first, too. :)
 
Winner! That worked. Thanks for everyone's help, I seem to be able to make some things happen now. Next plan is to make a global hotkey app for controlling iTunes (Like Winamp 5 does now). Command-Option-Ctrl-Right Arrow for next track, anyone? :)
 
Back
Top