Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Specifying arrays for two identical picker components in Xcode

Is this appropriate code for the arrays in a picker with two identical components? Or can both components somehow use the same arrays?
    - (void)viewDidLoad {
self.fromArray = [[NSArray alloc] initWithObjects:
@"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];

self.fromValues = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:12.0],
[NSNumber numberWithFloat:48.0],
[NSNumber numberWithFloat:24.0],
[NSNumber numberWithFloat:26.0],
[NSNumber numberWithFloat:52.0],
[NSNumber numberWithFloat:2080.0], nil];

self.toArray = [[NSArray alloc] initWithObjects:
@"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];

self.toValues = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:12.0],
[NSNumber numberWithFloat:48.0],
[NSNumber numberWithFloat:24.0],
[NSNumber numberWithFloat:26.0],
[NSNumber numberWithFloat:52.0],
[NSNumber numberWithFloat:2080.0], nil];
Also, how would I properly format a calculation using values from both components and a Text Field? This is just not working.
    - (IBAction)Convert:(id)sender {    
float valuein = [[fromValues objectAtIndex:row] floatValue];
float valueout = [[toValues objectAtIndex:row] floatValue];
float input = [inputText.text floatValue];
float result = input * valuein / valueout;
NSString *resultString = [[NSString alloc] initWithFormat: @"$ %@", [toArray objectAtIndex:row]];
}
Post edited by steveald on
Tagged:

Replies

  • BrianSlickBrianSlick Posts: 9,311Tutorial Authors, Registered Users
    Why would you think that you could not use the same array for each component?

    As far as your calculation, you aren't doing anything with "result".
  • stevealdsteveald Posts: 26New Users
    I wouldn't think that. I suspected I could since they are identical. I just don't know how to assign them to both components properly.

    For the calculation, I tried this, but I get undeclared identifier errors with the use of 'row'. It would seem I need to declare 'row', but the single-componenent tutorial I derived that bit of code from looked something like the following, so I'm not sure what's wrong.
    - (NSString *)pickerView:(UIPickerView *)pickerView
    titleForRow:(NSInteger)row
    forComponent:(NSInteger)component
    {
    if (component == 0) {
    return [fromArray objectAtIndex:row];
    }
    return [toArray objectAtIndex:row];
    }

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
    inComponent:(NSInteger)component {
    }

    - (IBAction)Convert:(id)sender {
    float valuein = [[fromValues objectAtIndex:row] floatValue];
    float valueout = [[toValues objectAtIndex:row] floatValue];
    float input = [inputText.text floatValue];
    float result = input * valuein / valueout;
    NSString *resultString = [[NSString alloc] initWithFormat: @"$ %@", [toArray objectAtIndex:row]];
    resultText.text = [NSString stringWithFormat:@"$%6.2f",result];
    }

    I had also tried this, but I got "No visible @interface for 'NSArray' declares the selector 'text'." errors.
    - (IBAction)Convert:(id)sender {
    float valuein = [[fromValues text] floatValue];
    float valueout = [[toValues text] floatValue];
    [resultText setText:[NSString stringWithFormat:@"%6.2f", result]];
    [inputText resignFirstResponder];
    }
    Post edited by steveald on
  • BrianSlickBrianSlick Posts: 9,311Tutorial Authors, Registered Users
    {
    if (component == 0) {
    return [fromArray objectAtIndex:row];
    }
    return [toArray objectAtIndex:row];
    }
    Describe in plain English what you think this code is doing.

    As for the rest, sound like you need to sit down with a good iOS development book. You're missing some fundamentals.
  • stevealdsteveald Posts: 26New Users
    I'm afraid I can't do as you ask. Only to say that I suspected that bit was unnecessary for my purposes when I incorporated the code it came with into my project.

    I've been through 3 iPhone/iOS/Xcode books, as well as myriad resources online from books to tutorials to sample code to how-to videos. The one thing I have missed is Objective-C fundamentals, relying instead on Xcode's ease-of-use and graphical user interface. As a result, I've managed to accomplish all of the many goals I have set for my project - until now. The biggest downside is only knowing everything works, and not always why. Thank you for the advice. I'll keep plugging away at this until I figure it out.
    Post edited by steveald on
  • BrianSlickBrianSlick Posts: 9,311Tutorial Authors, Registered Users
    Alright then, let me help with the English translation. Brush up on those fundamentals, because you absolutely must know this stuff if you want to make apps that are even slightly complex.

    So:
    if (component == 0)
    The "component" value was provided as one of the input variables in the method declaration. If the component value is zero (this means it is the first, or left-most, component), then:
    {
    return [fromArray objectAtIndex:row];
    }
    The "row" value was also provided as an input variable. Go to the array instance variable called "fromArray", select the object in the array that is at the index equivalent to row, and return it. The method exits at this point if this condition was executed.
        return [toArray objectAtIndex:row];
    }
    This code is only run if the component is not zero. This time, go to the array instance variable called "toArray", select the object in the array that is at the index equivalent to the row, and return it.

    Now then, what do you need to change in order for each component to use the same array?
  • stevealdsteveald Posts: 26New Users
    I set self.toArray equal to self.fromArray and now they both display the same objects. I also set self.toValues equal to self.fromValues. Where I think I'm stuck is calling the values individually from each component to use in the calculation.

    I'm afraid any "brushing up" would imply I had been over it before. As I said, my experience is limited to making Xcode do what I need and finding examples of more complex functions that I can modify to suit my needs - not the fundamental basics of how Objective-C works. Probably akin to knowing how to drive a car and change the oil without knowing exactly how to make the engine work. But thanks for the followup.
Sign In or Register to comment.