PDA

View Full Version : Very easy question: rect button




fireshot91
Jul 26, 2011, 11:06 AM
So, just FYI, I have never made an app. Don't know Obj-C, or anything. But I've been tasked with creating a simple HelloWorld app which displays a button, and when clicked a popup with just a HelloWorld text comes up.

Now, I've created the project, and I've moved a rect button onto the window. And that displays fine (With the text I want on it), but I can't get it to do any action onclick. Where do I even edit the file to add an onclick event? (Err..ontouch?)


Sorry, extremely newbie here. I'm sure this will take you guys a grand total of 5 seconds to explain to me.



ChOas
Jul 26, 2011, 11:17 AM
[...]

Sorry, extremely newbie here. I'm sure this will take you guys a grand total of 5 seconds to explain to me.

No, showing you how to do it would take already more than 30 seconds.

Explaining (for real) how to do it would take MUCH longer :)

francis21
Jul 26, 2011, 11:20 AM
So, just FYI, I have never made an app. Don't know Obj-C, or anything. But I've been tasked with creating a simple HelloWorld app which displays a button, and when clicked a popup with just a HelloWorld text comes up.

Now, I've created the project, and I've moved a rect button onto the window. And that displays fine (With the text I want on it), but I can't get it to do any action onclick. Where do I even edit the file to add an onclick event?


Sorry, extremely newbie here. I'm sure this will take you guys a grand total of 5 seconds to explain to me.


Yes, this would really take long to explain. But I'll do my best to give you the jist of it.

So the underlying concept here is Action and the ability to connect the views you've added to your View. Basically, to connect an action method to your rect button, you'll do the following first (in your ApplicationViewController.h):



@interface ...

...
...

- (IBAction)anyNameForThisMethod: (id)sender;



Then in the ApplicationViewController.m - where you implement any actions you've defined in the interface file - you'll do the following:



@implementation ...

...

- (IBAction)anyNameForThisMethod: (id)sender {
/*According to your description, a UIAlertView is an ideal thing to use, to pop up the "Hello, World" string. */

//Add your implementation here.
}



Then, the only thing left for you to do is to connect the rect button to this action method. You can accomplish this, you need to hold the control button and then drag from the rect button to the File Owner's object in the Interface Builder. Then, from now on, when the rect button is pressed (represented by the Touch Up Inside button - i.e. a basic tap on your iOS device), then the method anyNameForThisMethod will be called.

Hope this helps. :)

fireshot91
Jul 26, 2011, 11:46 AM
Then, the only thing left for you to do is to connect the rect button to this action method. You can accomplish this, you need to hold the control button and then drag from the rect button to the File Owner's object in the Interface Builder. Then, from now on, when the rect button is pressed (represented by the Touch Up Inside button - i.e. a basic tap on your iOS device), then the method anyNameForThisMethod will be called.

Hope this helps. :)

Sorry, I didn't know it would be that difficult to understand.

When I ctrl+drag the rect button, it doesn't..."attach" to the File's Owner. I guess I could've worded that better, but idk of any way

fireshot91
Jul 26, 2011, 12:41 PM
Also, when I copy/paste your code, it gives me 3 build errors.
These two in viewcontroller.m
expected specifier-qualifier-list before '-' token


expected '{' before '(' token

This one is in AppDelegate.m

expected specifier-qualifier-list before '-' token


EDIT: Nvm, I got that to work without errors. Now, it works fine. But is there a better message box that I can use other than

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:@"World"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];
[alert release];

I tried taking out the message line, and the two buttons. But whenever I do that, everything gives me an error.

francis21
Jul 26, 2011, 01:11 PM
Also, when I copy/paste your code, it gives me 3 build errors.
These two in viewcontroller.m
expected specifier-qualifier-list before '-' token


expected '{' before '(' token

This one is in AppDelegate.m

expected specifier-qualifier-list before '-' token


EDIT: Nvm, I got that to work without errors. Now, it works fine. But is there a better message box that I can use other than

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:@"World"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];
[alert release];

I tried taking out the message line, and the two buttons. But whenever I do that, everything gives me an error.

Well, another very simple idea is as follows: create an outlet that is connected to a label, and as the user presses your rect button, it would change the text on the label view to the following: "Hello, World".

PS: Try reading more about actions and outlets, as these are the fundamental elements in iOS programming. Because these are the things that you will be using throughout your iOS programming journey. :)

jnoxx
Jul 27, 2011, 03:21 AM
other message box

Hmm, iOS is all about providing the same UI troughout every app, that's what seperates iOS from Android/W7/RIM etc. Users can nearly navigate through every app, just because they are used to it.
So you can create your own boxes, like creating your own view like.


UIView *alertie;
alertie.backgroundColor = [UIColor blackColor];
[alertie setFrame:CGRectMake(20,20,100,100)];
[self.view addSubview:alertie];
//Now you have an alert view, you can add a label on there.


What you can do, is create a view yourself in XIB, create an IBOutlet to it in the .H File, like IBOutlet UIView *loadingView; and then create a nifty design in XIB. and assign loadingView through it like u did before (outlets).

Then just go like this in your code block


- (IBAction) someMethod:(id)sender {
NSLog(@"clicked..");
[self.view addSubview:loadingView];
}

about creating outlets for labels, and assigning text to them on your click, i'll leave to you.
And if you don't wonna get into programming, just hire a programmer to do stuff for you.