/

First View Controller


We previously cleared out the view content of the FirstViewController in the storyboard. In this section we'll add a UIButton and TouchUpInside delegate that will interface with the start: and stop: methods defined on the PublishViewController.

1 Select the Main.storyboard from the Project Explorer

2 Drag a Button from the Object Library and drop it onto the View for the First View Controller

3 Assign the Button instance as an IBOutlet in FirstViewController.h header file

    #import <UIKit/UIKit.h>

    @interface FirstViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UIButton *publishButton;

    @end

4 Define a TouchUpInside responder for the Button in FirstViewController.m

    - (IBAction)onPublishToggle:(id)sender {
    }

5 With the FirstViewController.m open, import the PublishViewController.h header and assign a couple attributes on the internal interface

    @interface FirstViewController () {
        PublishViewController *publisher;
        BOOL isPublishing;
    }
    @end

Since the PublishViewController is not linked with the Tabbed Application we have created, we are going to pull in the View Controller and its underlying UIView to the FirstViewController so that we can view and interact with the stream.

1 Override the viewDidLoad: method of FirstViewController, instantiate the PublishViewController add its UIView as a subview underneath the UIButton control previously added

    - (void)viewDidLoad {
        [super viewDidLoad];

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"publishView"];

        CGRect frameSize = self.view.bounds;
        publisher.view.layer.frame = frameSize;
        publisher = (PublishViewController *)controller;

        [self.view addSubview:publisher.view];
        [self.view sendSubviewToBack:publisher.view];
    }

2 If you remember from our PublishViewController when that gets attached, the stream will be created and a preview drawn to the context view. Additionally the view life cycle events will be passed down when switching between tabs in our Tab Application.

3 Modify the onPublishToggle: button delegate to invoke the start: and stop: methods on the PublishViewController instance

    - (IBAction)onPublishToggle:(id)sender {
        if(isPublishing) {
            [publisher stop];
        }
        else {
            [publisher start];
        }
        isPublishing = !isPublishing;
        [[self publishButton] setTitle:isPublishing ? @"STOP" : @"START" forState:UIControlStateNormal];
    }

4 To complete the FistViewController, override the viewDidAppear: and viewDidDisappear: methods to properly handle state

    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [[self publishButton] setTitle:@"START" forState:UIControlStateNormal];
    }

    -(void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
        isPublishing = NO;
    }