How To Select Random UIButton From NsMutablearray

Discussion in 'iOS Programming' started by mandude, Apr 27, 2010.

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    I want to have a Mutablearray, have a list of three UIButtons in the array, randomly select one, and create a CGPoint that will help me move the imageView to its new point. Once that random button is chosen and sent to the point (50, 100) after that the random object that was moved should be removed from the array here's what i got so far :


    h. file

    Code:
    
    UIButton*button1;
    UIButton*button2;
    UIButton*button3;
    
    
    NSMutableArray *array;
    
    @property.....
    @property.....
    @property.....
    -(IBAction)buttonPressed;
    
    
    m. file

    Code:
    
    -(void)viewDidLoad {
    
    array = [[NSMutableArray alloc] initWithObjects: button1, button2, button3,nil];
    
    }
    
    -(IBAction)buttonPressed {
    
    	[[array objectAtIndex: (random() % [array count]) ?????????];
    CGPoint point1 = **RANDOM UIButton FROM ARRAY**.center;
    point1.x = 50;
    point1.y = 100;
    **RANDOM UIButton FROM ARRAY**.center = point1;
    
    

    as you can see i need to be able to point the the random object from the array and set it to that point, (50, 100)
     
  2. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    Code:
    [[array objectAtIndex: (random() % [array count])];
    
    That line gets you the random object. Return it into something and use it

    Code:
    UIButton *randomlySelectedButton = (UIButton)[[array objectAtIndex: (random() % [array count])];
    
    You probably want to use arc4random too
     
  3. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #3
    yea.....still no good it doesnt seem to be working after doing it that way, here's what i did, please correct me

    .m file

    Code:
    
    
    -(IBAction)buttonPressed {
    
    UIButton *randomlySelectedButton = (UIButton)[[entireDeckArray
    	   objectAtIndex: (random() % [entireCardDeckArray count])]];
    	  CGPoint faceCenter = randomlySelectedButton.center;
    	  faceCenter.x = 32;
    	  faceCenter.y = 420;
    	  randomlySelectedButton.center = faceCenter;
    }
    
    
    
     
  4. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #4
  5. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #5
    -_- Im trying to make it so there are 3 buttons, and one other button to tap. When the user taps the button, I want a mutable array to have the list of the three buttons button1, button2, button3. After selecting Tay button I want to move it from it's current position to a new position, in this case the point (50,100) after the card is now drawn at the new point, I want to delete itself from the mutable array. That way the next time the user taps the button, it will choose randomly between the other two not selected yet
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    The type-cast shown in red is wrong.

    The variable name shown in green is not the same as the one shown in blue. Logically, they should be identical.

    You're not removing anything from any array. Your first post said this was necessary.

    The position 32,420 isn't what you said; you said 50,100.

    If something is already at the position (32,420), such as a previous button, then you haven't taken overlapping into account.

    Exactly what isn't working? I'm asking because you were asking earlier questions about how to show a hidden element. If your button is hidden, then it won't show. I'm assuming the problem is "It's not showing". If something else isn't working, you have to tell us exactly what that is.
     
  7. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #7
    well can you tell me the code right here of how to do this? this is exactly what i want, ill try more clearly. In interface builder i have four buttons, one that has the IBAction connected, and three others that have special names given to them UIButton *button 1 button2 button3 etc. In interface buider, the IBAction connected button is visible in the center of the screen, and the other 3 buttons with names are all checkmarked HIDDEN and are in the top left corner. Now i want it to be, that when the user taps the button connected to the IBAction:ButtonPressed, i want the device to choose randomly one of the three buttons, button1, button2, and buttton3. After choosing one, i want it to make the button to set itself to not being hidden, and appear at point (32, 420).After that i want the random button that was chosen to delete itself from the list of three buttons, that way, the second time the user taps the IBAction button, it will choose between the two buttons that are still hidden in the corner. and so forth, so imagine instead of 3 buttons theres 50 buttons. And each time this button is pressed it chooses one of the 50 buttons randomly, makes it appear at point (32, 420) and deletes that button's name from the list, so the next time the user presses the button, they cant select the button already moved to point (32, 420)
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    I see very little wrong with that description. The main problem I see is that you didn't code what you described.

    To help clarify what code you should write, I have converted your description to Objective-C comments. I have then placed the comments into two methods, which currently have no code, only comments.

    Code:
    -(void)viewDidLoad
    {
    	//In interface builder i have four buttons, 
    	//one that has the IBAction connected, 
    	//and three others that have special names given to them
    	//UIButton *button 1 button2 button3 etc. 
    	//
    	//In interface buider, the IBAction connected button is visible
    	// in the center of the screen, and the other 3 buttons with names 
    	//are all checkmarked HIDDEN and are in the top left corner. 
    
    	// TASK: put the buttons button1 button2 button3 into an array here.
    }
    
    -(IBAction)buttonPressed
    {
    	//Now i want it to be, that when the user taps 
    	//the button connected to the IBAction:ButtonPressed, 
    	//i want the device to choose randomly one of 
    	//the three buttons, button1, button2, and buttton3. 
    	//
    	//After choosing one, i want it to make the button to 
    	//set itself to not being hidden, and appear at point (32, 420).
    	//
    	//After that i want the random button that was chosen to 
    	//delete itself from the list of three buttons, 
    	//that way, the second time the user taps the IBAction button, 
    	//it will choose between the two buttons that are still hidden in the corner. 
    	//and so forth, 
    
    	// TASK: implement the above description.
    
    	// ADDITIONAL TASK:
    	// If there is a previously chosen button visible at (32, 420),
    	// hide it, move it, or do something so it doesn't interfere
    	// with the newly chosen button as it becomes visible.
    }
    
    When writing your code, do not remove the comments. They serve as the guideline for what the unwritten code should do. After the code is written, they remind you what the code should do, when you're testing and debugging.

    I also suggest that when you get it working for 3 randomly chosen buttons, that you try it next with 5 buttons, not 50.

    If you can't get it working, post complete compilable code for both the .h and the .m files. When you leave code out, everyone else has to guess what you actually wrote.
     
  9. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #9
    Exactly you have it spot on, but that's the thing, I don't really know the full code to just get three working.. I've solved many other problems. The code uibutton *randomlySelectedButton = [[..... Is exactly what I want to do, but when I type that line in it errors on me. But yea if u can help with the 3 or 4 lines I just don't know it'll save me so much time from the reference library
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    If you have code that won't compile, then post the code, and post the exact error message. "It errors on me" is too vague.

    You've already posted some code, that does some of the things you need, but it's unclear to me whether you've compiled it or not. If you have compiled it, it's unclear whether you had errors or not. For example, you posted this code:
    Code:
    UIButton *randomlySelectedButton = (UIButton)[[entireDeckArray
    	   objectAtIndex: (random() % [entireCardDeckArray count])]];
    As I pointed out before, the type-cast is wrong, and there are two variable names where one is more logical. Unfortunately, I can't tell if you've compiled this or not, and if you have, what error messages you got, because you haven't posted any results.

    If you didn't get error messages, you didn't describe what happens when you run it. You must describe what happens, not just say "It doesn't work". If it crashes, say "It crashed" and describe what happened before the crash. If it doesn't crash but only works once, then say that. If it doesn't crash but doesn't display anything, then say that. You must be specific and completely describe what happens. "It errors on me" is simply not enough to work from.
     
  11. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #11
    Revised Help I Have Exactly The Problem

    ok i figured out what my problem is, so here it is. So in interface builder, there are five Round Rect Buttons. One button is labeled, "Press Me" and is located in the exact center of the screen. the other four buttons are labeled "1", "2", "3", "4" and "5" respectively and are all in the top right corner and checkedmarked HIDDEN. The button labeled "Press Me" is connected to an IBAction call named "buttonPressed;" The other three buttons only have unique names to ID them such as "UIButton *button1" "UIbutton *button2", and "UIButton *button3;" "UIButton *button4;" and "UIbutton *button5"; I also have an NSMutablearray, that in it will contain the five buttons that have unique names to them. now i want it to be that when the user the taps the button labeled "Press Me" a call is made to randomly select one of the five buttons from my NSMutableArray, and send it to the point (40, 195), and at the same time set the mode of the random button to setHidden:NO Once the button is at the point (40, 195) i want it to automatically delete itself from the NSMUtableArray, because the second time the user presses the button "Press Me" it will have to choose randomly of the five buttons left over. FOr example, I press the button "Press ME" once and it randomly chooses the round rect button "button3" from the array. The user then sees on the screen the button "button3" appearing at the point (40, 195) the second time i press the button it randomly chooses one of the remaining buttons, button1, button2, button4, and button5. Once it does that it makes that button appear at (40, 185) thats what im trying to do, i need to know how to do this please thanks you here's my code so far :

    h. file
    Code:
    
    @interface.........
    
    {
    UIButton *button1;
    UIButton *button2;
    UIButton *button3;
    UIButton *button4;
    UIButton *button5;
    NSMutableArray *array;
    }
    @property (nonatomic, retain) IBOutlet UIButton *button1;
    @property (nonatomic, retain) IBOutlet UIButton *button2;
    @property (nonatomic, retain) IBOutlet UIButton *button3;
    @property (nonatomic, retain) IBOutlet UIButton *button4;
    @property (nonatomic, retain) IBOutlet UIButton *button5;
    @property (nonatomic, retain) NSMutableArray *array;
    
    -(IBAction)buttonPressed;
    @end
    
    .m file:
    Code:
    
    @implementation...
    @synthesize button1;
    @synthesize button2;
    @synthesize button3;
    @synthesize button4;
    @synthesize button5;
    @synthesize array;
    
    
    -(IBaction)buttonPressed {
    	UIButton *randomlySelectedButton = (UIButton) [[array objectAtIndex: (random() % [array count])] setHidden:NO];
    
    
    	CGPoint faceCenter = randomlySelectedButton.center;
    	faceCenter.x = 40;
    	faceCenter.y = 195;	
    	randomlySelectedButton.center = faceCenter;
    
    [array removeObject:randomlySelectedButton];
    }
    
    now when I try to compile this, i get a red error message underneath the line
    Code:
    	UIButton *randomlySelectedButton = (UIButton) [[entireCardDeckArray objectAtIndex: (random() % [entireCardDeckArray count])]setHidden:NO];
    
    
    the error reads:"Void value not ignored as it ought to be" I'm pretty sure it is referring to my setHidden:NO call. When i remove it and compile it with this line:

    Code:
    	UIButton *randomlySelectedButton = (UIButton) [[entireCardDeckArray objectAtIndex: (random() % [entireCardDeckArray count])]];
    
    i get another red error code and it says "expected ":" before "]" token" please help me!!!
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    That line of code is utterly incomprehensible. It's also wrong, but even if it were right, it would still be incomprehensible.

    Break It Down.

    I don't know why you think adding more and more code to a single malfunctioning line of code is going to make it work. You have several errors in that one line. Until you fix them, all you're doing is adding complexity on top of mistakes, which only serves to hide the mistakes.

    Simplify. Write short statements. Even if it means writing more of them. It's easier to see things that way.

    The red-hilited type-cast is wrong. You cannot cast directly to an object type, only to an object pointer type. The compiler isn't complaining about that error (so far), because you have another error that takes precedence.

    The green-hilited square braces are unnecessary, and in fact are the cause of the error message you reported. You can't just throw square braces around things. They have a specific meaning in Objective-C (and in C). You have to use them correctly. If you didn't notice they were misplaced, it's because you're writing statements that are too long and complicated.
     
  13. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #13
    ok instead of telling me how super wrong i am can you please write out the code for me on how i can achieve this? i tried so clearly to explain what I want my goal to be, but im not sure how to do this at all, the only thing im getting is replies that my code is wrong i knoww :[ so please can you post some code on how you would approach this?
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    Sorry, I'm not writing code for you.

    If you can't work it out from what's posted so far, it suggests you don't understand Objective-C well enough to be embarking on your own made-from-scratch app at this time. You would be better served by reviewing tutorials or books that contain known-working code, and working through any exercises or examples they contain.

    Frankly, it looks like you're in over your head. Not because you can't describe what you want in plain language. Your specification of what you want seems fairly detailed and precise, although it does seem to change somewhat every time you post it.

    You're in over your head for two reasons: you can't translate the specification into simple code, and you can't debug the code you've written. Those two skills aren't just fundamental to programming, they are programming: writing code from specs, and fixing code that doesn't work.

    You need to go back and gain proficiency before you can go forward. The real test of proficiency will be solving the problem yourself, without anyone else writing the code for you.
     

Share This Page