archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • An Example Interactive WatchKit App using Actions and Outlets - Techotopia
    interface objects within the corresponding storyboard scene Outlets can be created visually within Xcode by using Interface Builder and the Assistant Editor panel To establish outlets begin by loading the Interface storyboard file into the Interface Builder tool Within Interface Builder click on the scene so that it is highlighted before displaying the Assistant Editor by selecting the View Assistant Editor Show Assistant Editor menu option Alternatively it may also be displayed by selecting the center button the one containing an image of interlocking circles of the row of Editor toolbar buttons in the top right hand corner of the main Xcode window as illustrated in the following figure Figure 5 5 In the event that multiple Assistant Editor panels are required additional tiles may be added using the View Assistant Editor Add Assistant Editor menu option By default the editor panel will appear to the right of the main editing panel in the Xcode window For example in Figure 5 6 the panel to the immediate right of the Interface Builder panel is the Assistant Editor marked A Figure 5 6 By default the Assistant Editor will be in Automatic mode whereby it automatically attempts to display the correct source file based on the currently selected item in Interface Builder If the correct file is not displayed use the toolbar along the top of the editor panel to select the correct file The small instance of the Assistant Editor icon in this toolbar can be used to switch to Manual mode allowing the file to be selected from a pull right menu containing all the source files in the project Figure 5 7 Make sure that the InterfaceController swift file is displayed in the Assistant Editor and establish an outlet for the top most label by Ctrl clicking on the Label object in the scene and dragging the resulting line to the area immediately beneath the class InterfaceController declaration line in the Assistant Editor panel as shown in Figure 5 8 Figure 5 8 Upon releasing the line the configuration panel illustrated in Figure 5 9 will appear requesting details about the outlet to be defined Figure 5 9 eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Since this is an outlet the Connection menu should be set to Outlet The type and storage values are also correct for this type of outlet The only task that remains is to enter a name for the outlet so in the Name field enter amountLabel before clicking on the Connect button Repeat the above steps to establish an outlet for the second Label object named tipLabel Once the connections have been established review the InterfaceController swift file and note that the outlet properties have been declared for us by the Assistant Editor import WatchKit import Foundation class InterfaceController WKInterfaceController IBOutlet weak

    Original URL path: http://www.techotopia.com/index.php/An_Example_Interactive_WatchKit_App_using_Actions_and_Outlets (2016-02-13)
    Open archived version from archive


  • An Overview of WatchKit Tables - Techotopia
    3 The row controller within the table is a template for the row controllers that will be created within the interface controller To design the row simply drag and drop user interface objects from the Object Library onto the row and resize the items to achieve the desired layout In Figure 6 4 for example the template row controller contains an image and label Figure 6 4 It is important to be aware that the appearance of the visual elements in a row is generally defined at runtime by the interface controller using the outlines declared in the row controller class Any attributes set within the storyboard will serve as the default attributes for the visual elements and will appear in the app at runtime unless overridden in the interface controller The row controller template must also be assigned an identifier which will be referenced in the interface controller code when instances of that row type are created during initialization The identifier is configured by selecting the row controller item in the Document Outline panel and entering a suitable identifier name into the Identifier field of the Attributes Inspector panel Figure 6 5 Adding the Row Controller Class to the Extension As previously outlined each row controller type must have a corresponding row controller class file within the app extension This must be a subclass of NSObject and can be created using the following steps 1 Locate the WatchKit Extension folder within the Project Navigator panel and Ctrl click on it 2 From the resulting menu select the New File menu option 3 In the new file template panel select Source listed under iOS in the left hand panel and Cocoa Touch Class from the main panel before clicking Next 4 Enter a name for the class into the Class field and select NSObject from the Subclass of menu before clicking Next 5 Click Finish to add the new class to the extension Associating a Row Controller with a Row Controller Class Before the table can be displayed the row controller in the scene needs to be associated with the corresponding row controller class residing within the extension This is achieved by selecting the row controller in the Document Outline panel displaying the Identity Inspector View Utilities Show Identity Inspector and selecting the row controller class name from the Class menu Creating Table Rows at Runtime The interface controller class for the scene containing the table is responsible for configuring the table and creating the rows during the initialization phase of the WatchKit app launch process The interface controller will need an outlet connected to the table instance in the scene on which it will call either the setNumberOfRows withRowType or setRowTypes methods setNumberOfRows Used when all of the rows to be created in the table are of the same row type This method takes as parameters the number of rows to be created and the Identifier string for the row controller type as defined in the Attributes Inspector setRowTypes

    Original URL path: http://www.techotopia.com/index.php/An_Overview_of_WatchKit_Tables (2016-02-13)
    Open archived version from archive

  • A WatchKit Table Tutorial - Techotopia
    to switch to Manual mode allowing the file to be selected from a pull right menu containing all the source files in the project From this menu select Manual TableDemoApp TableDemoApp Watchkit Extension MyRowController swift Figure 7 7 With the MyRowController swift file displayed in the Assistant Editor panel Ctrl click on the image object in the storyboard scene and drag the resulting line to a location beneath the class declaration line in the Assistant Editor panel Release the line and establish an outlet connection named myImage Repeat the above step to establish an outlet from the label object named myLabel Remaining within the Assistant Editor edit the content of the file to import the WatchKit framework instead of UIKit On completion of these steps the MyRowController swift file should read as follows import WatchKit class MyRowController NSObject IBOutlet weak var myImage WKInterfaceImage IBOutlet weak var myLabel WKInterfaceLabel With these steps completed the next step is to create some data to be displayed and to add initialization code to the interface controller to display the rows at runtime Connecting the Table Outlet In the course of initializing the table it will be necessary to access the table instance from within the interface controller class which will in turn require the establishment of an outlet With the Interface storyboard file selected and the Document Outline panel displayed open the Assistant Editor panel and make sure that it is displaying the content of the InterfaceController swift file Ctrl click on the Table entry in the Document Outline panel and drag the resulting line to a position immediately beneath the class declaration line in the Assistant Editor panel Figure 7 8 Release the line and establish an outlet named myTable in the resulting connection dialog Creating the Data Clearly the rows within the table consist of an image and a label The next step is to add some data to the project to serve as content for these two objects This will take the form of two arrays declared within the interface controller class consisting of the names of the image files to be displayed on the Image object and strings to display on the Label object Select the InterfaceController swift file and edit it to add these two arrays as follows import WatchKit import Foundation class InterfaceController WKInterfaceController IBOutlet weak var myTable WKInterfaceTable let stringData Warm up Cardio Weightlifting Core Bike Cooldown let imageData walking treadmill weights core bikeriding cooldown Next a method needs to be added to the class to initialize the table with the content of the data arrays Remaining within the InterfaceController swift file implement this method as follows func loadTable myTable setNumberOfRows stringData count withRowType MyRowController for index labelText in enumerate stringData let row myTable rowControllerAtIndex index as MyRowController row myLabel setText labelText row myImage setImage UIImage named imageData index eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials

    Original URL path: http://www.techotopia.com/index.php/A_WatchKit_Table_Tutorial (2016-02-13)
    Open archived version from archive

  • A WatchKit Table Navigation Tutorial - Techotopia
    the storyboard should resemble Figure 8 2 Figure 8 2 With the new scene added to the storyboard and selected so it is highlighted in blue display the Attributes Inspector and enter DetailInterfaceController into the Identifier field By default labels are configured to display a single line of text which will be clipped if the text exceeds the width of the watch display To avoid this problem configure the label to wrap the text over multiple lines by selecting the label in the second scene displaying the Attributes Inspector and increasing the Lines attribute to 5 Figure 8 3 The design of the second scene is now complete The next step is to add an interface controller to go with it Adding the Detail Interface Controller Add the interface controller for the detail scene by locating the TableDemoApp WatchKit Extension entry in the Xcode Project Navigator panel Ctrl clicking on it and selecting New File from the menu In the template panel select the options to create an iOS Cocoa Touch Class source file and click Next Name the new class DetailInterfaceController and configure it to be a subclass of WKInterfaceController Click Next make sure that the file is to be generated into the extension folder then click Create Load the Interface storyboard file into Interface Builder Select the second scene so that it highlights in blue display the Identity Inspector panel and change the Class menu setting to the new DetailInterfaceController class as illustrated in Figure 8 4 Figure 8 4 Select the Label object in the detail scene in the storyboard and display the Assistant Editor panel Make sure that the Assistant Editor is displaying the DetailInterfaceController swift file and then control click and drag from the Label object to a position just beneath the class declaration line in the Assistant Editor panel Release the line and configure an outlet for the label named detailLabel The outlet should now be declared within the class file as follows import WatchKit import Foundation class DetailInterfaceController WKInterfaceController IBOutlet weak var detailLabel WKInterfaceLabel override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here Adding the Detail Data Array When the user selects a row in the table the detail scene will appear on the display The label within this scene is intended to provide a more detailed explanation of the exercise to be performed The text displayed will depend on which row was selected by the user In other words when the user selects the Warm up row the detail scene will provide details on how the user should perform the warm up routine For the purposes of this example the array containing the detailed descriptions will be placed in the interface controller of the first scene The appropriate text will then be extracted from the array based on the row chosen by the user and passed through to the detail view controller as the context object Select the InterfaceController swift file and modify it to add the detailData array

    Original URL path: http://www.techotopia.com/index.php/A_WatchKit_Table_Navigation_Tutorial (2016-02-13)
    Open archived version from archive

  • WatchKit Page-based User Interfaces and Modal Interface Controllers - Techotopia
    Pages at Runtime The interface controllers and ordering sequence for a paging interface may also be specified from within the application code via a call to the reloadRootControllersWithNames WKInterfaceController class method passing through an array containing the identifiers of the interface controllers to be included in the page navigation together with a second array containing context data to be passed to each controller during scene transitions for example WKInterfaceController reloadRootControllersWithNames controllerOne controllerTwo contexts contextObj1 contextObj2 It is also possible to make an interface controller the currently displayed controller within the page sequence via a call to the becomeCurrentPage method within an initialization method of the interface controller to be displayed Modal Presentation of Interface Controllers Interface controller scenes can be displayed modally using either storyboard segues or programmatically from within a WatchKit app extension Modal interface controllers are typically used to display information to the user and result in the scene associated with the controller appearing to the user together with an option located in the top left hand corner to dismiss the scene and return to the previous controller Modal Presentation in Code Modal interface controllers can be displayed individually or as a paging group A single interface controller can be presented in code via a call to the presentControllerWithName method passing through as parameters the identifier name of the interface controller to be displayed and an optional object containing context data which will be passed to the modal interface controller via the awakeWithContext lifecycle method For example the following code fragment modally presents the interface controller with the identifier matching controllerTwo presentControllerWithName controllerTwo context contextObj A set of interface controllers organized using page based navigation may be presented modally from within code using the presentControllerWithNames method passing through arrays containing the controller identifiers and corresponding context objects presentControllerWithNames controllerOne controllerTwo contexts contextObj1 contextObj2 By default the dismissal option displayed in the modal scene will be labelled Cancel This can be changed by setting the title property of the interface controller to the desired text eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Modal Presentation using Storyboard Segues An alternative to writing code to present an interface controller modally involves the use of storyboard segues All that is required to implement a modal segue is to Ctrl click on the user interface object within the scene that is to trigger the modal transition and then drag the line to the scene that is to be presented If a modal segue can be established the destination scene will highlight as shown in Figure 9 5 If the scene does not highlight a segue cannot be established It is not possible for example to establish a segue from a Label object because labels unlike Button objects do not trigger an event when tapped by the user Figure 9 5 Upon release

    Original URL path: http://www.techotopia.com/index.php/WatchKit_Page-based_User_Interfaces_and_Modal_Interface_Controllers (2016-02-13)
    Open archived version from archive

  • A WatchKit Page-based Navigation Tutorial - Techotopia
    steps to establish a segue between the second and third interface controllers so that the storyboard matches that of Figure 10 6 Figure 10 6 Run the app and test that the three scenes can now be navigated by making swiping motions on the display Figure 10 7 Assigning Interface Controllers The scene now consists of three scenes but at the moment only the first scene has an Interface Controller associated with it One approach to take at this point might be to add two new interface controller classes to the project one for each of the two remaining scenes in the page based navigation set Since all three scenes essentially perform the same task however a more efficient approach is to use the same interface controller class for all three The first scene is already assigned to the InterfaceController class so select the second scene so that it is highlighted in blue display the Identity Inspector panel and change the Class menu setting to InterfaceController Figure 10 8 Repeat these steps with the third interface controller selected Figure 10 8 In each case make sure that the Module menu is set to PageDemoApp WatchKit Extension Adding the Timer Interface Controller When the Start button is tapped on any of the paged controller scenes an additional scene will be modally displayed to the user This scene will contain a single object in the form of a WKInterfaceTimer instance Start by dragging and dropping an Interface Controller object from the Object Library panel so that it is positioned beneath the three existing scenes in the storyboard Figure 10 9 Drag and drop a Timer object from the Object Library onto the newly added scene With the Timer object selected display the Attributes Inspector panel and set the Horizontal and Vertical position attributes to Center Click on the T button within the Font text field select System from the popup panel Bold from the Style menu and set the Size setting to 28 as shown in Figure 10 10 Figure 10 10 On completion of these steps the scene should match that illustrated in Figure 10 11 Figure 10 11 By default the Timer object displays hours minutes and seconds as numbers referred to as positional format A range of other configuration alternatives is available within the Attributes Inspector for changing both what is displayed on the timer and how it is displayed It is for example possible to also display month week day and year values It is also possible to change the way in which this information is displayed including fully spelling out the time and date in words instead of numbers Although this example will use the default setting it is worth reviewing the options in the Attributes Inspector panel for future reference Locate and select the PageDemoApp WatchKit Extension entry in the Xcode Project Navigator panel Ctrl click on it and select the New File menu option Create a new iOS Cocoa Touch Class named TimerInterfaceController subclassed from WKInterfaceController and proceed with the steps to generate the class source file into the PageDemoApp WatchKit Extension project folder Return to the Interface storyboard file select the timer scene so that it is highlighted in blue and using the Identity Inspector panel change the Class menu to TimerInterfaceController Switch to the Attributes Inspector panel and enter TimerInterfaceController into the Identifier field Select the Timer object in the timer scene display the Assistant Editor panel and verify that it is displaying the source code for the TimerInterfaceController swift file Ctrl click on the Timer object and drag the resulting line to a position immediately after the class declaration line in the Assistant Editor panel Release the line and establish an outlet named workoutTimer On completion of these steps the start of the TimerInterfaceController swift file should read as follows import WatchKit import Foundation class TimerInterfaceController WKInterfaceController IBOutlet weak var workoutTimer WKInterfaceTimer override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here With the timer interface controller added and configured the next step is to implement the modal segues so that the interface controller is presented when the Start buttons are tapped Adding the Modal Segues When the buttons on the three paged scenes are tapped by the user the timer scene will need to be displayed modally Establish the first segue by Ctrl clicking on the Button object in the first scene and dragging to the timer scene Release the line and select the modal option from the Action Segue menu Repeat these steps for the Button objects on the remaining two scenes so that the storyboard resembles Figure 10 12 Figure 10 12 Compile and run the app and verify that tapping the button on each of the scenes causes the scene for the timer interface controller to appear Note that a Cancel option is provided on the timer scene to return to the original scene eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Configuring the Context Data When the timer interface controller is presented it will need to be initialized with a countdown time The amount of time will vary depending on the scene from which the timer was launched This means that we will need to find a way to identify which of the three modal segues triggered the appearance of the timer interface controller and based on that information pass the appropriate time duration through as context data The first step in this process is to establish a way to distinguish one segue from another by assigning identifiers Begin by selecting the segue line from the left most scene to the timer scene and displaying the Attributes Inspector panel Within the panel enter walkSegue into the Identifier field as shown in Figure 10 13 Figure 10 13 Follow the same steps to assign identifiers to

    Original URL path: http://www.techotopia.com/index.php/A_WatchKit_Page-based_Navigation_Tutorial (2016-02-13)
    Open archived version from archive

  • WatchKit App and Parent iOS App Communication - Techotopia
    the WKInterfaceController class rather than on an instance of the class The first parameter to the method named requestDictionary in the above example is a Dictionary object containing any data that needs to be passed to the parent iOS app in order for the app to perform the necessary tasks The second parameter to the method is a closure expression containing the code that will be called and executed by the parent iOS app in order to reply to the openParentApplication call When the closure expression is called it can be passed a Dictionary object represented by replyDictionary in the above example containing any data that needs to be returned to the extension together with an NSError object or a nil value if no error occurred used to report any problems encountered whilst the request was being fulfilled by the parent app The following code for example creates a dictionary containing a key value pair and calls the openParentApplication method using a closure expression which outputs the value of the reply dictionary object based on a key of replyString let requestValues myValue startPlay WKInterfaceController openParentApplication requestValues reply replyValues error Void in println replyValues replyString The handleWatchKitExtensionRequest Method When the openParentApplication method is called the handleWatchKitExtensionRequest method is called on the app delegate of the parent iOS app This method is passed the Dictionary object declared in the openParentApplication method call and a reference to the reply closure expression The handleWatchKitExtensionRequest method will typically extract data from the dictionary perform any necessary tasks create a new dictionary populated with the reply data and then pass that through as a parameter when calling the reply closure expression The following code shows an example implementation of the handleWatchKitExtensionRequest method func application application UIApplication handleWatchKitExtensionRequest userInfo NSObject AnyObject reply NSObject AnyObject Void var replyValues Dictionary String AnyObject replyValues replyString Playback started reply replyValues Understanding iOS Background Modes Although the parent iOS app is not automatically suspended when the user is no longer interacting with the matching WatchKit app there are still situations where the iOS app running in the background may be suspended or terminated by the iOS operating system There are a number of ways to mitigate this risk depending on whether the tasks being performed by the iOS app on behalf of the WatchKit app are short or long term in nature eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Using the beginBackgroundTaskWithName Method If the iOS app needs to perform a task which is short term and where suspension prior to replying to the WatchKit app would be detrimental to the user experience the use of the UIApplication beginBackgroundTaskWithName and endBackgroundTask methods is recommended The beginBackgroundTaskWithName method indicates to the operating system that the app needs extra time to complete a task The endBackgroundTask method is called to notify the operating

    Original URL path: http://www.techotopia.com/index.php/WatchKit_App_and_Parent_iOS_App_Communication (2016-02-13)
    Open archived version from archive

  • A WatchKit openParentApplication Example Project - Techotopia
    to silent mode The audio player instance is then configured with the mp3 file containing the audio to played and an initial volume level set Implementing the Audio Control Methods With the audio player configured and initialized the next step is to add some methods to control the playback of the music Remaining within the ViewController swift file implement these three methods as follows func stopPlay audioPlayer stop func startPlay audioPlayer play func adjustVolume level Float audioPlayer volume level Each of these methods will need to be called by the corresponding action methods as follows IBAction func playAudio sender AnyObject startPlay IBAction func stopAudio sender AnyObject stopPlay IBAction func sliderMoved sender AnyObject adjustVolume volumeControl value Compile and run the iOS app and test that the user interface controls allow playback to be started and stopped via the two buttons and that the slider provides control over the volume level With the iOS app now functioning it is time to focus on creating the matching WatchKit app Adding the WatchKit App Target Within Xcode select the File New Target menu option In the target template dialog select the Apple Watch option listed beneath the iOS heading In the main panel select the WatchKit App icon and click on Next On the subsequent screen turn off the Include Glance Scene and Include Notification Scene options before clicking on the Finish button As soon as the extension target has been created a new panel will appear requesting permission to activate the new scheme for the extension target Activate this scheme now by clicking on the Activate button in the request panel Designing the WatchKit App Scene Select the Interface storyboard file located under OpenParentApp WatchKit App so that the storyboard loads into Interface Builder Drag and drop a Label two Buttons and a Slider from the Object Library onto the scene canvas so that the layout matches that shown in Figure 13 5 Figure 13 5 eBookFrenzy com Purchase the fully updated watchOS 2 Swift 2 edition of this book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Select the Label object display the Attributes Inspector panel and set the Alignment property to center the displayed text Within the Position section of the panel change the Horizontal menu to Center Double click on the uppermost of the two buttons and change the text to Play Repeat this step for the second button this time changing the text so that it reads Stop Select the Slider object and in the Attributes Inspector panel change both the Maximum and Steps properties to 10 On completion of the above steps the scene layout should resemble Figure 13 6 Figure 13 6 Display the Assistant Editor and verify that it is showing the contents of the InterfaceController swift file Using the Assistant Editor establish an outlet connection from the Label object in the user interface named statusLabel Next create action connections

    Original URL path: http://www.techotopia.com/index.php/A_WatchKit_openParentApplication_Example_Project (2016-02-13)
    Open archived version from archive



  •