CS175
Chris Pollett
Sep 24, 2014
//in the demo's ViewController.h - (IBAction)sliderChanged:(id)sender;
// in the demo's ViewController.m
-(IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
int sliderPos = (int)(slider.value + 0.5f);
NSString *text = [[NSString alloc] initWithFormat:@"%d", sliderPos];
_sliderLabel.text = text;
}
and then we hook it up in IB as well. We set it up as the result of the Value Changed event for the UISlider.
#import <UIKit/UIKit.h> #define kShowSegmentIndex 0 @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField *numberField; @property (weak, nonatomic) IBOutlet UILabel *sliderLabel; @property (weak, nonatomic) IBOutlet UIView *switchView; @property (weak, nonatomic) IBOutlet UISwitch *leftSwitch; @property (weak, nonatomic) IBOutlet UISwitch *rightSwitch; -(IBAction)textFieldDoneEditing: (id)sender; -(IBAction)backgroundClick:(id)sender; -(IBAction)sliderChanged:(id)sender; -(IBAction)switchChanged:(id)sender; -(IBAction)toggleShowHide:(id)sender; @end
-(IBAction)switchChanged:(id)sender
{
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[_leftSwitch setOn:setting animated:YES];
[_rightSwitch setOn:setting animated:YES];
}
-(IBAction)toggleShowHide:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSInteger segment = segmentedControl.selectedSegmentIndex;
if(segment == kShowSegmentIndex) [_switchView setHidden:NO];
else [_switchView setHidden:YES];
}
After writing this code we need to set up each of the outlets and actions in Interface Builder.
@protocol MyDelegate // list optional methods -(void) optionalMethod1; // ... @required //list required methods to implement protocol -(void) requiredMethod1:(id)myThing; //... @end;
@interface MyClass : NSObject <MyDelegate> //... @end;
#import#define kShowSegmentIndex 0 @interface ViewController : UIViewController <UIActionSheetDelegate> @property (retain, nonatomic) IBOutlet UITextField *nameField; @property (retain, nonatomic) IBOutlet UITextField *numberField; @property (retain, nonatomic) IBOutlet UILabel *sliderLabel; @property (retain, nonatomic) IBOutlet UISwitch *rightSwitch; @property (retain, nonatomic) IBOutlet UISwitch *leftSwitch; @property (retain, nonatomic) IBOutlet UIView *switchView; -(IBAction)textFieldDoneEditing: (id)sender; -(IBAction)backgroundClick:(id)sender; -(IBAction)sliderChanged:(id)sender; -(IBAction)switchChanged:(id)sender; -(IBAction)toggleShowHide:(id)sender; // we hook up doStuff with a new button we add to our view -(IBAction)doStuff:(id)sender; @end
Here is the code for the doStuff method which we set up to be called on a Touch Up Inside event on a new button with label DoStuff we add to out storyboard.
-(IBAction)doStuff:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way!"
destructiveButtonTitle:@"Yes, I'm Sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
Here now is the code we add to implement the UIActionSheetDelegate protocol (again, this will be in ViewController.m)
-(IBAction)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(!(buttonIndex == [actionSheet cancelButtonIndex]))
{
NSString *msg = nil;
if(_nameField.text.length > 0)
{
msg = [NSString stringWithFormat:
@"You can breathe easy, %@, everything went okay.",
_nameField.text ];
}
else
{
msg = @"You can breathe easy, everything went okay.";
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Something was done"
message:msg
delegate:self
cancelButtonTitle:@"Phew!"
otherButtonTitles:nil];
[alert show];
}
}
<script type="text/javascript"> <!-- hide from old browsers -- code -- // --> </script>
<script type="text/javascript" src="myscript.js" />
return x; // has the effect of just return!
x = 5.5; y = x.toString();
document.write("Hi there!");
document.writeln("This strings has a new line after it!");
<a href="javascript:alert('hi there')">Hi there alert</a>
Hi there alert
<button onclick="alert('hi there')">Hi There!</button>
<button id="yo">Yo!</button>
<script>
document.getElementById("yo").onclick = function() {alert("yo");}
</script>
var my_object = new Object();
my_object.make= "V6" /* would then give a property make a value. */ //can access as p = my_object["make"] q = my_object.make
my_object.subObject = new Object();
for(var prop in my_object){...}
var myArr = new Array(1, 2, "hello") var myArr = new Array(100); var myArr = [1,2,3]; //to access myArr[0] //to determine length myArr.length
Below is an example of the syntax for declaring a function in Javascript
function swap(i, j, a)
{
var tmp=a[i]; /* explicitly defined variables
have scope within the function
if I had declared the variable
implicitly it would have global scope */
a[i] = a[j]; a[j] = tmp;
}
swap(10, 5, b);
var b = swap;
swap = function(i, j, a) { /* code above */}
function swap()
{ var i = this.arguments[0], j=this.arguments[1], a=this.arguments[2];
//same code as before
}
function car(new_make, new_model, new_year)
{
this.make = new_make;
this.model = new_model;
this.year = new_year;
}
my_car = new car("Ford", "Contour SVT", "2000");