Transcript Segues

Managing Multiple Views
and Segues
FA 172
Intro to Mobile App Development
Multiple Views
• Recall the RiddleApp the class created on the
first day of classes
• Used a navigation controller, multiple views,
and segues
• For today, we will learn to manage multiple
views and pass data (objects) between views
Product inventory app
• Three projects
• ProductInvAppPrototype
– Focus on user interface
• ProductTest
– Focus on testing an existing product class
• ProductInvApp
– Create the final app
ProductInvAppPrototype
• Create a single view project with three buttons
• Embed in a navigation controller
– Select the view controller, then
Navigator -> Embed In -> Navigation Controller
• Add one view controller to the storyboard with a
label and an image view
– Set label text to some product name (e.g., “Apple”)
– Get an image file from the web representing the
product (e.g., apple.jpeg), then drag onto
“Supporting Files” section
– Select the ImageView control, set the image file name
ProductInvAppPrototype
• Make two more copies of the view controller
(that contains a label and an image)
– Update these two copies so the three views
represent different products (e.g., apple, orange
and pomelo)
• Control-drag from each button to a view
controller to create the corresponding segues
– Select “push”
• Build and execute
ProductTest
• Create a single-view application with a single button
– Add an action connection
– Test code will be placed in the corresponding method
• Download the Product class (Product.h and Product.m)
– Three properties: name, imgFileName, and stockLevel
(the first two are of type NSString* the third is an int)
– An initWith… method that initializes the properties
– Methods to increase/decrease stockLevel:
-(void) acceptDelivery:(int) qty
-(void) releaseStock:(int) qty
• Test the Product class (test code provided in website)
ProductInventoryApp
• Follow instructions posted on the course website
• Summary
– Two view controllers (controlled by a navigation
controller), one for the main interface, the other for
the product
– Product class must be included in the project
– Three products created by main view controller
– Detail/product view controller has its display based on
a product object
ProductInventoryApp: main view
• No properties defined in ViewController.h file
• No outlet connections
• No action connections (button click functionality handled
through the segues)
• Three product variables declared outside of the methods
within the ViewController.m file (these could have been
properties but there is no reason to have the data available
as properties)
• Product objects created within viewDidload method
(initialization code executed when view appears)
• prepareForSegue method checks which button was pressed
(which segue was used) and then sets the appropriate
product to be set by the second view
ProductInventoryApp: product view
• Separate class (with superclass UIViewController) needs to be
created and associated to this view controller
• Properties defined in .h file
– outlet connections for all controls (except buttons)
– One product object (to be set by the main view, but used by this view
for display)
• Action connections (for the add and remove buttons)
• viewDidload contains code (in .m file) that displays details of the
product object
• Code for the add/remove actions
– Retrieve data from the text field and convert to int
– Call methods to increase/decrease stock on the product object
– Redisplay stock level
ProductInventoryApp