Button Nube

Discussion in 'iOS Programming' started by IDMah, Oct 20, 2011.

  1. IDMah macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #1
    Hi all.

    I can't seem to figure out how I would create a button template
    ie. a button with labels in each corner and one in the centre.

    and then create a matrix of 9 x 13 of those buttons.
    also how do you access the matrix without having 117 IBOutlets ?

    Could I do it in IB or would it be better to create it programmatically?

    thanks
    Ian

    ps. Should the button object be a UIViewClass ?? or an NSObject..
    pss. I did call it a NUBE question.. :)
     
  2. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    You can have multiple buttons connect to a single IBAction method.

    You'd set it up like:

    Code:
    - (IBAction) buttonPressed:(UIButton *)sender;
    You can then use sender to figure out which button was hit. I think in your case the best solution would be something like:

    Code:
    switch (sender.tag)
    {
        case 0:[...]
    Tag is an int that you can assign separately to each button in IB.
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    You mean you want to make an Gridview? You can just make them in code, with some simple math calculations, and then
    Code:
    for (UIButton *button in self.view.subviews) {
     [button addTarget:@selector(something];
    }
    I don't have XCode open right now, so don't just copy paste what I wrote, but think about it, because 117 outlets is indeed a bit of waste.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Seems to me subclassing UIButton would help in this department.
     
  5. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #5
    ahhh yes... but now..

    Thanks gang.. I was going to use NSMatrix but I think that the game will require more exact placing of the buttons / images.. so I'm going to use something like this in a loop.

    Code:
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(X1,40, Y1, 40);
    [view addSubview:button];
    but one question that spring to mind is how do I access all the buttons?
    if I make 117 buttons how do I access or get the events I'd like to get receive a value when the button is pushed..

    *Also* is it possible to change a button type 'on the fly' from Sticky to 'push' and vice versa.

    thanks.
    Ian

    ps. Since the labels are for Diagnostics anyways I'm just going to stack the
    text instead of rendering creating some fancy four label button. just a
    thought couldn't I create an image by converting NSRectangle ? or is that
    too old school way of thinking?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Good thing you abandoned this option. NSMatrix is not available for iOS.

    Might want to review the parameters and their order for CGRectMake. Doesn't look like you are using it correctly.

    If you need access to these buttons after this loop, you should probably save references to them. As for "getting the events", that is exactly what adding the target to the button sets up for you.

    Not sure what you mean by sticky vs. push. Care to elaborate?

    Seems rather old school to me. If you don't want to subclass your button, perhaps consider adding your extra labels as subviews.
     
  7. IDMah, Oct 24, 2011
    Last edited: Oct 24, 2011

    IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #7
    push vs. Sticky buttons

    Thanks I'll double check the CGRectMake info.

    Code:
    // yeap ... (positionX, positionY, sizeX, SizeY)
    button.frame = CGRectMake(X1, Y1, 40, 40);
    
    A push button is like the ok button.. it activates when you release on it..

    the Sticky is like a push button switch. push to turn it on..
    (a graphic could light it up) or change colour it .. "Sticks" on
    and when you click on it again it goes back to normal again..

    Let me guess, that's not available on the iphone either..

    ??? Subviews ???

    thanks.
    Ian

    ps. found those buttons in here.
    http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Button/Button.pdf
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    When you're coding an iOS app, you should probably avoid documentation for Mac apps and stick with iOS documentation. In other words: UIButton in this case. You'll find info about its various states and types in the class reference for it.

    If you don't know what subviews are or how they can come in handy, perhaps it's time to step away from the real coding and go learn the fundamentals of iOS Development.
     
  9. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #9
    Ouch !

    Transitioning from books to a real project is been tough !!! but I appreciate all the understanding and help people are giving me. Frankly it's a second career for me, since my old one is almost dead and buried.

    but I'm doing my best.

    I'll try not to ask any more stupid questions. but it's a confusing maze of too much information.
    thanks for any help
    Ian
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Didn't mean for that to hurt. It was just some advice before you get too far in over your head.

    I understand. But what books have you been learning from that don't teach you about subviews?

    It wasn't a stupid question. It's just that without a decent knowledge of the fundamentals, any answer given may not make any sense. Kind of like if you had asked how to write a book in French and I answered you "en francais". :D
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #11
    I prefer "un livre".
     

Share This Page