• 0

iOS measurement conversion app with picker


Question

I am building a simple measurement conversion tool (cups to ounces, tsp to tbsp) with a picker (I'd like to use a drop down, but no luck there).

My app launches. The picker populates with options from the datasource.

How ever when I select an option, the app crashes with the following console output


2012-12-10 09:26:41.068 Cooks Helper2[63484:11303] -[UIView text]: unrecognized selector sent to instance 0x7188720
(lldb)
[/CODE]

Here are my classes

[CODE]
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) NSArray *conversionType;
@property (strong, nonatomic) NSArray *conversionFactor;
@property (strong, nonatomic) IBOutlet UIView *picker;
@property (strong, nonatomic) IBOutlet UITextField *originalText;
@property (strong, nonatomic) IBOutlet UILabel *resultText;
- (IBAction)textFieldReturn:(id)sender;
@end
[/CODE]

[CODE]
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize picker, originalText, resultText;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_conversionType = @[@"Ounces to Cups", @"Cups to Ounces",
@"Teaspoons to Tablespoons", @"Tablespoons to Teaspoons"];

_conversionFactor = @[ @0.25f, @4.00f, @0.33f, @3.00f];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView Datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _conversionType.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _conversionType[row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
float factor = [_conversionFactor[row] floatValue];
float original = [originalText.text floatValue];
float result = original * factor;

NSString *resultString = [[NSString alloc] initWithFormat:
@"%.2f", result];
resultText.text = resultString;
}
@end
[/CODE]

Thanks for any help you can offer :)

5 answers to this question

Recommended Posts

  • 0

Okay, so I have the picker issue sorted out, I think. I did the referencing outlet incorrectly. Apparently, I had right clicked just outside of the text field, so it was a UIView object instead of a UITextField. My guess it was trying to pull the text attribute from the viewcontroller itself, which obviously, there is none. I have "remedied" that by correcting a new RO to viewcontroller.h and deleteing the old one, but now the application won't launch. Crashes with the following


Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x13335740> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key originalText
[/CODE]

  • 0

You have an extra hook set up in your nib somewhere. Maybe you renamed an outlet property or something. Go to the connections tab (last one) on the right in IB and you should see whats up.

Thanks for the reply! Figured it out late last night. Thanks for your help!

  • 0

No problem. Is there a particular reason you are declaring a property and then only using the instance variable. For example:


- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _conversionType.count;
}
[/CODE]

Ideally would be

[CODE]
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.conversionType.count;
}
[/CODE]

This uses the getter created by the system when you declare a property.

  • 0

No problem. Is there a particular reason you are declaring a property and then only using the instance variable. For example:


- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _conversionType.count;
}
[/CODE]

Ideally would be

[CODE]
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.conversionType.count;
}
[/CODE]

This uses the getter created by the system when you declare a property.

No reason really. I'm actually pretty new to iOS programming, and used a tutorial online for parts of this. That was how it was written in the tutorial. Thanks for the input though. I appreciate it!

This topic is now closed to further replies.
  • Posts

    • Just the price of doing business. The scamble to pull as much from the web as possible is happening, and it's happening before a case like this changes how or what is legal do to with AI in terms of data harvesting. But even then as we've seen with the likes of Google who ignore cookie requests and just accept the fact they'll get fined, it's built into their business price model now. AI is here, its not going away. Their reward if any from the court case would be best suited to trying to incorprate AI or licence their end points as authentic human verified content. The problem is, as we've seen these same news papers are using AI themselves.
    • Which finger's fingernail are we talking about? I can see how not having this info can lead to massive differences in interpretation.
    • This Chinese company is reportedly developing a feature Apple and Samsung can only dream of by Hamid Ganji While companies like Apple and Samsung have been relatively conservative with their devices’ battery capacities in recent years, Chinese manufacturers have taken the competition to the next level by introducing significantly larger batteries. However, the latest report from China suggests that a local company may already be developing a smartphone with a whopping 14,000mAh battery. Chinese leaker Digital Chat Station claimed on Weibo that a smartphone maker is developing a device with a 14,000mAh battery. If true, it would be the largest battery ever used in a smartphone and could, in theory, provide up to a week of battery life on a single charge. The leaker did not reveal the name of the company behind the device, but there are some clues. This week, HONOR unveiled the X80 Pro Max in China with an 11,000mAh battery and 90W wired charging support. The company also launched the Honor Win in January, which packs a 10,000mAh battery. HONOR, a former subsidiary of Huawei, has a proven track record of developing smartphones with unusually large batteries. However, other Chinese brands, including Xiaomi, have also launched devices such as the Xiaomi 17 Pro Max with 7,500mAh batteries. Though Chinese users on Weibo also believe the company behind the new battery is HONOR. Interestingly, Digital Chat Station said the device with the 14,000mAh battery weighs around 220 grams, making it lighter than the Apple iPhone 17 Pro Max (233 grams) and slightly heavier than the Samsung Galaxy S26 Ultra (214 grams). The iPhone 17 Pro Max currently packs a 5,088mAh battery in eSIM-only versions, while the Galaxy S26 Ultra features a 5,000mAh battery. Neither device is expected to see a dramatic increase in battery capacity in its next-generation successor. So when it comes to battery comparison, Chinese brands are unbeaten. HONOR smartphones are currently available in the EU, but the Chinese brand has no official presence in the United States due to restrictions imposed by the U.S. government.
  • Recent Achievements

    • First Post
      kinowa earned a badge
      First Post
    • Rookie
      krychek57 went up a rank
      Rookie
    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
    • Dedicated
      Scoobystu earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      461
    2. 2
      +Edouard
      171
    3. 3
      PsYcHoKiLLa
      136
    4. 4
      Michael Scrip
      78
    5. 5
      Xenon
      77
  • Tell a friend

    Love Neowin? Tell a friend!