warning: invalid receiver type, with setTitle

Discussion in 'iOS Programming' started by rd42, Mar 8, 2010.

  1. rd42 macrumors newbie

    Joined:
    Mar 6, 2010
    #1
    I'm trying to change the text on a UIButton. I've searched and searched and arrived at setTitle being the proper way to change the value of a UIButton when I click on it.

    When I use the code below I get:
    I'm new to this but I think the error is saying that the receiver "paused", a UIButton, is an invalid receiver of the message from the setTitle method.

    Code:
    if(paused)
    { isPaused = NO;
    [pause setTitle:@"paused" forState:UIControlStateNormal];
    }
    
    In the interface
    Code:
    @property(nonatomic, retain) IBOutlet UIButton *pause;
    
    Thank you for any hints or answers.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Please post the code that defines the variables: paused, isPaused, and pause. I've hilited them in red above. We need to see what type each variable is.

    You've shown the property definition for 'pause', but you're not using that property in the posted code, you're using a variable named 'pause'.

    EDIT: It might help if you posted the code for the entire method that has the error, rather than just the isolated code fragment where the error occurred. This would provide a large part of the error's context.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    This is probably the problem: lots of noobs think that they can use
    Code:
    pause
    instead of
    Code:
    [self pause]
    despite these being, potentially, different things.

    OP: try replacing pause with a call to the synthesized method.
     
  4. rd42 thread starter macrumors newbie

    Joined:
    Mar 6, 2010
    #4
    Thanks for the help, here are the full files.

    Interface
    Code:
    #import <UIKit/UIKit.h>
    
    @interface MainViewController : UIViewController <UIAccelerometerDelegate> {
      IBOutlet UILabel *labelX;
      IBOutlet UILabel *labelY;
      IBOutlet UILabel *labelZ;
      
      IBOutlet UIProgressView *progressX;
      IBOutlet UIProgressView *progressY;
      IBOutlet UIProgressView *progressZ;
    
      BOOL isPaused, useAdaptive;	
    
    	
      UIAccelerometer *accelerometer;
    }
    
    @property (nonatomic, retain) IBOutlet UILabel *labelX;
    @property (nonatomic, retain) IBOutlet UILabel *labelY;
    @property (nonatomic, retain) IBOutlet UILabel *labelZ;
    
    @property (nonatomic, retain) IBOutlet UIProgressView *progressX;
    @property (nonatomic, retain) IBOutlet UIProgressView *progressY;
    @property (nonatomic, retain) IBOutlet UIProgressView *progressZ;
    
    @property (nonatomic, retain) UIAccelerometer *accelerometer;
    
    @property(nonatomic, retain) IBOutlet UIButton *pause;
    
    -(IBAction)pauseOrResume:(id)sender;
    
    @end
    
    Implementation
    Code:
    //  Created by Brandon Cannaday on 8/5/09.
    //  Copyright 2009 Paranoid Ferret Productions. All rights reserved.
    //
    
    #import "MainViewController.h"
    
    @implementation MainViewController
    
    @synthesize labelX;
    @synthesize labelY;
    @synthesize labelZ;
    
    @synthesize progressX;
    @synthesize progressY;
    @synthesize progressZ;
    
    @synthesize pause;
    
    @synthesize accelerometer;
    
    /*
     // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
            // Custom initialization
        }
        return self;
    }
    */
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
      [super viewDidLoad];
      
    	isPaused = YES;
    	
      self.accelerometer = [UIAccelerometer sharedAccelerometer];
      self.accelerometer.updateInterval = .1;
      self.accelerometer.delegate = self;
    }
    
    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    	if(!isPaused)
    	{ 
      labelX.text = [NSString stringWithFormat:@"%@%f", @"X: ", acceleration.x];
      labelY.text = [NSString stringWithFormat:@"%@%f", @"Y: ", acceleration.y];
      labelZ.text = [NSString stringWithFormat:@"%@%f", @"Z: ", acceleration.z];
      
      self.progressX.progress = ABS(acceleration.x);
      self.progressY.progress = ABS(acceleration.y);
      self.progressZ.progress = ABS(acceleration.z);
    }
    }
    
    /*
    // 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.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    	self.pause = nil;
    	
    }
    
    -(IBAction)pauseOrResume:(id)sender
    {
    	if(isPaused)
    	{
    		// If we're paused, then resume and set the title to "Pause"
    		isPaused = NO;
    		//pause.title = @"Paused!";
    		//[pause setTitle: @"title" forState: UIControlStateNormal];
    	}
    	else
    	{
    		// If we are not paused, then pause and set the title to "Resume"
    		isPaused = YES;
    		//pause.title = kLocalizedResume;
    	}
    	
    	// Inform accessibility clients that the pause/resume button has changed.
    	UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
    }
    
    - (void)dealloc {
    	[pause release];
        [super dealloc];
    }
    
    
    @end
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Try:
    Code:
    [self.pause setTitle:@"title" forState:UIControlStateNormal];
     
  6. rd42 thread starter macrumors newbie

    Joined:
    Mar 6, 2010
    #6
    Thanks for all the help. Looks like I forgot to declare

    Code:
      IBOutlet UIButton *pause;
    in my interface.

    this works:
    Code:
    [pause setTitle: @"title" forState: UIControlStateNormal];
    
    Thanks again for helping me out.

    Robert
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    It may work, but if you have pause defined as a property, it is recommended that you access it via the accessor methods. self.pause is a shortcut to one of those.
     
  8. rd42 thread starter macrumors newbie

    Joined:
    Mar 6, 2010

Share This Page