Help with crashing app

Discussion in 'iOS Programming' started by greg1067, Dec 24, 2009.

  1. greg1067 macrumors newbie

    Joined:
    Nov 30, 2009
    #1
    Hello to all. And first of all Happy Holidays to all. I am having problems with my app crashing. I got the UIPicker working and I am able to use the selected data in my app. See my thread UIPicker help for further details on that. But now when I click the button my app crashes with this error in the console.

    2009-12-24 23:17:43.609 ICookie[10353:207] *** -[UITextField setValue:]: unrecognized selector sent to instance 0x3b0cee0
    2009-12-24 23:17:43.610 ICookie[10353:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UITextField setValue:]: unrecognized selector sent to instance 0x3b0cee0'

    I did not include the stack. If that would help diagnose this I can include it. Here is the method that is connected to the button. When I comment all lines except the NSLog command it runs fine. I do not see where in this the problem lies.

    Code:
    -(IBAction) calc:(id)sender
    {
    
    	float converttemp = [firsttextbox floatValue]; 
    	
    	if ([from objectAtIndex:[calcPicker selectedRowInComponent:0]] ==  @"Teaspoon" && [to objectAtIndex:[calcPicker selectedRowInComponent:1]] == @"Tablespoon") {
    		
    		float tsp_tbsresult;
    		tsp_tbsresult = converttemp / 3;
    		outputtxt = tsp_tbsresult;
    		[secondtextbox  setValue:(float) outputtxt];  
    
    		
    		NSLog(@"Teaspoon and tablespoon have been selected");
    	
    	
    	
    
    }
    	
    	
    
    	
    }
    
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Like the error says, a UITextField doesn't have a setValue: method. You need to use its text property instead. Additionally, you need to convert your float to a string:

    Code:
    secondtextbox.text = [NSString stringWithFormat:@"%f", tsp_tbsresult];
     
  3. greg1067 thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #3
    Thanks for the response. I copied and pasted your suggestion and the app does not crash but I get a compilation error at that line:
    "Request fro member 'text' in something not a structure or union"
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  5. greg1067 thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #5
    ICookieViewController.h

    Code:
    //
    //  ICookieViewController.h
    //  ICookie
    //
    //  Created by **** on 12/21/09.
    //  Copyright __MyCompanyName__ 2009. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    float inputtxt;
    float outputtxt;
    
    @interface ICookieViewController : UIViewController
    
    
    
    
    
    
    
    <UIPickerViewDataSource, UIPickerViewDelegate>
    {
    
    	IBOutlet UIPickerView *calcPicker;
    	NSArray* from;
    	NSArray* to;
    	IBOutlet id firsttextbox;
    	IBOutlet id secondtextbox;
    	
    	
    }
    
    @property (nonatomic, retain) UIPickerView* calcPicker;
    @property (nonatomic, retain) UITextField* firsttextbox;
    @property (nonatomic, retain) UITextField* secondtextbox;
    //@property (nonatomic, retain) IBOutlet UITextField * doneButtonPressed;
    
    -(IBAction)calc:(id)sender;
    
    @end
    
    
    ICookieViewController.m

    Code:
    //
    //  ICookieViewController.m
    //  ICookie
    //
    //  Created by **** on 12/21/09.
    //  Copyright __MyCompanyName__ 2009. All rights reserved.
    //
    
    #import "ICookieViewController.h"
    
    @implementation ICookieViewController
    @synthesize calcPicker;
    @synthesize firsttextbox;
    @synthesize secondtextbox;
    
    
    
    /*
    // The designated initializer. Override to perform setup that is required before the view is loaded.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
            // Custom initialization
        }
        return self;
    }
    */
    
    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
    }
    */
    
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    	
    	from = [[NSArray alloc] initWithObjects:@"Teaspoon", @"Tablespoon", @"Ounce", @"Cup", @"Pint", @"Quart", @"Gallon", @"Milliliter", @"Liter", nil];
    	
    	to = [[NSArray alloc] initWithObjects:@"Teaspoon", @"Tablespoon", @"Ounce", @"Cup", @"Pint", @"Quart", @"Gallon", @"Milliliter", @"Liter", nil];
    	
    }
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
    	return 2;
    }
    
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
    	if (component == 0) {
    		return [from count];
    	}
    else {
    	return [to count];
    	}
    }
    
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */
    
    - (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.
    }
    
    -(IBAction) calc:(id)sender
    {
    
    	float converttemp = [firsttextbox floatValue]; 
    	
    	if ([from objectAtIndex:[calcPicker selectedRowInComponent:0]] ==  @"Teaspoon" && [to objectAtIndex:[calcPicker selectedRowInComponent:1]] == @"Tablespoon") {
    		
    		float tsp_tbsresult;
    		tsp_tbsresult = converttemp / 3;
    		outputtxt = tsp_tbsresult;
    		[secondtextbox  setValue:(float) outputtxt]; //this is how I had the result returned to a textbox in an earlier version
    		//[secondtextbox.text] = [NSString stringWithFormat:@"%f", tsp_tbsresult];
    
    		
    		NSLog(@"Teaspoon and tablespoon have been selected");
    	
    	
    	
    
    }
    	
    	
    
    	
    }								
    
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{
    	switch (component) {
    		case 0:
    			return [from objectAtIndex:row];
    		case 1:
    			return [to objectAtIndex:row];
    	}
    	return nil;
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
    	[calcPicker release];
    	[from release];
    	[to release];
        [super dealloc];
    }
    
    @end
    
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Code:
    IBOutlet id firsttextbox;
    IBOutlet id secondtextbox;
    Don't use id here, because you know that these are UITextFields, so they should be declared as so:
    Code:
    IBOutlet UITextField *firsttextbox;
    IBOutlet UITextField *secondtextbox;
     
  7. greg1067 thread starter macrumors newbie

    Joined:
    Nov 30, 2009
    #7
    Hello again. Still having issues casting the firsttextbox value to float so I can do the math. Been away from it for a few days so haven't been scratching my head. But now I am back at it. This is getting frustrating for a noob. :( What is so maddening is that the very first declaration:

    Code:
    float converttemp =  [firsttextbox floatValue]; 
    
    is how I declared and cast the value in a straight xcode project. But now trying to do it in an iPhone environment it won't run. At this point I'm not sure if my object connections are correct anymore of if they were correct to begin with. The error associated with the above line is Incompatible types in initialization.
     

Share This Page