iOS: View Controllers
-
Upload
jussi-pohjolainen -
Category
Technology
-
view
1.492 -
download
2
Transcript of iOS: View Controllers
![Page 1: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/1.jpg)
iOS: View Controllers
Jussi Pohjolainen Tampere University of Applied Sciences
![Page 2: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/2.jpg)
Screen, Window, UIView
![Page 3: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/3.jpg)
View Controller
![Page 4: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/4.jpg)
Several Screens?
• Typical iOS app has several screens • Usually this is implemented so that you have several View Controller classes, one for each screen
• UIViewController class holds a property that points to UIView (which is visible in the screen)
![Page 5: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/5.jpg)
UIViewController
#import <UIKit/UIKit.h>
@interface MainScreenViewController : UIViewController
@end
![Page 6: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/6.jpg)
Adding a BuIon to the View of ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize a view, this could be a custom view also..
UIButton* button =
[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Main1" forState: UIControlStateNormal];
// Add the view to the controller
[self setView: button];
}
![Page 7: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/7.jpg)
Root Controller - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. MainScreenViewController* mainscreen = [[MainScreenViewController alloc] init]; [[self window] setRootViewController:mainscreen]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
![Page 8: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/8.jpg)
Root Controller?
• SeKng a view controller as root controller of a window – Add’s view controller’s view to window – AutomaMcally resizes the view to be the same size than window
![Page 9: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/9.jpg)
XIB?
• It’s usually wise to implement the UI in xib • You can create new empty xib file to your project
• Set the File’s owner to the view controller!
![Page 10: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/10.jpg)
View Controller and View
• The View Controller has a property View • In XIB, you can add to canvas a UIView and connect it as outlet of the view controller’s view!
![Page 11: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/11.jpg)
Content View Controller vs Container View Controller
• Content View Controller – Content on the screen using Views – Usually has a .xib
• Container View Controller – Content owned by other View Controllers – Parent to other controllers – Container manages a API to manage children (change screens)
– For example: Tab Bar Controller, Naviga<on Controller
![Page 12: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/12.jpg)
Tab Bar Controller
![Page 13: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/13.jpg)
NavigaMon Controller
![Page 14: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/14.jpg)
Complex App
![Page 15: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/15.jpg)
UITABVIEWCONTROLLER
![Page 16: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/16.jpg)
UITabController
• To change a view controller • Holds array of view controllers • Really easy to implement
![Page 17: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/17.jpg)
UITabController: Add Titles
• ViewController class has a property tabBarItem!
![Page 18: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/18.jpg)
Init in more detail
• The init method here loads the xib-‐file… you pass the xib-‐file name to the method:
![Page 19: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/19.jpg)
Init in more detail
• When iniMalizing the controller – SettingsViewController* settingsview = [[SettingsViewController alloc] init];
• This is equivalent to this! – SettingsViewController* settingsview = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
• What happens if nibname is nil? – Search for a xib file whose name is the same as your class and search inside of this app bundle!
![Page 20: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/20.jpg)
UINAVIGATIONCONTROLLER
![Page 21: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/21.jpg)
About Nav Controller
• Presents data organized hierarchically • Stack-‐based collecMon of view controllers – Stack: path taken by the user through the hierarchical data
– BoIom stack: starMng point, top stack: current posiMon
• Holds – NavigaMon bar, a back buIon and opMonal custom views
![Page 22: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/22.jpg)
![Page 23: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/23.jpg)
![Page 24: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/24.jpg)
CreaMng a UINavigaMonController // Create view controller
SettingsViewController* settingsview = [[SettingsViewController alloc] init];
// Create navigation controller.
// Initialize it with the first screen
UINavigationController* navController =
[[UINavigationController alloc]
initWithRootViewController:settingsview];
// Add navigation controller to window
[[self window] setRootViewController:navController];
![Page 25: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/25.jpg)
Modifying NavigaMon Stack
• Add another view to stack – [navController pushViewController:otherViewController animated:YES];
• To get pointer to navController in ViewController, use • [[self
navigationController] pushViewController:otherViewController animated:YES];
![Page 26: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/26.jpg)
Passing informaMon // Get text from UITextField
NSString* myText = [[self someTextField] text];
// Create the view controller
SettingsView1Controller* sv1 = [[SettingsView1Controller alloc] init];
// Pass the text to the view controller
[sv1 setText: myText];
// Push the view controller to nav controller. viewDidLoad is
// called!
[[self navigationController] pushViewController:sv1 animated:YES];
![Page 27: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/27.jpg)
Receiving informaMon @interface SettingsView1Controller : UIViewController { } // Attribute and set+get methods for text @property NSString* text; @property IBOutlet UILabel* label; * * * - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib.
// Set the given text to label: [[self label] setText: [self text]]; }
![Page 28: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/28.jpg)
The Nav Bar
![Page 29: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/29.jpg)
The Nav Bar
![Page 30: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/30.jpg)
UINavigaMonItem
• Set a Mtle – UINavigationItem *n = [self navigationItem]; – [n setTitle:@"Settings"];
• Other properMes – MtleView -‐> Can have any view on navigaMon bar – rightBarBuIonItem -‐> another buIon to the right – See documentaMon
![Page 31: iOS: View Controllers](https://reader033.fdocuments.us/reader033/viewer/2022052505/5555c0b0d8b42a52568b5449/html5/thumbnails/31.jpg)
Right BuIon