Very easy question: rect button

Discussion in 'iPhone/iPad Programming' started by fireshot91, Jul 26, 2011.

  1. fireshot91, Jul 26, 2011
    Last edited: Jul 26, 2011

    macrumors 601

    fireshot91

    Joined:
    Jul 31, 2008
    Location:
    Northern VA
    #1
    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.
     
  2. macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #2
    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 :)
     
  3. macrumors member

    Joined:
    May 2, 2011
    Location:
    Mississauga, ON, Canada
    #3

    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. :)
     
  4. thread starter macrumors 601

    fireshot91

    Joined:
    Jul 31, 2008
    Location:
    Northern VA
    #4
    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
     
  5. fireshot91, Jul 26, 2011
    Last edited: Jul 26, 2011

    thread starter macrumors 601

    fireshot91

    Joined:
    Jul 31, 2008
    Location:
    Northern VA
    #5
    Also, when I copy/paste your code, it gives me 3 build errors.
    These two in viewcontroller.m
    Code:
     expected specifier-qualifier-list before '-' token
    
    
     expected '{' before '(' token
    This one is in AppDelegate.m

    Code:
    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

    Code:
        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.
     
  6. macrumors member

    Joined:
    May 2, 2011
    Location:
    Mississauga, ON, Canada
    #6
    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. :)
     
  7. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    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.

    Code:
     
    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

    Code:
    - (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.
     

Share This Page