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 Build Dynamic iPad TableViews with Prototype Table View Cells - Techotopia
    outlets that we will later declare in the CarTableViewCell subclass Using Interface Builder components may 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 20 4 making sure to stretch the label objects so that they extend toward the right hand edge of the cell Note also that the height of the cell itself has been increased Figure 20 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 synthesize modelLabel synthesize 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 20 5 Figure 20 5 Upon releasing the pointer select makeLabel from the resulting menu to establish the outlet connection Figure 20 6 Repeat these steps to connect the carModels 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

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

  • Implementing iPad TableView Navigation using Xcode Storyboards - Techotopia
    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 21 2 Figure 21 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 21 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 need to be updated with the

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

  • Using an Xcode Storyboard to Create a Static iPad Table View - Techotopia
    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 note that the storyboard view must be zoomed in at this point 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 22 1 Figure 22 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 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 22 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 22 3 Figure 22 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

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

  • Creating a Simple iOS 5 iPad Table View Application - Techotopia
    the class declaration the code will still compile but we will not be warned if we have 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 interface TableExampleViewController end 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 by the ARC when the array is no longer required To do so it is recommended 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 the end of your TableExampleViewController m file pragma mark Table view data source 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

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

  • Creating a Navigation based iOS 5 iPad Application using TableViews - Techotopia
    by creating an instance of our root view controller class initializing it with the RootViewController NIB user interface file and assigning the object reference to a variable named rootController Next a UINavigationController instance is created and assigned to the navigationController variable As part of the navigation controller initialization process 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 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 interface RootViewController end 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 respectively 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 lines we have to add are as follows if cell nil cell UITableViewCell alloc initWithStyle UITableViewCellStyleDefault reuseIdentifier CellIdentifier cell textLabel text self authorList objectAtIndex indexPath row These lines of code simply create a new cell object if one is needed identify the row requested use that value as the index into our array of authors and assigns it to the appropriate property of the cell 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 24 3 Figure 24 3 When the user selects an author we want to navigate to another table view that lists 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 Objective C class 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 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

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

  • An iPad iOS 5 Split View and Popover Example - Techotopia
    in turn is assigned as root view controller of the window prior to being displayed Configuring Master View Items The root view controller created for us by Xcode is actually a subclass of UITableView The next step therefore is to configure the table view to display the list of web sites For this purpose we will need to configure two array objects to store the web site names and corresponding URL addresses Select the splitViewMasterViewController h file and modify it as follows to declare these two arrays import UIKit UIKit h class splitViewDetailViewController import CoreData CoreData h interface splitViewMasterViewController UITableViewController NSFetchedResultsControllerDelegate property nonatomic retain NSArray siteNames property nonatomic retain NSArray siteAddresses property strong nonatomic splitViewDetailViewController detailViewController property strong nonatomic NSFetchedResultsController fetchedResultsController property strong nonatomic NSManagedObjectContext managedObjectContext end Having declared the arrays modify the splitViewMasterViewController m file to add the synthesize directive and to initialize the arrays in the viewDidLoad method Note that Xcode has already added some code to the viewDidLoad method for the template example so be sure to remove this before adding the new code below Also be sure to add code to release memory allocated to these arrays when the application exits import splitViewMasterViewController h import splitViewDetailViewController h interface splitViewMasterViewController void configureCell UITableViewCell cell atIndexPath NSIndexPath indexPath end implementation splitViewMasterViewController synthesize detailViewController detailViewController synthesize fetchedResultsController fetchedResultsController synthesize managedObjectContext managedObjectContext synthesize siteNames siteAddresses void viewDidLoad super viewDidLoad Do any additional setup after loading the view typically from a nib siteNames NSArray alloc initWithObjects Yahoo Google Apple eBookFrenzy nil siteAddresses NSArray alloc initWithObjects http www yahoo com http www google com http www apple com http www ebookfrenzy com nil self tableView selectRowAtIndexPath NSIndexPath indexPathForRow 0 inSection 0 animated NO scrollPosition UITableViewScrollPositionMiddle void viewDidUnload Relinquish ownership of anything that can be recreated in viewDidLoad or on demand For example self myOutlet nil self siteNames nil self siteAddresses nil As outlined in the chapter entitled Creating a Simple iOS 5 iPad Table View Application there are a number of methods that must be implemented in order for the items to appear within the table view object Fortunately Xcode has already placed template methods for us to use in the splitViewMasterViewController m file First modify the numberOfRowsInSection method to notify the table view of the number of items to be displayed in this case a value equal to the number of items in our siteNames array Since there is only one section in the table also modify the numberOfSectionsInTableView method accordingly Note that Xcode has once again added some code for the template example so be sure to remove this code NSInteger numberOfSectionsInTableView UITableView tableView return 1 NSInteger tableView UITableView tableView numberOfRowsInSection NSInteger section return siteNames count Next modify the cellForRowAtIndexPath method to return the item to be displayed using the row number argument as an index into the siteNames array 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 self configureCell cell atIndexPath

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

  • Implementing a Page based iOS 5 iPad Application using UIPageViewController - Techotopia
    for the 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

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

  • An Example iOS 5 iPad UIPageViewController Application - Techotopia
    time the user turns a page in the application the data source methods for a UIPageViewController object are going to create a new instance of our contentViewController class and set the dataObject property to the HTML that is to be displayed on the web view object As such the viewWillAppear method of contentViewController needs to assign the value stored in the dataObject property to the web view object To achieve this behavior add the appropriate synthesize directive to the file followed by the viewWillAppear method to assign the HTML to the web view import contentViewController h interface contentViewController end implementation contentViewController synthesize webView dataObject void viewWillAppear BOOL animated super viewWillAppear animated webView loadHTMLString dataObject baseURL NSURL URLWithString end At this point work on the content view controller is complete The next step is create the data model for the application Creating Data Model The data model for the application is going to consist of an array object containing a number of string objects each configured to contain slightly different HTML content For the purposes of this example the data source for the UIPageViewController instance will be the application s pageAppViewController class This class will therefore need references to an NSArray and a UIPageViewController object It will also be necessary to declare this class as implementing the UIPageViewControllerDataSource protocol Select the pageAppViewController h file and add these references as follows together with an import directive for the contentViewController h file import UIKit UIKit h import contentViewController h interface pageAppViewController UIViewController UIPageViewControllerDataSource property strong nonatomic UIPageViewController pageController property strong nonatomic NSArray pageContent end The final step in creating the model is to add a method to the pageAppViewController m file to add the HTML strings to the array and then call that method from viewDidLoad note also the addition of the synthesize directive import pageAppViewController h interface pageAppViewController end implementation pageAppViewController synthesize pageController pageContent void createContentPages NSMutableArray pageStrings NSMutableArray alloc init for int i 1 i 11 i NSString contentString NSString alloc initWithFormat html head head body h1 Chapter d h1 p This is the page d of content displayed using UIPageViewController in iOS 5 p body html i i pageStrings addObject contentString pageContent NSArray alloc initWithArray pageStrings void viewDidLoad super viewDidLoad self createContentPages The application now has a content view controller and a data model from which the content of each page will be extracted by the data source methods The next logical step therefore is to implement those data source methods As previously outlined in Implementing a Page based iOS 5 iPad Application using UIPageViewController instances of the UIPageViewController class need a data source This takes the form of two methods one of which is required to return the view controller to be displayed after the currently displayed view controller and the other the view controller to be displayed before the current view controller Since the pageAppViewController is going to act as the data source for the page view controller object these two methods together with two convenience methods which

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



  •