picker view help!

Discussion in 'iOS Programming' started by geiger10d, Mar 3, 2011.

  1. geiger10d macrumors member

    Joined:
    Jan 31, 2011
    #1
    hey guys, in my app im making a weight tracker, so i made a picker view
    it works fine, but when a picker is holding numbers, i need to know a way to have a picker componet hold numbers from like 0-1000 is there a way to do that? or do i actually have to put @"1",@"2"etc all the way up to 1000! please help!!

    heres my code

    Code:
    - (void)viewDidLoad {
    	
    	NSArray *breadArray = [[NSArray alloc] initWithObjects:@"white", @"Whole Wheat", @"rye", @"SourDough", @"Seven Grain", nil];
    	self.breadTypes = breadArray;
    	[breadArray release];
    	
    	NSArray *fillingArray = [[NSArray alloc] initWithObjects:@"Ham", @"Turkey", @"Penut Butter", @"Tuna", @"Sevvv",@"adfadsf", @"asdasd", nil];
    	self.fillingTypes = fillingArray;
    	[fillingArray release];
    	
    	NSArray *weightArray = [[NSArray alloc] initWithObjects:@"LBS", @"KILO", nil];
    	self.weightTypes = weightArray;
    	[weightArray release];
    	
    	
        [super viewDidLoad];
    }
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Code:
    int max =1000;
    NSMutableArray *myArray = [NSMutableArray arrayWithCapacity:max];
    int current = 1;
    while(current<=max)
    {
    [myArray addObject:[[NSNumber numberWithInt:current] stringValue];
    current++
    }
    
    Seems pretty simple?

    p.s. I just typed this into the reply box: it's not tested etc
     
  3. geiger10d thread starter macrumors member

    Joined:
    Jan 31, 2011
    #3

    why do i have to change it to a mutable array?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Well, you can't add objects to an immutable array can you?
     
  5. geiger10d thread starter macrumors member

    Joined:
    Jan 31, 2011
    #5

    oh yes im sorry, but that code did not work, the last two lines of it i think were off starting with {
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    1) Learn how to report issues. Saying it "did not work" is about as useful as a chocolate fireguard. If it didn't compile what are the errors? On what line? If it did not do what was expected then what did it do? How did this differ from expectations

    2) You should be able to fix it yourself.
     
  7. geiger10d thread starter macrumors member

    Joined:
    Jan 31, 2011
    #7
    easy...

    Code:
    	
    	{
    		[myArray addObject:[[NSNumber numberWithInt:current] stringValue];
    		 current++
    		 }
    	
    
    error i got was m:79: error: expected ']' before ';' on those lines.


    if i knew how to fix it... i wouldnt be asking.. if you dont want to help me out then you dont have to reply.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Uh hu so you need to add a ] to balance out the [. There are three [ and only 2 ].
     
  9. geiger10d thread starter macrumors member

    Joined:
    Jan 31, 2011
    #9
    Code:
    
    int max =1000;
    	NSMutableArray *myArray = [NSMutableArray arrayWithCapacity:max];
    	int current = 1;
    	while(current<=max)
    	{
    		[myArray addObject:[[NSNumber numberWithInt:current] stringValue]];
    		 current++
    	}
    	
    
    
    is that in the right spot? bcausae i get a error with the } and the end of it saying /er.m:81: error: expected ';' before '}' token
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    As I said I just typed it straight in. Every statement must end in ; current++ is a statement.
     
  11. geiger10d thread starter macrumors member

    Joined:
    Jan 31, 2011
    #11
    thanks for your help, i switched it around a little bit by using this code insted

    Code:
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for(int i = 0; i < 1001; i++){
        [array addObject:[NSString stringWithFormat:@"%d", i]];
    }
    self.numberArray = array;
    [array release];
    
    
     

Share This Page