Download - Squeek school 4
![Page 1: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/1.jpg)
Разработка приложений для iOSзанятие #4
squeek school
суббота, 30 апреля 2011 г.
![Page 2: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/2.jpg)
View Controller
• Управляет содержимым экрана
• Наследуй от UIViewController, чтобы определить свою логику
• Несколько ViewController-ов могут совмещаться
“Your” and “Our” View Controllers• Create your own UIViewController subclass for each screenful• Plug them together using existing composite view controllers
View Controller
View Controller
View Controller
NavigationController
25Friday, January 22, 2010
“Your” and “Our” View Controllers• Create your own UIViewController subclass for each screenful• Plug them together using existing composite view controllers
View Controller
View Controller
View Controller
Tab BarController
25Friday, January 22, 2010
суббота, 30 апреля 2011 г.
![Page 3: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/3.jpg)
свой ViewController
#import <UIKit/UIKit.h>@interface MyViewController : UIViewController {NSMutableArray *myData; UILabel *myLabel;}
@property (readonly) NSArray *myData;- (void)doSomeAction:(id)sender;
view объявлен в UIViewController
Creating Your View in Code• Override -loadView
■ Never call this directly
• Create your views• Set the view property• Create view controller with -init
// Subclass of UIViewController- (void)loadView{
}
MyView *myView = [[MyView alloc] initWithFrame:frame];
[myView release];self.view = myView; // The view controller now owns the view
29Friday, January 22, 2010
суббота, 30 апреля 2011 г.
![Page 4: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/4.jpg)
свой ViewController
- (void)loadView {MyView *myView = [[MyView alloc] initWithFrame:frame];self.view = myView; [myView release];}
суббота, 30 апреля 2011 г.
![Page 5: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/5.jpg)
свой ViewController
• File - New File - View Controller
• автоматическое создание nib-файла
• создание нового объекта viewController при помощи -initWithNibName:bundle:
Creating Your View with Interface Builder• Lay out a view in Interface Builder• File’s owner is view controller class• Hook up view outlet• Create view controller
with -initWithNibName:bundle:
30Friday, January 22, 2010
суббота, 30 апреля 2011 г.
![Page 6: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/6.jpg)
Навигация• UINavigationController
• UITabBarController
• UIScrollView
суббота, 30 апреля 2011 г.
![Page 7: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/7.jpg)
UINavigationControllerUINavigationController
• Stack of view controllers• Navigation bar
View Controller
View Controller
View Controller
NavigationController
5Thursday, January 28, 2010
UINavigationController• Stack of view controllers• Navigation bar
View Controller
View Controller
View Controller
NavigationController
5Thursday, January 28, 2010
суббота, 30 апреля 2011 г.
![Page 8: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/8.jpg)
управление набором ViewController
- (void)pushViewController:(UIViewController *)viewControlleranimated:(BOOL)animated;
!!!вызывается retain
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
pop обычно не вызывается, автоматически при нажатии ‘<-’
суббота, 30 апреля 2011 г.
![Page 9: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/9.jpg)
первый ViewController
- (void)applicationDidFinishLaunching}
navController = [[UINavigationController alloc] init];
[navController pushViewController:firstViewController animated:NO];
[window addSubview:navController.view];}
суббота, 30 апреля 2011 г.
![Page 10: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/10.jpg)
а как использовать?
- (void)someAction:(id)sender {}
UIViewController *viewController = ...; [self.navigationController pushViewController:viewControlle animated:YES];
}
суббота, 30 апреля 2011 г.
![Page 11: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/11.jpg)
посмотрим?
•New Window-Based app•Новый ViewController с кнопкой•NavigationController и FirstViewController в AppDelegate•SecondViewController•Cвойство title у ViewController
суббота, 30 апреля 2011 г.
![Page 12: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/12.jpg)
как связать все ViewController?
• Не стоит использовать глобальные переменные или AppDelagate
• Что нужно на входе следующему ViewController? (и только это!)
Best Practices for Data Flow• Figure out exactly what needs to be communicated• Define input parameters for your view controller
List Controller
Detail Controller
Data
16Thursday, January 28, 2010
• В обратную сторону - используя делегаты (с помощью протоколов)
суббота, 30 апреля 2011 г.
![Page 13: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/13.jpg)
бонусы к навигацииCustomizing Navigation• Buttons or custom controls• Interact with the entire screen
20Thursday, January 28, 2010
суббота, 30 апреля 2011 г.
![Page 14: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/14.jpg)
UINavigationTitle
• Содержится у любого ViewControllerNavigation Item Ownership
View Controller
Right Bar Button Item
Navigation Item
Left Bar Button Item
Title View
22Thursday, January 28, 2010суббота, 30 апреля 2011 г.
![Page 15: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/15.jpg)
кнопки справа и слева
• UIBarButtonItem отображается сверху
• Отображает строку, изображение• Target + Action (как и во всех кнопках)
суббота, 30 апреля 2011 г.
![Page 16: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/16.jpg)
создание UIBarButtonItem
Text Bar Button Item
- (void)viewDidLoad{
UIBarButtonItem *fooButton = [[UIBarButtonItem alloc]initWithTitle:@"Foo”style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(foo:)];
self.navigationItem.leftBarButtonItem = fooButton;
[fooButton release];}
25Thursday, January 28, 2010
- (void)viewDidLoad {UIBarButtonItem *fooButton = [[UIBarButtonItem alloc] initWithTitle:@"Foo” style:UIBarButtonItemStyleBordered target:self action:@selector(foo:)];
self.navigationItem.leftBarButtonItem = fooButton; [fooButton release];
}
System Bar Button Item
- (void)viewDidLoad{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddstyle:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(add:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];}
26Thursday, January 28, 2010
- (void)viewDidLoad {UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd style:UIBarButtonItemStyleBordered target:selfaction:@selector(add:)];
self.navigationItem.rightBarButtonItem = addButton; [addButton release];}
суббота, 30 апреля 2011 г.
![Page 17: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/17.jpg)
кнопка редактирования
• реализованная практика• Very common pattern• Every view controller has one available
■ Target/action already set up
Edit/Done Button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Called when the user toggles the edit/done button- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
// Update appearance of views}
27Thursday, January 28, 2010
self.navigationItem.leftBarButtonItem = self.editButtonItem;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {}
суббота, 30 апреля 2011 г.
![Page 18: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/18.jpg)
вместо надписиCustom Title View• Arbitrary view in place of the title
UISegmentedControl *segmentedControl = ...self.navigationItem.titleView = segmentedControl;[segmentedControl release];
28Thursday, January 28, 2010
суббота, 30 апреля 2011 г.
![Page 19: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/19.jpg)
Скрыть клавиатуру
• подписаться на делегат у textField
• подписаться на событие -(BOOL)textFieldShouldReturn:(UITextField *)textField;
• [textField resignFirstResponder];
суббота, 30 апреля 2011 г.
![Page 20: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/20.jpg)
Дата в строку и обратно
• NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
• [formatter setDateFormat:@”DD-MM-YYYY”];
• NSDate *date = [formatter dateFromString:string];
• NSString *string = [formatter stringFromDate:date];
суббота, 30 апреля 2011 г.
![Page 21: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/21.jpg)
UITabBarControllerUITabBarController• Array of view controllers• Tab bar
Tab BarController
32Thursday, January 28, 2010
UITabBarController• Array of view controllers• Tab bar
View Controller
View Controller
View Controller
Tab BarController
32Thursday, January 28, 2010
суббота, 30 апреля 2011 г.
![Page 22: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/22.jpg)
используем UITabBarController
- (void)applicationDidFinishLaunching}
// Create a tab bar controllertabBarController = [[UITabBarController alloc] init];
// Set the array of view controllerstabBarController.viewControllers = myViewControllers;
// Add the tab bar controller’s view to the window[window addSubview:tabBarController.view];
- (void)applicationDidFinishLaunching}
// Create a tab bar controllertabBarController = [[UITabBarController alloc] init];
// Set the array of view controllerstabBarController.viewControllers = myViewControllers;
// Add the tab bar controller’s view to the window[window addSubview:tabBarController.view];
- (void)applicationDidFinishLaunching}
// Create a tab bar controllertabBarController = [[UITabBarController alloc] init];
// Set the array of view controllerstabBarController.viewControllers = myViewControllers;
// Add the tab bar controller’s view to the window[window addSubview:tabBarController.view];
- (void)applicationDidFinishLaunching{
tabBarController = [[UITabBarController alloc] init];
NSArray *myViewControllers = [NSArray arrayWithObjects:someViewController, nil];
tabBarController.viewControllers = myViewControllers;
[window addSubview:tabBarController.view];}
суббота, 30 апреля 2011 г.
![Page 23: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/23.jpg)
добавляем картинку и надписьCreating Tab Bar Items
• Title and image
- (void)viewDidLoad{
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@“Playlists” image:[UIImage imageNamed:@“music.png”] tag:0];self.tabBarItem = item;[item release];
}
36Thursday, January 28, 2010суббота, 30 апреля 2011 г.
![Page 24: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/24.jpg)
стандартный наборCreating Tab Bar Items• System item
- (void)viewDidLoad{
UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemBookmarks tag:0]self.tabBarItem = item;[item release];
}
37Thursday, January 28, 2010
суббота, 30 апреля 2011 г.
![Page 25: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/25.jpg)
а если слишком много?
More View Controllers• What happens when a tab bar controller has too many
view controllers to display at once?
39Thursday, January 28, 2010
More View Controllers• What happens when a tab bar controller has too many
view controllers to display at once?■ “More” tab bar item
displayed automatically■ User can navigate to
remaining view controllers
39Thursday, January 28, 2010суббота, 30 апреля 2011 г.
![Page 26: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/26.jpg)
Tab Bar + Navigation ControllersMultiple parallel hierarchies
41Thursday, January 28, 2010суббота, 30 апреля 2011 г.
![Page 27: Squeek school 4](https://reader033.vdocuments.pub/reader033/viewer/2022052904/557eff95d8b42af44a8b4cbf/html5/thumbnails/27.jpg)
архитектураTab Bar + Navigation Controllers
Navigation Controller
View Controller
Navigation Controller
View Controller
View ControllerTab Bar Controller
42Thursday, January 28, 2010
суббота, 30 апреля 2011 г.