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".
  • Using Xcode Storyboards to create an iOS 5 iPhone Tab Bar Application - Techotopia
    necessary for a single view controller based application named TabBarViewController m and TabBarViewController h As a result of selecting the Use Storyboard option a MainStoryboard storyboard file has also been created Renaming the Initial View Controller The next step in creating the application involves renaming the TabBarViewController class provided by Xcode to a more descriptive name Since this view controller presents the view that will be displayed when the user touches the first tab on the tab bar we will name this class Tab1ViewController To achieve this select the TabBarViewController h file and use the mouse to highlight the TabBarViewController class name in the editing panel With the name highlighted select the Edit Refactor Rename menu option In the resulting panel enter Tab1ViewController into the text box and click on Preview In the preview panel select the Save button followed by the option to Disable snapshots Adding the View Controller for the Second Content View The ultimate goal of this chapter is to create a tab bar based application consisting of two tabs with corresponding views Xcode has already created the first view controller for us and we have subsequently renamed this class The next step therefore is to add the view controller for the second view To achieve this select the File New New File menu option and on the resulting panel select UIViewController subclass Click Next and on the next screen name the new class Tab2ViewController and change the Subclass of menu to UIViewController Click Next and proceed with the creation process Adding the Tab Bar Controller to the Storyboard As previously explained the navigation between view controllers in a Tab Bar based interface is handled by a Tab Bar Controller It will be necessary therefore to add one of these to our storyboard Begin by selecting the MainStoryBoard storyboard file in the Xcode project navigator panel and note as illustrated in Figure 16 1 that the storyboard currently only contains the original view controller instance added for us by Xcode when the project was created Figure 16 1 In order to add a Tab Bar Controller to the storyboard select the Tab1 View Controller in the storyboard design area and select the Editor Embed In Tab Bar Controller menu option The Tab Bar Controller will subsequently appear in the storyboard already connected to the Tab Bar Controller as shown in Figure 16 2 Figure 16 2 Now that the Tab Bar Controller has been added the next task is to add a view controller to the storyboard that will correspond to the Tab2ViewController class previously added to the project Adding a Second View Controller to the Storyboard The second view controller may be added to the storyboard simply by dragging and dropping one from the Object Library panel View Utilities Show Object Library Once the new view controller has been added to the storyboard canvas select it so that it is highlighted with a blue border and then display the Identity Inspector panel View Utilities Show Identity

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

  • An Overview of iOS 5 Table Views and Xcode Storyboards - Techotopia
    Xcode Storyboards to Build Dynamic TableViews with Prototype Table View Cells will work through the creation of example projects intended to demonstrate the use of storyboards in the context of table views An Overview of the Table View Table Views present the user with data in a list format and are represented by the UITableView class of the UIKit framework The data is presented in rows whereby the content of each row is implemented in the form of a UITableViewCell object By default each table cell can display a text label textLabel a subtitle detailedTextLabel and an image imageView More complex cells can be created by either adding subviews to the cell or subclassing UITableViewCell and adding your own custom functionality and appearance Static vs Dynamic Table Views When implementing table views using an Xcode storyboard it is important to understand the distinction between static and dynamic tables Static tables are useful in situations when a fixed number of rows need to be displayed in a table The settings page for an application for example would typically have a predetermined number of configuration options and would be an ideal candidate for a static table Dynamic tables also known as prototype based tables on the other hand are intended for use when a variable number of rows need to be displayed from a data source Within the storyboard editor Xcode allows you to visually design a prototype table cell which will then be replicated in the dynamic table view at runtime in order to display data to the user The Table View Delegate and dataSource Each table view in an application needs to have a delegate and a dataSource associated with it with the exception of static tables which do not have data source The dataSource implements the UITableViewDataSource protocol which basically consists of a number of methods that define title information how many rows of data are to be displayed how the data is divided into different sections and most importantly supplies the table view with the cell objects to be displayed The delegate implements the UITableViewDelegate protocol and provides additional control over the appearance and functionality of the table view including detecting when a user touches a specific row defining custom row heights and indentations and also implementation of row deletion and editing functions Table View Styles Table views may be configured to use either plain or grouped style In the grouped style the rows are grouped together in sections represented by rounded rectangles For example Figure 17 1 shows a table view configured to use the grouped style Figure 17 1 In the case of the plain style the items are listed without separation and using the full width of the display Figure 17 2 Table Views using plain style can also be indexed whereby rows are organized into groups according to specified criteria such as alphabetical or numerical sorting Table View Cell Styles In addition to the style of the Table View itself different styles may also

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

  • Using Xcode Storyboards to Build Dynamic TableViews with Prototype Table View Cells - Techotopia
    be dragged from the Object Library panel and dropped onto a scene within the storyboard Note however that this is only possible when the storyboard view is zoomed in With this in mind verify that the storyboard is zoomed in using the controls in the bottom right hand corner of the canvas and then drag and drop two Labels and an Image View object onto the prototype table cell Resize and position the items so that the cell layout resembles that illustrated in Figure 18 4 making sure to stretch the label objects so that they extend toward the right hand edge of the cell Figure 18 4 Having configured the storyboard elements for the table view portion of the application it is time to begin modifying the table view and cell subclasses Modifying the CarTableViewCell Class Within the storyboard file two labels and an image view were added to the prototype cell which in turn has been declared as an instance of our new CarTableViewCell class In order to manipulate these user interface objects from within our code we need to declare three outlets and then connect those outlets to the objects in the storyboard scene Begin therefore by selecting the CarTableViewCell h file and adding the three outlet properties as follows import UIKit UIKit h interface CarTableViewCell UITableViewCell property nonatomic strong IBOutlet UIImageView carImage property nonatomic strong IBOutlet UILabel makeLabel property nonatomic strong IBOutlet UILabel modelLabel end Having declared the properties synthesize access within the CarTableViewCell m implementation file import CarTableViewCell h implementation CarTableViewCell synthesize makeLabel makeLabel synthesize modelLabel modelLabel synthesize carImage carImage end With the outlet properties declared the next step is to establish the connections to the user interface objects Within the storyboard file Ctrl click on the white background of the prototype table cell and drag the resulting blue line to the uppermost of the two labels as outlined in Figure 18 5 Figure 18 5 Upon releasing the pointer select makeLabel from the resulting menu to establish the outlet connection Figure 18 6 Repeat these steps to connect the modelLabel and carImage outlets to the second label and image view objects respectively Creating the Table View Datasource Dynamic Table Views require a datasource to provide the data that will be displayed to the user within the cells By default Xcode has designated the CarTableViewController class as the datasource for the table view controller in the storyboard It is within this class therefore that we can build a very simple data model for our application consisting of a number of arrays The first step is to declare these as properties in the CarTableViewController h file import UIKit UIKit h interface CarTableViewController UITableViewController property nonatomic strong NSArray carImages property nonatomic strong NSArray carMakes property nonatomic strong NSArray carModels end With the properties declared these need to be synthesized within the CarTableViewController m file In addition the arrays need to be initialized with some data when the application has loaded making the viewDidLoad method an ideal location

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

  • Implementing TableView Navigation using Xcode Storyboards - Techotopia
    functionality will be made possible by the addition of a Navigation Controller to the storyboard This can be added by selecting the Car Table View Controller item in the storyboard so that it highlights in blue and then selecting the Xcode Editor Embed In Navigation Controller menu option Once performed the storyboard will appear as outlined in Figure 19 2 Figure 19 2 Establishing the Storyboard Segue When the user selects a row within the table view a segue needs to be triggered to display the car detail view controller In order to establish this segue Ctrl click on the prototype cell located in the Car Table View Controller scene and drag the resulting line to the Car Detail View Controller scene Upon releasing the line select the Push option from the resulting menu note that this step can only be performed if the storyboard is currently zoomed in The storyboard will update to display a segue connection between the table view and the view controller In code that will be implemented later in this chapter it will be necessary to reference this specific segue In order to do so it must therefore be given an identifier Click on the segue connection between Car Table View Controller and Car Detail View Controller display the Attributes Inspector View Utilities Show Attributes Inspector and change the Identifier value to ShowCarDetails In addition a toolbar should have appeared in both scenes Double click on these toolbars and change the title to Cars and Car Details respectively Figure 19 3 Build and run the application and note that selecting a row in the table view now displays the second view controller which in turn has a button in the toolbar to return to the table view Clearly we now need to do some work on the CarDetailViewController class so that details about the selected car are displayed in the view Modifying the CarDetailViewController Class For the purposes of this example application the car detail view is going to display the make and model of the selected car together with a photograph In order to achieve this the class is going to need outlets to two labels and a UIImageView object which will later be added to the view In addition to the outlets the class is also going to need an internal data model that contains information about the car It will be the job of the table view controller to update this model prior to the segue occurring so that it reflects data on the selected car For the sake of simplicity the data model will take the form of an NSArray object Select the CarDetailViewController h file and modify it as follows import UIKit UIKit h interface CarDetailViewController UIViewController property strong nonatomic NSArray carDetailModel property strong nonatomic IBOutlet UILabel makeLabel property strong nonatomic IBOutlet UILabel modelLabel property strong nonatomic IBOutlet UIImageView imageView end Next modify the CarDetailViewController m file to add the corresponding synthesize directive When the view appears it will also

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

  • Using an Xcode Storyboard to Create a Static Table View - Techotopia
    is to be dynamic The most obvious indications of this are the presence of a Prototype Cell within the table view and the words Prototype Content in the view Click within the grey area of the table view and display the Attributes Inspector panel The Content attribute will currently be set to Dynamic Prototypes so use the menu to change the attribute to Static Cells At this point the table view within the storyboard will change to display a static table containing three rows as illustrated in Figure 20 1 Figure 20 1 Designing the Static Table With a static table view added to the project the full power of Interface Builder is available to us to design the layout and content of the table and cells The first step is to change the table view style With the table view selected in the storyboard and the Attributes Panel displayed change the Style attribute to Grouped and the Sections attribute to 2 The table view should now consist of two group sections with three rows per section For the purposes of this example we only need two rows in the top section so click on the third row to highlight it and press the delete key on the keyboard Note that additional rows may be added if required by selecting the corresponding table view section and changing the Rows property In the case of the bottom section only one row is required so Command click on the bottom two rows and delete them from the view Select the top section by clicking on the shaded area immediately above the cells note that sections may also be selected by clicking on the Table View Section item in the toolbar strip located across the top of the storyboard editor and also from the hierarchy list located in the center panel of the Xcode window Within the Attributes Inspector change the Header property to Car Details Repeat this step on the lower section to change the header to Car Photo Next stretch the height of the single cell in the bottom section so that it fills the remainder of the table view such that the table view appears as illustrated in the following figure Figure 20 2 Adding Items to the Table Cells Using the Object Library panel drag and drop an Image View object into the cell in the bottom section and resize it to fill most of the available space Also drag and drop labels into the two cells in the top section and set appropriate text properties so that the layout resembles that of Figure 20 3 Figure 20 3 The user interface design of the table view is now complete so the next step is to create outlets to the labels and the image view These of course need to be declared in the StaticTableViewController subclass So far in this book we have added outlets manually in the code and then connected them visually in Interface Builder In this

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

  • Creating a Simple iOS 5 iPhone Table View Application - Techotopia
    failed to implement any methods The only indication we will get will be the application crashing at run time For this reason including these declarations is generally considered to be good programming practice Next edit the TableExampleViewController m class implementation file to synthesize access to the array import TableExampleViewController h implementation TableExampleViewController synthesize colorNames end Now that we have declared our array we need to initialize it with some data when the view is loaded In order to achieve this we will override the viewDidLoad method Scroll down the TableExampleViewController m file until you find the template method and modify the method as follows void viewDidLoad super viewDidLoad self colorNames NSArray alloc initWithObjects Red Green Blue Indigo Violet nil Having allocated memory to our array and initialized it the next task is to make sure the memory gets released when the array is no longer required To do so it is necessary to add code to the viewDidUnload method void viewDidUnload Release any retained subviews of the main view e g self myOutlet nil self colorNames nil With our data constructed all we need to do now is implement the numberOfRowsInSection and cellForRowAtIndexPath methods in order to conform to the minimum requirements of the UITableViewDataSource protocol We will begin by implementing the numberOfRowsInSection method which is called by the Table View to identify how many rows are to be displayed Since our data is held in an array we can simply return the number of array elements to satisfy this requirement so add the following method to your TableExampleViewController m file Customize the number of rows in the table view NSInteger tableView UITableView tableView numberOfRowsInSection NSInteger section return self colorNames count Next the cellForRowAtIndexPath method needs to be implemented Customize the appearance of table view cells UITableViewCell tableView UITableView tableView cellForRowAtIndexPath NSIndexPath indexPath static NSString CellIdentifier Cell UITableViewCell cell tableView dequeueReusableCellWithIdentifier CellIdentifier if cell nil cell UITableViewCell alloc initWithStyle UITableViewCellStyleDefault reuseIdentifier CellIdentifier Configure the cell cell textLabel text self colorNames objectAtIndex indexPath row return cell In the above method everything with the exception of the following line is standard boilerplate code that you can reuse every time you need to implement this particular method cell textLabel text self colorNames objectAtIndex indexPath row The method begins by creating a cell identifier Next an attempt is made to reuse an existing cell that may have scrolled off the screen and is therefore available for use as the currently requested cell If no previous cell is available for use then a new one is created using the UITableViewCellStyleDefault style The cellForRowAtIndexPath method is passed as an argument an object of type NSIndexPath from which we are able to extract the row number for which the cell is being requested by the table view This row number is used in turn to extract the corresponding element from our colorNames array and assign it to the textLabel property of the cell object Building and Running the Application Once the code has been implemented for the

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

  • Creating a Navigation based iOS 5 iPhone Application using TableViews - Techotopia
    our newly created rootController is assigned as the navigation controller s root view controller thereby ensuring that this is the first view displayed to the user In terms of creating the navigation controller this is all that needs to be done The remainder of the work to complete this project involves setting up the views that will be displayed to the user Setting up the Data in the Root View Controller The root view controller is going act as the data source and app delegate for the table view that displays the list of authors As such we need to add some code to set up this data and override some methods to implement the functionality of our application Begin by editing the RootViewController h interface file and adding the declaration for an array to hold the names of the authors who are going to appear on the root view controller s table view interface RootViewController UITableViewController NSArray authorList property nonatomic strong NSArray authorList end Edit RootViewController m and add the line to synthesize access to the authorList array then edit the viewDidLoad method to implement the code to initialize the authorList array and set the title of the root table view to Authors import RootViewController h implementation RootViewController synthesize authorList void viewDidLoad super viewDidLoad self authorList NSArray alloc initWithObjects Clancy Thomas Lehane Dennis nil self title Authors end Next modify the viewDidUnload method to ensure that we indicate that we are finished with the authorList array when the view unloads or the application exits void viewDidUnload Release anything that can be recreated in viewDidLoad or on demand e g self myOutlet nil self authorList nil Writing Code to Display the Data in the Table View When a table view is displayed it needs to know how many sections and rows it is going to be required to display It finds this information by making calls to the numberOfSectionsInTable and numberOfRowsInSection methods Our RootViewController m file contains pre created templates for these methods currently coded to return 0 In our example we want to return 1 for the number of sections and a value equivalent to the number of items in our authorList array so we need to modify these methods accordingly removing the warning lines in the process NSInteger numberOfSectionsInTableView UITableView tableView Return the number of sections return 1 NSInteger tableView UITableView tableView numberOfRowsInSection NSInteger section Return the number of rows in the section return self authorList count Having identified the number of items to be displayed the table view then needs to get a UITableViewCell object containing the data to be displayed for each row It achieves this by calling the cellForRowAtIndexPath method for each row passing through the current row as an argument When called this method identifies the row being requested and constructs and returns a cell object Implement the code to return a table cell for each row of the table view by modifying the template method as follows UITableViewCell tableView UITableView tableView cellForRowAtIndexPath NSIndexPath indexPath static NSString CellIdentifier Cell UITableViewCell cell tableView dequeueReusableCellWithIdentifier CellIdentifier if cell nil cell UITableViewCell alloc initWithStyle UITableViewCellStyleDefault reuseIdentifier CellIdentifier Configure the cell cell textLabel text self authorList objectAtIndex indexPath row return cell Note that Xcode has already written most of the code for us In fact the only line we have to add is as follows cell textLabel text self authorList objectAtIndex indexPath row This line of code simply identifies the row requested uses it as the index into our array of authors and assigns it to the appropriate property of the cell object created for us in the preceding lines of the method Now is a good time to make sure we have not made any errors in our coding so build and run the application to make sure it all works so far Assuming all is well the application should appear in the simulator as shown in Figure 17 3 Figure 17 3 When the user selects an author we want to navigate to another table view listing the books written by the chosen author That table view will have its own NIB file containing the table view object and also its own view controller The next steps therefore are to create these objects and configure them to meet our project requirements Creating the Second View Controller To create the view controller for the table view that will display the list of books written by the selected author Ctrl click on the TableView entry in the project navigator panel and select New File On the template selection screen select iOS Cocoa Touch Class followed by the UIViewController subclass icon and click Next On the next screen select UITableViewController from the Subclass of menu and make sure that the With XIB for user interface option is on Name the new class BooksViewController and proceed with the class creation process Before we begin work on our second table view we need to make sure that the new class is declared within our root view controller class To do so edit the RootViewController h file add code to forward reference the BooksViewController class and add an outlet to the new view controller import UIKit UIKit h class BooksViewController interface RootViewController UITableViewController NSArray authorList BooksViewController booksController property nonatomic strong NSArray authorList property nonatomic strong IBOutlet BooksViewController booksController end Connecting the Second View Controller to the Root View Controller Having created an outlet for the books view controller within our root view controller class the next step is to connect that outlet to the books controller object Select the RootViewController xib file and drag a new View Controller UIViewController object from the Object library panel View Utilities Show Object Library to the Objects panel Display the Identity Inspector View Utilities Show Identity Inspector and change the Class from UIViewController to BooksViewController Figure 17 4 Select the File s Owner entry listed under Placeholders and open the Connections Inspector View Utilities Show Connections Inspector Click in the circle located to the right of

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

  • Implementing a Page based iOS 5 iPhone Application using UIPageViewController - Techotopia
    page controller by a data source The UIPageViewController DataSource In order to function a UIPageViewController instance must be assigned a data source which in turn is responsible for providing view controller objects as required for each page The data source takes the form of a class instance that implements the UIPageViewControllerDataSource protocol which at a minimum must implement the following two methods viewControllerAfterViewController This method is passed a view controller representing the currently displayed page and is required to return the view controller corresponding to the next page in the paging sequence viewControllerBeforeViewController This method is passed the view controller representing the currently displayed page and is required to return the view controller corresponding to the previous page in the paging sequence The mechanism used to create the requested view controllers and the content therein will generally be application specific and is at the discretion of the developer Apple does however recommend that in order to ensure optimal performance and minimal resource usage the view controllers be created on an as needed basis rather than pre created When a UIPageViewController object is initialized a number of configuration options may be specified to configure the appearance and behavior of the contained views Navigation Orientation The page controller is capable of transitioning between views using either a vertical or horizontal paradigm In the case of horizontal navigation page transitions take place in the same way pages are turned in a physical book by sweeping a finger on the screen either left or right In the case of horizontal navigation pages are turned by making vertical gestures in much the same way the pages of a wall calendar are flipped These options are configured using the following constants UIPageViewControllerNavigationOrientationHorizontal UIPageViewControllerNavigationOrientationVertical Spine Location The UIPageViewController class allows for the location of the spine to be configured The term spine in this context is analogous to the spine of a book and dictates the location of the axis on which each page will turn The behavior of the spine location settings vary depending on the navigation orientation setting For example the default for most configurations is UIPageViewControllerSpineLocationMin which places the spine on the left hand side or top of the screen depending on whether the navigation orientation is horizontal or vertical Similarly the UIPageViewControllerSpineLocationMax setting will position the spine at the right or bottom edge of the display In order to display two pages simultaneously the UIPageViewControllerSpineLocationMid setting should be used The view controller may also be configured to treat pages as being double sided via the doubleSided property Note that when using UIPageViewControllerSpineLocationMid spine location it will be necessary to provide the page controller with two view controllers one for the left hand page and one for the right for each page turn Similarly when using either the min or max spine location together with the double sided setting view controllers for both the front and back of the current page will be required for each page The UIPageViewController Delegate Protocol In addition to

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



  •