Computer Music: Musc216
Objective-C Code Examples


Declaring Variables

NSString *myString;
myString = @"Hello World!";

int myValue;
myIntValue = 10;

float myValue;
myFloatValue = 99.1234;


Put a Value in a Field

[myField setFloatValue:myValue];
[myField setIntValue:myValue];
[myField setStringValue:myValue];

Get a Value from a Field

myValue = [myField  floatValue]; // Note the lowercase for "float" on floatValue
myValue = [myField  intValue]; // Note the lowercase for "int" on intValue
myValue = [myField  stringValue]; // Note the lowercase for "string" on stringValue

Get a Value from a Field in another Class

[Note done yet.]


Test for a Selection in a Popup Button

NSString *title = [myPopupButton titleOfSelectedItem];
if ([title isEqualToString:@"1stItem"])
  do something here;
else if ([title isEqualToString:@"2ndItem"])
  do something here;
else if ([title isEqualToString:@"3rddItem"])
  do something here;


Set Values in Fields at Startup

-(void)awakeFromNib
{

   [myField setIntValue:110];
   [myField setFloatValue:110.999];
   [myField setStringValue:@"My string"];

}


Set Values in Fields with a Slider

Code Comment

-(IBAction)setMyValue:(id)sender
{

   myValue = [sender intValue]; // Get the current value
   myNewValue = myValue*2; // See comment
   if (myNewValue < 20)
      myNewValue = 20;
   [myField setIntValue:myNewValue];

}

The value of a slider is in the range 0 - 100. So, if you want the value to be 0 - 200, multiply the current value by 2.

More Sample Code:

ForNext Loops

IfThenElse

NSLog

Main Page