Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:26:54 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: slider control  (Read 1556 times)
MikeF
Newbie
*
Posts: 49


Email
« on: February 14, 2011, 11:38:05 AM »

Hello again, i am experimenting with the 'slide control'. In the example Iím looking at there is a slider control which updates a numerical label immediately to the left of the slider on the main view (Iím sure you've all seen this before). Itís part of another newbie iphone program taken from another book, and one particular line of code is confusing me. It's within the slider action method, and within this list of commands is some kind of cast command? I understand how casting works with commands like
 
int t= (int) floatValue/7;  // sort of thing...
 
but how does the command (in bold) work?

-(IBAction) sliderChanged:(id)sender {
   UISlider *slider =(UISlider *)sender;
   int progressAsInt =(int)(slider.value+0.5f);
   NSString *newText = [[NSString alloc] initWithFormat:@"%d",progressAsInt];
   sliderLabel.text =newText;
   [newText release];
 
 }
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: February 14, 2011, 07:03:25 PM »

The IBAction method is passed the actual object that triggered the method to be called.   If just a slider is going to be connected to this method, you could  have declared the method like so:

Code: (Objective-C)
-(IBAction) sliderChanged: (UISlider *0 slider {
    ...
}
That's how I would do it.  The example you've shown, which declares the argument to be an id type, is a more general way of declaring the argument to the method, implying that any type of object could be passed  as the argument.  The cast and assignment that's performed

Code: (Objective-C)
 UISlider *slider = (UISlider *) sender;
just copies the reference to the object into a statically-typed variable (refer to Chapter 9 in the text).  The casting just makes the type of data on the right match the type on the left side.   In theory, some conversion could be taking place, but in reality the reference (pointer) is just copied from the variable sender to the variable slider.
  
Cheers,

Steve
« Last Edit: February 14, 2011, 07:05:57 PM by skochan » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ÔĹ© 2009 classroomM.com. All rights reserved.