A number buttons with title by achance

Discussion in 'iOS Programming' started by todizara, Jan 30, 2011.

  1. macrumors newbie

    Joined:
    Dec 17, 2010
    #1
    Hi everyone!
    I'd like to display 16 button with a title of number between 1 and 8. The titles will be assigned by chance, but a number will appear only twice.

    My code is as follows. When I compile it, while it is blank on the simulator.

    A line (on the r = r-1) advised me : assignment makes integer from pointer without a cast
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	int myRand, a, r;
    	a=0; //initialize a
    	NSArray * test = [[NSArray alloc] init]; //Table for testing the random
    	for (int i=0; i<=3; i++){
    		for (int j=1; j<=4; j++){
    			r=2;
    			while (r!=0){
    				myRand = 1+arc4random() % 9;
    				for(int k=0; k=a; k++){
    					if(myRand=[test objectAtIndex:k]){
    						r=r-1;
    					}
    				}
    				UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    				myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
    				[myButton setTitle:[NSString stringWithFormat: @"%d",myRand] forState:UIControlStateNormal];
    				myButton.tag =myRand;
    				[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    				[self.view addSubview:myButton];
    				r=1;
    			}
    		}
    	}
    	
    }
    
    I hope as i was clear, thank you to be good enough help me.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    The line with the error is:
    Code:
    if(myRand=[test objectAtIndex:k]){
    What type is myRand? What type does objectAtIndex: return? Are they the same?
     
  3. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    A few odd comments:

    You create an immutable NSArray, don't add anything to it when you create it, and then call objectAtIndex on that array. Can't work.

    Do you mean this:

    Code:
    if(myRand == [test objectAtIndex:k]){
    Do you mean this:

    Code:
    for(int k=0; k == a; k++){
    If you want to display a set of random numbers you should create a list of the numbers you want to display, then shuffle the list, then just iterate over the list.
     
  4. todizara, Jan 30, 2011
    Last edited: Jan 30, 2011

    thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #4
    You're right, I'll do it like that.
    Thank you

    By the way, how to mix the elements of an array as
    NSArray * myList = [NSArray arrayWithObjects: @ "1", @ "2", @ "3", @ "1", @ "4", @ "3", nil];

    I know it in C but not in objective-C.
     
  5. thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #5
    I arranged the array elements.
    I did leave several buttons and display these elements later. The problem is now running the application. He runs around and closes after three seconds.

    Here's code.
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
    	int nb=[myListe count];
    	int ordre[nb];
    	int n, number,a;
    	while (myListe.count > 0) {
    	n = arc4random() % (myListe.count);
    	number = [[myListe objectAtIndex:n] intValue];
    	ordre[myListe.count-1] = number;
    	[myListe removeObjectAtIndex:n];
    	}
    a=0;
    for (int i=0; i<=3; i++){
    	for (int j=1; j<=4; j++){
    	int value=ordre[a];
    	UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    	myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
    	[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
    	myButton.tag =value;
    	[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    	[self.view addSubview:myButton];
    	a++;
    	}
    }
    	[myListe release];	
    }
    
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    What basic debugging have you done to determine the cause? Are there compile-time errors or warnings? What about at run-time? Have you looked at the crash log? Etc.
     
  7. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    This is a valid shuffle algorithm

    Code:
    for (int i = [aMutableArray count]; i > 1; i--)
        [aMutableArray exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
    
     
  8. thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #8
    the problem is on the line
    int value = order [0] Assigned value IS garbage or undefined
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    I don't think your ordre array is being setup as dynamically as you would like it to be.
     
  10. todizara, Jan 31, 2011
    Last edited by a moderator: Jan 31, 2011

    thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #10
    what to do, do you have an idea?

    I use your code, but the application always crash.
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
    for (int i = [myListe count]; i > 1; i--)
        [myListe exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
    int a=0;
    for (int i=0; i<=3; i++){
    	for (int j=1; j<=4; j++){
    	value = [[myListe objectAtIndex:a] intValue];
    	UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    	myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
    	[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
    	myButton.tag =value;
    	[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    	[self.view addSubview:myButton];
    	a++;
    	}
    }
    	[myListe release];	
    }
    
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Okay, but again what debugging have you done? Saying it "always crash" is just far too vague for us to really help. The more relevant information you can provide up front, the quicker we can help you. Perhaps take a gander at Mike Ash's excellent Getting Answers blog article.

    EDIT:

    P.S. The above code won't even compile since value is undeclared.
     
  12. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    This line is wrong:
    Code:
    [myListe release];
    The rest looks more or less OK.

    Regarding debugging:

    Turn on Run > Stop At Objective-C exceptions.
    Run Build and analyze on your code.

    Set a breakpoint at the start of the new code.
    Single step through the code and verify that it's doing what you expect it to do.
    Tell us what line it crashes on.
    What is shown in the debugger console after it crashes?
     
  13. todizara, Feb 2, 2011
    Last edited: Feb 2, 2011

    thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #13
    Thank you for your indication.

    I have another error on the line for (b = 0, b <= 16 b + +)

    Code:
    NSString *buttonName = @"button";
    for (int b=1; b<=16; b++){
    	NSString *num = [NSString stringWithFormat:@"%d",b];
    	NSString *myButton = [NSString stringWithFormat:@"%@%@",buttonName,num];
    	UIButton *myButton;
    }
    
    l'erreur est expected identifier or '(' before 'for'
     
  14. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Well, there are a number of issues with that code snippet, but none of them is related to your error. You should probably give us the code for your entire method.
     
  15. thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #15
    Here is my codes
    Code:
    #import "MemoViewController.h"
    
    @implementation MemoViewController
    
    NSString *buttonName = @"button";
    int b=0;
    for (int k=0; K<16; k++){
    	NSString *num = [NSString stringWithFormat:@"%d",b];
    	NSString *myButton = [NSString stringWithFormat:@"%@%@",buttonName,num];
    	UIButton *myButton;
    	b++;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    	NSMutableArray * myListe = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt:1],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:6],[NSNumber numberWithInt:7], [NSNumber numberWithInt:7], [NSNumber numberWithInt:8], [NSNumber numberWithInt:8],  nil];
    	for (int i = [myListe count]; i > 1; i--)
    		[myListe exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];
    	int a=0;
    	for (int i=0; i<=3; i++){
    		for (int j=1; j<=4; j++){
    			int value = [[myListe objectAtIndex:a] intValue];
    			myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    			myButton.frame = CGRectMake((40+(i*60)),(j*60), 50, 50); 
    			//[myButton setTitle:[NSString stringWithFormat: @"%d",value] forState:UIControlStateNormal];
    			myButton.tag =value;
    			[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    			[self.view addSubview:myButton];
    			a++;
    		}
    	}
    	//[myListe release];	
    }
    
    int tag;
    
    -(IBAction) buttonClicked:(id)sender{
    	tag = [sender tag] ;
    	[self ChangeButton];
    }
    
    -(void) ChangeButton{
    	[myButton setTitle:[NSString stringWithFormat: @"%d",tag] forState:UIControlStateNormal];
    }
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    There are also error on the line where there is myButton saying they are not declared before, but I believe that this problem will be solved if the problem of the loop for is resolved.
    
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Code like which I've highlighted in red must exist within a method. It makes no sense outside of one.
     
  17. thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #17
    Do you have an idea to declared 16 buttons myButton1, myButton2, ..., myButton16. To avoid rewriting UIButton myButton1, ..., UIButoon myButton16
     
  18. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    Yeah, an array would probably be the best solution for that.

    But, I gotta ask: how comfortable are you with the basics of Objective-C / iPhone programming? It seems to me you don't have a good enough understanding of the fundamentals. Perhaps you should consider stepping away from the real coding to go learn those.
     
  19. thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #19
    Thank you for the information, I solve the problem.
     

Share This Page