PDA

View Full Version : A number buttons with title by achance




todizara
Jan 30, 2011, 02:02 PM
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

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



dejo
Jan 30, 2011, 02:15 PM
The line with the error is:
if(myRand=[test objectAtIndex:k]){

What type is myRand? What type does objectAtIndex: return? Are they the same?

PhoneyDeveloper
Jan 30, 2011, 02:50 PM
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:

if(myRand == [test objectAtIndex:k]){

Do you mean this:

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.

todizara
Jan 30, 2011, 02:56 PM
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.
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.

todizara
Jan 31, 2011, 08:09 AM
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.

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.

- (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];
}

dejo
Jan 31, 2011, 09:27 AM
The problem is now running the application. He runs around and closes after three seconds.

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.

PhoneyDeveloper
Jan 31, 2011, 09:37 AM
This is a valid shuffle algorithm

for (int i = [aMutableArray count]; i > 1; i--)
[aMutableArray exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];

todizara
Jan 31, 2011, 12:37 PM
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.

the problem is on the line
int value = order [0] Assigned value IS garbage or undefined

dejo
Jan 31, 2011, 12:55 PM
the problem is on the line
int value = order [0] Assigned value IS garbage or undefined
I don't think your ordre array is being setup as dynamically as you would like it to be.

todizara
Jan 31, 2011, 01:57 PM
I don't think your ordre array is being setup as dynamically as you would like it to be.
what to do, do you have an idea?

This is a valid shuffle algorithm

for (int i = [aMutableArray count]; i > 1; i--)
[aMutableArray exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random() % i];


I use your code, but the application always crash.

- (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];
}

dejo
Jan 31, 2011, 02:36 PM
...but the application always crash.
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 (http://www.mikeash.com/getting_answers.html) blog article.

EDIT:

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

PhoneyDeveloper
Jan 31, 2011, 03:26 PM
This line is wrong:
[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?

todizara
Feb 2, 2011, 11:52 AM
Thank you for your indication.

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


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'

dejo
Feb 2, 2011, 12:07 PM
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.

todizara
Feb 2, 2011, 12:17 PM
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.

Here is my codes

#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.

dejo
Feb 2, 2011, 12:21 PM
Here is my codes

#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.


Code like which I've highlighted in red must exist within a method. It makes no sense outside of one.

todizara
Feb 2, 2011, 12:34 PM
Code like which I've highlighted in red must exist within a method. It makes no sense outside of one.

Do you have an idea to declared 16 buttons myButton1, myButton2, ..., myButton16. To avoid rewriting UIButton myButton1, ..., UIButoon myButton16

dejo
Feb 2, 2011, 02:20 PM
Do you have an idea to declared 16 buttons myButton1, myButton2, ..., myButton16. To avoid rewriting UIButton myButton1, ..., UIButoon myButton16

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.

todizara
Feb 28, 2011, 01:01 AM
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.

Thank you for the information, I solve the problem.