More iPhone Controls, Javascript




CS175

Chris Pollett

Sep 24, 2014

Outline

Introduction

Sliders

Slider Screenshot

Slider Demo Screenshot

Segmented Controls and Switches

Segmented Control and Switch Header

#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

Segmented Control and Switch Implementation

-(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.

Switch Segmented Control Screenshot

Demo of Switch and Segmented Controls

Action Sheets and Alerts

How we modify our Controller's Header

#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

Code for doStuff

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];
}

Code for Protocol Method

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];
    }
}

Action Sheet in Action

Image when actionsheet invoke Image after destructive button title clicked

Javascript

Understanding Javascript

Basic Syntax

Ways to invoke Javascript

Remarks on Ways to invoke Javascript

Objects

Arrays

Functions in Javascript

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;
}

Constructors