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 Overview of the iPhone iOS 4 Application Development Architecture - Techotopia
    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 The Target Action pattern IBOutlets and IBActions google IOSBOX google When you create an iOS iPhone app you will typically design the user interface the view using the Interface Builder tool and write the view controller and model code in Objective C 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 Objective C 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 to be called when an event is triggered in a view object This allows us to connect a view object created within a nib file using Interface Builder to the code that we have written in the view controller object This is one of the ways that we bridge the separation between the View and the Controller in our MVC design pattern As we will see in Creating an Interactive iOS 4 iPhone App action methods are declared using the IBAction keyword The opposite to an Action is the Outlet As previously described an Action allows a view object to call a method on a controller object An Outlet on the other hand allows a view controller object method to directly access the properties of a view object A view controller might for example need to set the text on a UILabel object In order to do so an

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


  • Creating an Interactive iOS 4 iPhone App - Techotopia
    class belonging to the UIKit framework The next step is to extend the subclass to include the two outlets and our action method We will begin by adding outlets for our text field and label objects by declaring variables and using the IBOutlet keyword import UIKit UIKit h interface UnitConverterViewController UIViewController UITextField tempText UILabel resultLabel property nonatomic retain IBOutlet UILabel resultString property nonatomic retain IBOutlet UITextField tempString end Next we need to declare the action that will be called when the user touches the Convert button in our user interface This is declared using the IBAction keyword import UIKit UIKit h interface UnitConverterViewController UIViewController UITextField tempText UILabel resultLabel property nonatomic retain IBOutlet UILabel resultLabel property nonatomic retain IBOutlet UITextField tempText IBAction convertTemp id sender end We have now declared that our View Controller class contains a method called convertTemp Having made that declaration we now need to implement the method in the implementation source file To do so select the UnitConverterViewController m file so that the contents appear in the editing pane import UnitConverterViewController h implementation UnitConverterViewController The designated initializer Override to perform setup that is required before the view is loaded id initWithNibName NSString nibNameOrNil bundle NSBundle nibBundleOrNil if self super initWithNibName nibNameOrNil bundle nibBundleOrNil Custom initialization return self Implement loadView to create a view hierarchy programmatically without using a nib void loadView Implement viewDidLoad to do additional setup after loading the view typically from a nib void viewDidLoad super viewDidLoad Override to allow orientations other than the default portrait orientation BOOL shouldAutorotateToInterfaceOrientation UIInterfaceOrientation interfaceOrientation Return YES for supported orientations return interfaceOrientation UIInterfaceOrientationPortrait void didReceiveMemoryWarning Releases the view if it doesn t have a superview super didReceiveMemoryWarning Release any cached data images etc that aren t in use void viewDidUnload Release any retained subviews of the main view e g self myOutlet nil void dealloc super dealloc end The first step is to instruct Objective C to synthesize accessor methods for our tempText and resultLabel objects and then to implement the convertTemp method The relevant section of the UnitConverterViewController m file should now read as follows import UnitConverterViewController h import UnitConverterViewController h implementation UnitConverterViewController synthesize resultLabel tempText void convertTemp id sender double farenheit tempText text doubleValue double celsius farenheit 32 1 8 NSString resultString NSString alloc initWithFormat Celsius f celsius resultLabel text resultString resultString release Before we proceed it is probably a good idea to pause and explain what is happening in the above code Those already familiar with Objective C however may skip the next few paragraphs In this file we are implementing the convertTemp method that we declared in the h file This method takes as a single argument a reference to the sender The sender is the object that triggered the call to the method in this case our Button object Whilst we won t be using this object in the current example this can be used to create a general purpose method in which the behavior of the method changes depending on how i e

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

  • Writing iOS 4 Code to Hide the iPhone Keyboard - Techotopia
    the text field object in the View window From the resulting menu select the textField outlet Save the design and exit the Interface Builder tool Hiding the Keyboard when the User Touches the Return Key The next step is to wire up our application so that the keyboard is dismissed when the user touches the keyboard Return key To do so we need to write a method that will resign the first responder on the text field in other words hide the keyboard when the return key is pressed Begin by editing the hideKeyboardViewController h interface file to declare the method which we will name textFieldReturn import UIKit UIKit h interface hideKeyboardViewController UIViewController UITextField textField property nonatomic retain IBOutlet UITextField textField IBAction textFieldReturn id sender end Having declared the method we now need to implement it in the hideKeyboardViewController m implementation file import hideKeyboardViewController h implementation hideKeyboardViewController synthesize textField IBAction textFieldReturn id sender sender resignFirstResponder end google ADSDAQBOX FLOW google In the above method we are making a call to the resignFirstResponder method of the object that triggered the event The first responder is the object with which the user is currently interacting in this instance the virtual keyboard displayed on the iPhone screen Having written the code for our method we now need to wire up our user interface so that it gets called at the appropriate time We will perform this task in Interface Builder so double click on the hideKeyboardViewController xib file to load the file once more Once Interface Builder has loaded select the text field in the view window and display the Connections Inspector window Tools Connections Inspector or Command 2 Click on the circle to the right of the Did End on Exit event drag the line to the File s Owner icon and select textFieldReturn from the list of available methods Save the design exit from Interface Builder and click on the Build and Run button in the Xcode toolbar When the application appears in the iOS Simulator select the text field so that the keyboard appears and then touch the Return key The keyboard should subsequently disappear from view Hiding the Keyboard when the User Taps the Background The second mechanism for hiding the keyboard involves wiring up an event to be called when the user touches the background view of the screen We will begin the process by writing the action method to perform this task From within Xcode select the hideKeyboardViewController h file and add a declaration for our new backgroundTouched action method import UIKit UIKit h interface hideKeyboardViewController UIViewController UITextField textField property nonatomic retain IBOutlet UITextField textField IBAction textFieldReturn id sender IBAction backgroundTouched id sender end Save the file then open the hideKeyboardViewController m file and implement the action by calling the resignFirstResponder method of our textField object import hideKeyboardViewController h implementation hideKeyboardViewController synthesize textField IBAction textFieldReturn id sender sender resignFirstResponder IBAction backgroundTouched id sender textField resignFirstResponder end google IOSBOX google Having written the code for our action method

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

  • Understanding iPhone iOS 4 Views, Windows and the View Hierarchy - Techotopia
    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 In the example applications we have created in previous chapters the UIWindow instance is contained in its own NIB file with the name MainWindow xib The View Hierarchy iPhone iOS 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 4 iPhone 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 the following figure 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 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 If we were to design the above nested view hierarchy in Interface Builder it might appear as illustrated in the following figure google IOSBOX google In this example the UIWindow instance is not visible because it is fully obscured by the blue UIView1 instance Displayed on top of and within the frame of UIView1 are the UIButton1 UILabel and yellow UIView2 subviews Displayed on top of and within the frame of UIView2 are its respective subviews namely UIButton2 and UIImageView 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 dealt with View Types Apple groups the various views included in the UIKit framework into a number of different categories The Window The

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

  • IOS 4 iPhone Rotation, View Resizing and Layout Handling - Techotopia
    into Interface Builder add an additional button to the interface and reposition both buttons so that they appear as outlined in the following figure In order to gain an appreciation of the importance of handling orientation changes save the design and build and run the application Once the simulator appears rotate the device by 90⁰ Because we have enabled rotation the view will rotate to the landscape orientation Here we see a classic example of the implications of enabling rotation in an application without taking the additional steps to reformat the view to match the change in orientation Having rotated the device only the top button is visible and it is still centered assuming that the device is in portrait mode The second button is similarly positioned resulting in it being outside the viewable area of the screen In order to ensure the correct layout of the user interface it is necessary to make some changes to the autosize settings of the two buttons The current settings for a view are accessed from within Interface Builder by selecting the component and displaying the Size Inspector Tools Size Inspector or Command 3 The following figure shows the Size Inspector for the top button in our user interface google IOSBOX google The section of this window that is of interest to us is the area entitled Autosizing and in particular the white box on the left The outer box represents the superview and the inner box represents the subview that is contained within the superview Any autosizing settings that are configured on a subview are relative to the superview Within the subview box are two intersecting lines with arrows at each end These vertical and horizontal lines indicate whether the height and width of the selected view object should increase or decrease in proportion to size changes in the corresponding superview A faint dotted line indicates that the currently selected view object is not to resize in relation to the superview A solid red line on the other hand indicates that it should resize proportionally to the parent view The graphic to the right of the white box provides a visual representation of how the view will behave based on the current settings The red box represents the subview and the white box represents the containing superview Click on the horizontal line to change it to solid red and see how the width of the red box representing the button grows and shrinks as the containing superview size changes Similarly enable resizing for the height of the button and observe how the height of the button also now changes proportionally to the superview These two settings correspond to the UIViewAutoResizingFlexibleHeight and UIViewAutoResizingFlexibleWidth autoresizing mask properties The lines separating the four sides of the inner subview and the outer superview boxes are referred to as struts and control what happens to the margin between each side of the subview and the corresponding sides of the superview when the latter resizes as the result

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

  • Creating an iOS 4 iPhone Multiview Application using the Tab Bar - Techotopia
    switch between using a tab bar Each content view will need a view controller and a NIB file to contain the user interface To create these right click over the Classes category located in the Groups and Files panel on the left hand side of the main Xcode project window and select Add New File This will display the template selection screen as illustrated in the following figure In the left hand panel verify that the iOS Cocoa Touch Class option is selected and that the current selection in the main panel is the UIViewController subclass icon Since we also need an Interface Builder NIB file and are developing for the iPhone make sure that the With XIB for Interface Builder option is selected and the Targeted for iPad option is de selected before clicking on the Next button On the following screen specify ScreenOneViewController m as the file name and verify that the Also create ScreenOneViewController h option is selected Click Finish to create the new view controller class files and corresponding Interface Builder file Repeat the above procedure to create the second content view and view controller this time using the name ScreenTwoViewController Configuring the App Delegate The next class we are going to add to our project is the root controller Before we do so however we need to add an outlet to this controller within the app delegate file and also add some code to the didFinishLaunchWithOptions method to add the view instance of the root controller as a subview of the application s main window Select the TabBar item at the top of the Groups and Files panel so that all the files in the project are listed Select the TabBarAppDelegate h file and modify it as follows in the editing panel import UIKit UIKit h interface TabBarAppDelegate NSObject UIApplicationDelegate UITabBarController tabController UIWindow window property nonatomic retain IBOutlet UIWindow window property nonatomic retain IBOutlet UITabBarController tabController end Next edit the TabBarAppDelegate m implementation file and modify the code as follows import TabBarAppDelegate h implementation TabBarAppDelegate synthesize tabController synthesize window pragma mark pragma mark Application lifecycle BOOL application UIApplication application didFinishLaunchingWithOptions NSDictionary launchOptions Override point for customization after application launch window addSubview tabController view self window makeKeyAndVisible return YES void dealloc tabController release window release super dealloc end Now that we have made the appropriate additions to the app delegate subclass we can now add the root view controller to the project Creating the UITabBarController As previously mentioned because we are designing a tab bar based multiview application it makes sense for us to use an instance of the UITabBarController class as the root controller This is achieved from within MainWindow xib so double click on this file to load it into Interface Builder From within the Library window click and drag a Tab Bar Controller object over to a blank area of the MainWindow xib window Once the object has been added the list of items in the MainWindow xib window should appear as follows

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

  • Creating a Simple iOS 4 iPhone Table View Application - Techotopia
    of the dataSource outlet and drag the blue line to the File s Owner icon located in the TableExampleViewController xib document window Once the connection is established repeat this task for the delegate outlet The connections should now be displayed as follows Now that the connections have been established save the file and exit from Interface Builder Implementing the dataSource In order to bring our table view application to life we need to implement two dataSource methods in our view controller Before doing so however we need to create the data itself In this example we are going to create an NSArray object and initialize it with some strings In the main Xcode project window select the TableExampleViewController h file and modify it in the editing pane to declare the array import UIKit UIKit h interface TableExampleViewController UIViewController UITableViewDelegate UITableViewDataSource NSArray colorNames property nonatomic retain NSArray colorNames end Note the references to UITableViewDelegate and UITableViewDataSource in the above code These are included to notify the Objective C compiler that the class implements these two protocols Consequently if we fail to implement the mandatory methods required by these protocols the compiler will notify us with a warning If this line is omitted from 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 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 uncomment it by removing the and markers and modify the method as follows Implement viewDidLoad to do additional setup after loading the view typically from a nib 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 and dealloc methods void viewDidUnload Release any retained subviews of the main view e g self myOutlet nil self colorNames nil void dealloc colorNames release super dealloc 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

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

  • Creating a Navigation based iOS 4 iPhone Application using TableViews - Techotopia
    created template of this method currently coded to return 0 In our example we want to return a value equivalent to the number of items in our authorList array so we need to modify this method accordingly NSInteger tableView UITableView tableView numberOfRowsInSection NSInteger 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 autorelease 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 that has been requested uses it as the index into our array of authors and assigns it to the appropriate property of the cell object that was created for us in the preceding lines of the method Now is a good time to make sure we haven t 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 follows google IOSBOX google When we click on an author we want to navigate to another table view that provides a list of books written by that 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 two files and configure them to meet our 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 right click on Classes under Groups Files in the main Xcode project window and select Add New File On the template selection screen select iOS Cocoa Touch Class followed by the UIViewController subclass icon Make sure that the UITableViewController subclass option is selected and that the Targeted for iPad option is off We will manually create the NIB file later so leave the With XIB for user interface option unchecked Click on Next and name the class BooksViewController Since we will need an interface header file leave the Also create BooksViewController h option selected and click Finish to create the files 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 and add code to forward reference the BooksViewController class and add an outlet to the new view controller class BooksViewController interface RootViewController UITableViewController NSArray authorList BooksViewController booksController property nonatomic retain IBOutlet BooksViewController booksController property nonatomic retain NSArray authorList end Before we move on to the next step be sure to save the above changes to the RootViewContoller h file by selecting File Save so that the outlet is visible to us from inside Interface Builder Failure to save the file at this point may result in the outlet not appearing when we try to create connections from within Interface Builder Connecting the Second View Controller to the Root View Controller Load RootViewController xib into Interface Builder and drag a new View Controller UIViewController object from the Library window Tools Library or Command L to the RootViewController xib document window Open the Identity Inspector Tools Identity Inspector or Command 4 and change the Class from UIViewController to BooksViewController Select the File s Owner icon and open the Connections Inspector Tools Connections Inspector or Command 2 Ctrl click in the circle located to the right of the booksController outlet and drag the blue line to the Books View Controller item in the RootViewController xib window to make the connection to the outlet Save the design and exit from Interface Builder Creating the NIB File for the Second Table View The next step is to create the NIB file for our second view controller This will contain the instance of the table view that we will use to display the books that the selected author has written In the main Xcode window right click on the Resources entry listed in the Groups Files panel and select Add New File In the New File dialog select User Interface under the iOS list and select the View XIB icon in the main panel Click Next and name the file BooksViewController xib before clicking the Finish button to create the file Double click on BooksViewController xib in the main project window to load it into Interface Builder At this point the file currently contains a UIView object when we actually want it to contain a UITableView In the BooksViewController xib document window select the View icon and press the Delete key on the keyboard to remove it from the design From the Library window Tools Library or Command L drag a Table View object into the document window The design window will subsequently update to reflect the presence of the table view populating it with some dummy data If the view window does not appear as illustrated in the previous figure simply double click on the Table View icon in the document window Select the File s Owner icon display the Identity Inspector and change the class to BooksViewController With the File s Owner item still selected display the Connections Inspector Tools Connections Inspector or Command 2

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



  •