• 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

    • I think you meant the "ntfs3" driver, but yes there have been a lot of fixes for it in this release and previous releases, not 100% sure if the issue you mentioned is fixed though. In any case, the new "ntfs" driver in 7.1 doesn't have that issue (at least, no reports of such have come thru), but your kernel needs to explicitly enable support for the new driver first (like how CachyOS kernel has it), and you need to edit your mount points in /etc/fstab to use "ntfs" instead of the other drivers.
    • Epic Games says Unreal Engine 6 will help developers "build content faster" using AI models by Pulasthi Ariyasinghe Epic Games is rolling out the latest major update to Unreal Engine 5 today, and at the same time, the company also dropped some information on the next-generation version of the product, Unreal Engine 6. This was already revealed a few weeks ago alongside the new Rocket League upgrade reveal. The company says it is combining the features of Unreal Engine and Unreal Editor for Fortnite to create this new version of its popular media creation tool. On top of creating entire games, the new engine will also focus on letting developers operate large-scale live service titles more easily, whether by shipping content into their own ecosystems or into Fortnite. The use of large language models is also mentioned here, with Epic saying it will be a core part of the engine. "We see LLMs, generative AI models, and tools like Claude and Codex playing a central role in helping you build content faster while maintaining the creative control you need," adds the company. Here is the rundown of what's new about version 6 of Unreal Engine: With all these changes to the programming model, portability upgrades, and generative AI integration, Epic says the new version of the engine will "change a lot about how games are made." The company aims to ship Unreal Engine 6 into early access in late 2027, with a full release planned for 12-18 months later. Epic Games also dropped a lengthy blog post about the new Unreal Engine 5.8 update for game developers over here. The release is focused on delivering better performance, customization, and streamlined workflows for development teams. This will be the final major update for this version of the engine before Epic switches to focus fully on Unreal Engine 6's early access launch.
    • Watch Louis Rossmann's recent experience on YouTube about trying to get a warranty replacement from Samsung. It's crazy.
    • That is the thing, how many of these people don't realise they are using AI? If they use Google Search they have no choice but to use AI. So yes, maybe half of American adults do use and I expect a lot of Uk adults do to, but I bet most of them don't realise it. Myself, i avoid the rubbish.
    • They use FREE AI. They aren't paying for this meme-generating slopware...
  • Recent Achievements

    • One Month Later
      Vincian earned a badge
      One Month Later
    • First Post
      Jocimo earned a badge
      First Post
    • Week One Done
      suprememobiles48 earned a badge
      Week One Done
    • One Month Later
      Windows Guy earned a badge
      One Month Later
    • One Month Later
      Prasann earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      500
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      88
    4. 4
      Steven P.
      68
    5. 5
      neufuse
      65
  • Tell a friend

    Love Neowin? Tell a friend!