Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rd42

macrumors newbie
Original poster
Mar 6, 2010
6
0
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:
warning: invalid receiver type " <------the " is in the warning

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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,421
A sea of green
Code:
if([COLOR="Red"]paused[/COLOR])
{ [COLOR="red"]isPaused[/COLOR] = NO;
[[COLOR="red"]pause[/COLOR] setTitle:@"paused" forState:UIControlStateNormal];
}
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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'.

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.
 

rd42

macrumors newbie
Original poster
Mar 6, 2010
6
0
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
 

rd42

macrumors newbie
Original poster
Mar 6, 2010
6
0
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
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
this works:
Code:
[pause setTitle: @"title" forState: UIControlStateNormal];
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.