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".
  • Working with Array and Dictionary Collections in Swift - Techotopia
    println treeArray 2 This approach can also be used to replace the value at an index location treeArray 1 Redwood The above code replaces the current value at index position 1 with a new String value that reads Redwood Appending Items to an Array Items may be added to an array using either the append method or and operators The following for example are all valid techniques for appending items to an array treeArray append Redwood treeArray Redwood treeArray Redwood Maple Birch Inserting and Deleting Array Items New items may be inserted into an array by specifying the index location of the new item in a call to the array s insert atIndex method An insertion preserves all existing elements in the array essentially moving them to the right to accommodate the newly inserted item treeArray insert Maple atIndex 0 Similarly an item at a specific array index position may be removed using the removeAtIndex method call treeArray removeAtIndex 2 To remove the last item in an array simply make a call to the array s removeLast method as follows treeArray removeLast Array Iteration The easiest way to iterate through the items in an array is to make use of the for in looping syntax The following code for example iterates through all of the items in a String array and outputs each item to the console panel var treeArray Pine Oak Yew Maple Birch Myrtle for tree in treeArray println tree Upon execution the following output would appear in the console Pine Oak Yew Maple Birch Myrtle eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Swift Dictionary Collections google ADSDAQBOX FLOW google String dictionaries allow data to be stored and managed in the form of key value pairs Dictionaries fulfill a similar purpose to arrays except each item stored in the dictionary has associated with it a unique key to be precise the key is unique to the particular dictionary object which can be used to reference and access the corresponding value Currently only String Int Double and Bool data types are suitable for use as keys within a Swift dictionary Swift Dictionary Initialization A dictionary is a data type designed specifically to hold multiple values in a single unordered collection Each item in a dictionary consists of a key and an associated value The data types of the key and value elements type may be specified specifically using type annotation or left to the compiler to identify using type inference A new dictionary may be initialized with a collection of values referred to as a dictionary literal at creation time using the following syntax var variableName key type value type key 1 value 1 key 2 value2 The following code creates a new array assigned to a variable thereby making it mutable that is initialized with

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


  • The iOS 8 Application and Development Architecture - Techotopia
    manages the user interface and the Controller provides the basic logic for the application and acts as the go between providing instructions to the Model based on user interactions with the View and updating the View to reflect responses from the Model The true value of this approach is that the Model knows absolutely nothing about the presentation of the application It just knows how to store and handle data and perform certain tasks when called upon by the Controller Similarly the View knows nothing about the data and logic model of the application Within the context of an object oriented programming environment such as the iOS 8 SDK and Swift the Model View and Controller components are objects It is also worth pointing out that applications are not restricted to a single model view and controller In fact an app can consist of multiple view objects controller objects and model objects The way that a view controller object interacts with a Model is through the methods and properties exposed by that model object This in fact is no different from the way one object interacts with another in any object oriented programming environment In terms of the view controller s interactions with the view however things get a little more complicated In practice this is achieved using the Target Action pattern together with Outlets and Actions eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters The Target Action pattern IBOutlets and IBActions When you create an iOS 8 app you will typically design the user interface the view using the Interface Builder tool and write the view controller and model code in Swift using the Xcode code editor The previous section looked briefly at how the view controller interacts with the model In this section we will look at how the view created in Interface Builder and our view controller code interact with each other When a user interacts with objects in the view for example touching and releasing a button control an event is triggered in this case the event is called a Touch Up Inside event The purpose of the Target Action pattern is to allow you to specify what happens when such events are triggered In other words this is how you connect the objects in the user interface you have designed in the Interface Builder tool to the back end Swift code you have written in the Xcode environment Specifically this allows you to define which method of which controller object gets called when a user interacts in a certain way with a view object The process of wiring up a view object to call a specific method on a view controller object is achieved using something called an Action An action is a method defined within a view controller object that is designed

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

  • Understanding iOS 8 Views, Windows and the View Hierarchy - Techotopia
    section The user does not see or interact directly with the UIWindow object These windows may be created programmatically but are typically created automatically by Interface Builder when you design your user interface The View Hierarchy iOS 8 user interfaces are constructed using a hierarchical approach whereby different views are related through a parent child relationship At the top of this hierarchy sits the UIWindow object Other views are then added to the hierarchy If we take the example from the chapter entitled Creating an Interactive iOS 8 App we have a design that consists of a window a view a text field a button and a label The view hierarchy for this user interface would be drawn as illustrated in Figure 17 1 Figure 17 1 In this example the UIWindow object is the parent or superview of the UIView instance and the UIView is the child or subview of the UIWindow Similarly the text label and button objects are all subviews of the UIView A subview can only have one direct parent As shown in the above example however a superview may have multiple subviews In addition view hierarchies can be nested to any level of depth Consider for example the following hierarchy diagram Figure 17 2 The hierarchical structure of a user interface has significant implications for how the views appear and behave Visually subviews always appear on top of and within the visual frame of their corresponding parent The button in the above example therefore appears on top of the parent view in the running application Furthermore the resizing behavior of subviews in other words the way in which the views change size when the device is rotated is defined in relation to the parent view Superviews also have the ability to modify the positioning and size of their subviews eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters If we were to design the above nested view hierarchy in Interface Builder it might appear as illustrated in Figure 17 3 In this example the UIWindow instance is not visible because it is fully obscured by the UIView1 instance Displayed on top of and within the frame of UIView1 are the UIButton1 UILabel and UIView2 subviews Displayed on top of and within the frame of UIView2 are its respective subviews namely UIButton2 and UIImageView Figure 17 3 The view hierarchy also defines how events are handled when a user interacts with the interface essentially defining something called the responder chain If for example a subview receives an event that it cannot handle that event is passed up to the immediate superview If that superview is also unable to handle the event it is passed up to the next parent and so on until it reaches a level within the responder chain where it can be

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

  • An Introduction to Auto Layout in iOS 8 - Techotopia
    label is now permitted to grow in width up to the specified limit allowing longer content to be displayed without clipping Auto layout constraints are by nature interdependent and as such situations can arise where a constraint on one view competes with a constraint on another view to which it is connected In such situations it may be necessary to make one constraint stronger and the other weaker in order to provide the system with a way of arriving at a layout solution This is achieved by assigning priorities to constraints Priorities are assigned on a scale of 0 to 1000 with 1000 representing a required constraint and lower numbers equating to optional constraints When faced with a decision between the needs of a required constraint and an optional constraint the system will meet the needs of the required constraint exactly while attempting to get as close as possible to those of the optional constraint In the case of two optional constraints the needs of the constraint with the higher priority will be addressed before those of the lower Alignment Rects When working with constraints it is important to be aware that constraints operate on the content of a view not the frame in which a view is displayed This content is referred to as the alignment rect of the view Alignment constraints such as those that cause the center of one view to align with that of another will do so based on the alignment rects of the views disregarding any padding that may have been configured for the frame of the view Intrinsic Content Size Some views also have what is known as an intrinsic content size This is the preferred size that a view itself believes it needs to be to display its content to the user A Button view for example will have an intrinsic content size in terms of height and width that is based primarily on the text or image it is required to display and internal rules on the margins that should be placed around that content When a view has an intrinsic content size Auto Layout will automatically assign two constraints for each dimension for which the view has indicated an intrinsic content size preference i e height and or width One constraint is intended to prevent the size of the view becoming larger than the size of the content otherwise known as the content hugging constraint The other constraint is intended to prevent the view from being sized smaller than the content referred to as the compression resistance constraint Content Hugging and Compression Resistance Priorities The resize behavior of a view with an intrinsic content size can be controlled by specifying compression resistance and content hugging priorities A view with a high compression resistance priority and a low content hugging priority will be allowed to grow but will resist shrinking in the corresponding dimension Similarly a high compression resistance priority in conjunction with a high content hugging priority will cause the

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

  • Working with iOS 8 Auto Layout Constraints in Interface Builder - Techotopia
    menu With the label still selected in the view canvas and the Align menu panel displayed enable the checkbox next to the Horizontal Center in Container property Figure 19 12 Since no offset from the center is required leave the offset value at 0 Figure 19 12 With the constraint appropriately configured click on the Add 1 Constraint button to add the constraint to the view Having configured some constraints rotate the orientation once again noting this time that the label is visible and positioned sensibly Testing different display form factors in the Preview screen should also demonstrate that the constraints are working to keep the label correctly positioned for different devices Figure 19 13 In this example so far only a small subset of the Auto Layout features provided by Xcode 6 has been used In actual fact Xcode 6 provides a wide range of options and visual cues that are designed to ease the task of creating Auto Layout constraints The Auto Layout Features of Interface Builder A number of features are provided in Xcode 6 in order to assist in the implementation of Auto Layout based constraints This section will present a guided tour of many of these features Suggested Constraints When objects are added to a layout canvas Interface Builder does not implement any default constraints on those views leaving the developer to add constraints as needed There is however the option to have Interface Builder apply suggested constraints When this option is used Interface Builder will apply what it believes to be the correct constraints for the layout based on the positioning of the views Suggested constraints can be added either to the currently selected view objects or to an entire scene layout In situations where constraints are missing from a layout resulting in warnings Interface Builder also provides the option to automatically add the constraints that it believes are missing The options to perform these tasks are accessed via the Resolve Auto Layout Issues menu in the toolbar as illustrated in Figure 19 14 The top section of the menu represents tasks that relate to the currently selected views in the canvas whilst the options in the lower section apply to all views in the currently selected view controller scene google ADSDAQBOX FLOW google Figure 19 14 Most of the time the suggested constraints will exactly match the required layout behavior and occasionally the suggested constraints will be incorrect Most of the time however the suggested constraints provide an excellent starting point for implementing Auto Layout A typical process for designing a user interface might therefore involve positioning the views by dragging and dropping them into place applying suggested constraints and then editing and fine tuning those constraints to perfect the layout To see suggested constraints in action select the label view in the AutoLayoutExample project and select the Clear Constraints option from the Resolve Auto Layout Issues menu At this point there are no constraints in the layout and the old positioning problem appears when the view is rotated With the label still selected choose the Reset to Suggested Constraints menu option A review of the view canvas and change of orientation should demonstrate that Interface Builder has suggested and applied the exact same constraints that we previously added manually eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Visual Cues Interface Builder includes a number of visual cues in the layout canvas to highlight the constraints currently configured on a view and to draw attention to areas where problems exist When a view is selected within the layout canvas the constraints that reference that view will be represented visually Consider for example the label view created in our AutoLayoutExample application When selected in the canvas a number of additional lines appear as shown in Figure 19 15 Figure 19 15 The vertical line that runs through the center of the label indicates the presence of a constraint that positions the label in the horizontal center of the parent view analogous to the NSLayoutAttribute CenterX attribute If expressed as an equation therefore this would read as label NSLayoutAttribute CenterX superview NSLayoutAttribute CenterX The I beam line running from the bottom edge of the label view to the bottom edge of the parent view indicates that a vertical space constraint is in place between the two views The absence of any additional visual information on the line indicates that this is an equality constraint Figure 19 16 shows an example of a greater than or equal to horizontal constraint between two button views Figure 19 16 The horizontal line running beneath the Button label text indicates that constraints are in place to horizontally align the content baseline represented by NSLayoutAttributeBaseline of the two buttons Width constraints are indicated by an I beam line running parallel to the edge of the view in the corresponding dimension The text view object in Figure 19 17 for example has a greater than or equal to width constraint configured Figure 19 17 Highlighting Constraint Problems Interface Builder also uses a range of visual cues and decorations to indicate that constraints are either missing ambiguous or in conflict Valid and complete Auto Layout configurations are drawn using blue lines When part of a layout is ambiguous the constraint lines are orange Ambiguity typically occurs when a constraint is missing Take for example the label view used earlier in the chapter If only the horizontal center constraint is set that constraint line will appear in orange because Auto Layout does not know where to position the view in the vertical plane Once the second constraint is set between the bottom edge of the label and the bottom of the superview the constraint line will turn blue to indicate that the layout is no longer ambiguous Red constraint lines are

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

  • An iOS 8 Auto Layout Example - Techotopia
    9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters With the label still selected use the Align menu to constrain the label to the horizontal center of the container All that now remains is to establish some constraints between the two labels Ctrl click on the left most label and drag the line to the right hand label before releasing the line From the resulting menu hold down the Shift key on the keyboard and select the Baseline and Horizontal Spacing options to align the content baselines of the two labels and to add a spacing constraint With both options selected hit the keyboard return key to add the constraints Rotate the canvas display into landscape mode by selecting the view controller and clicking on the left most icon in the layout scene toolbar so that the view is highlighted in blue Display the Attributes Inspector in the Utilities panel and change the Orientation value under Simulated Metrics to Landscape Note that the layout now re organizes to accommodate the orientation change Figure 20 6 Adjusting Constraint Priorities Up until this point the layout is behaving correctly using basic constraints with default priorities We are now going to introduce some problems that cannot be handled adequately by the constraints as they currently stand With the view still in landscape mode double click on the left hand label and change the text so that it reads Customer Record Using the Attributes Inspector change the Alignment property so that the text is right aligned With the view in landscape mode the label appears correctly positioned Rotate the view to portrait orientation however and the label is clearly being clipped by the left hand edge of the parent view Figure 20 7 Clearly there is some work to be done to make the user interface appear correctly in both orientations The first step is to consider the constraints that are currently set on the label views The right hand label has a constraint that forces it to be centered horizontally in the parent view The left hand label on the other hand has a constraint that connects its trailing edge to the leading edge of the second label The absence of a constraint on the left hand edge of the Customer Record label is resulting in the label being pushed off the screen in portrait mode One possible solution to this problem might be to create a new constraint on the Customer Record label that puts some space between the left hand edge of the customer record label and the left hand edge of the parent view To add this constraint rotate the view back to landscape so that the label is fully visible select the Customer Record label display the Pin menu turn off the Constrain to margins option and establish a standard space constraint between the left hand edge of the view and the nearest neighbor Before adding the constraint change the Update Frames option from

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

  • Implementing iOS 8 Auto Layout Constraints in Swift Code - Techotopia
    a View Once a constraint has been created it needs to be assigned to a view in order to become active This is achieved by passing it through as an argument to the addConstraint method of the view instance to which it is being added In the case of multiple constraints each is added by a separate call to the addConstraint method This leads to the question of how to decide which view the constraint should be added to In the case of a constraint that references a single view the constraint must be added to the immediate parent of the view When a constraint references two views the constraint must be applied to the closest ancestor of the two views Consider for the purposes of an example the view hierarchy illustrated in Figure 21 1 Figure 21 1 A constraint referencing only Label A should be added to the immediate parent in this case View B A constraint referencing Button B and Label B on the other hand must be added to the nearest common ancestor which in this case is View C A constraint referencing Button A and Button B must once again be added to the nearest common ancestor which equates to View A For the purposes of an example the following code excerpt creates a new constraint and adds it to a view var myConstraint NSLayoutConstraint item mybutton attribute NSLayoutAttribute Width relatedBy NSLayoutRelation Equal toItem mylabel attribute NSLayoutAttribute Width multiplier 5 0 constant 0 self view addConstraint myConstraint Turning off Auto Resizing Translation When adding views to a layout in code the toolkit will by default attempt to convert the autosizing mask for that view to Auto Layout constraints Unfortunately those auto generated constraints will conflict with any constraints added within the application code It is essential therefore that translation be turned off for views to which constraints are to be added in code This is achieved by calling the setTranslatesAutoresizingMaskIntoConstraints method of the target view passing through false as an argument For example the following code creates a new Button view turns off translation and then adds it to the parent view let mybutton UIButton mybutton setTitle My Button forState UIControlState Normal mybutton setTranslatesAutoresizingMaskIntoConstraints false self view addSubview mybutton eBookFrenzy com Purchase the fully updated iOS 9 Swift 2 Xcode 7 edition of this book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters An Example Application Create a new Xcode project using the Single View Application template Select Universal from Devices menu and Swift from the language menu entering AutoLayoutCode as the product name Creating the Views For the purpose of this example the code to create the views and constraints will be added to the viewDidLoad method of the AutoLayoutCode view controller Select the ViewController swift file locate this method and modify it to create a button and a label and add them to the main view override

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

  • Implementing Cross-Hierarchy Auto Layout Constraints in iOS 8 - Techotopia
    the Device and Language menus to Universal and Swift respectively Select the Main storyboard file from the project navigator panel select the view and change the background color to a light shade of grey using the Attributes Inspector Drag and drop UIView Button and Label views onto the design canvas as illustrated in Figure 22 2 making sure to center the label object horizontally and vertically within the parent view Select the newly added view object click on the Resolve Auto Layout Issues menu from the toolbar in the lower right hand corner of the canvas and select the Reset to Suggested Constraints option listed under All Views in View Controller Establishing Outlets In order to set a cross hierarchy constraint within code it will be necessary to implement some outlets Since the constraint will need to reference both the button and the label outlets need to be configured for these views Select the label object and display the Assistant Editor using the View Assistant Editor Show Assistant Editor menu option or by selecting the center button the one containing an image of two interlocking circles of the row of Editor toolbar buttons in the top right hand corner of the main Xcode window Make sure that the Assistant Editor is showing the ViewController swift file Ctrl click on the Label object in the view and drag the resulting line to the area immediately beneath the class declaration directive in the Assistant Editor panel Upon releasing the line the connection panel will appear Configure the connection as an Outlet named myLabel and click on the Connect button Repeat the above steps to add an outlet for the button object named myButton As currently constrained the label object is centered horizontally within the view we are referring to as View B In place of this constraint we need the label to be aligned with the center of the button object This will involve removing the CenterX constraint and replacing it with a new constraint referencing the button This requires outlets for both the View B instance and the CenterX constraint Ctrl click on the View B parent of the label object and drag the resulting line to the area immediately beneath the previously declared outlets in the Assistant Editor Release the line and configure an outlet named viewB Next select the label object so that the associated constraint lines appear Click on the vertical line passing through the label view so that it highlights Ctrl click on the constraint line and drag to the Assistant Editor panel Figure 22 3 and create a new outlet for this object named centerConstraint Figure 22 3 Writing the Code to Remove the Old Constraint With the necessary outlets created the next step is to write some code to remove the center constraint from the label object For the purposes of this example all code will be added to the viewDidLoad method of the view controller Select the ViewController swift file and locate and modify

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