Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: ios/chrome/browser/infobars/infobar_picker_view_controller.mm

Issue 2107743002: Add InfoBarPickerController for displaying a picker view from an infobar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@update_passwords_strings
Patch Set: Rename InfoBarPickerController to InfoBarPickerViewController Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/infobars/infobar_picker_view_controller.h"
6
7 #include "base/logging.h"
8 #import "base/mac/objc_property_releaser.h"
9
10 namespace {
11 // Font size of text in the picker view.
12 CGFloat kUIPickerFontSize = 26;
13 } // namespace
14
15 @interface InfoBarPickerViewController ()<UIPickerViewDelegate,
16 UIPickerViewDataSource> {
17 base::mac::ObjCPropertyReleaser _propertyReleaser_InfoBarPickerViewController;
18 id<InfoBarPickerViewControllerDelegate> _delegate; // weak
19 }
20 // "Done" button presented in the navigation bar.
21 @property(nonatomic, retain) UIBarButtonItem* doneButton;
22 // "Cancel" button presented in the navigation bar.
23 @property(nonatomic, retain) UIBarButtonItem* cancelButton;
24 @end
25
26 @implementation InfoBarPickerViewController
27
28 @synthesize pickerView = _pickerView;
29 @synthesize navigationBar = _navigationBar;
30 @synthesize doneButton = _doneButton;
31 @synthesize cancelButton = _cancelButton;
32
33 - (instancetype)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle {
34 self = [super initWithNibName:nibName bundle:nil];
35 if (self) {
36 _propertyReleaser_InfoBarPickerViewController.Init(
37 self, [InfoBarPickerViewController class]);
38 _pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
39 _doneButton = [[UIBarButtonItem alloc]
40 initWithBarButtonSystemItem:UIBarButtonSystemItemDone
41 target:nil
42 action:nil];
43 _cancelButton = [[UIBarButtonItem alloc]
44 initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
45 target:nil
46 action:nil];
47 }
48 return self;
49 }
50
51 - (void)loadView {
52 self.pickerView.showsSelectionIndicator = YES;
53 self.pickerView.backgroundColor = [UIColor whiteColor];
54 self.pickerView.delegate = self;
55 self.pickerView.dataSource = self;
56 NSInteger initialRow =
57 [self.delegate infoBarPickerViewControllerInitialRow:self];
58 [self.pickerView selectRow:initialRow inComponent:0 animated:NO];
59
60 self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
61 UINavigationItem* navigationItem =
62 [[[UINavigationItem alloc] initWithTitle:@""] autorelease];
63 [navigationItem setRightBarButtonItem:self.doneButton];
64 [navigationItem setLeftBarButtonItem:self.cancelButton];
65 [navigationItem setHidesBackButton:YES];
66 [self.navigationBar pushNavigationItem:navigationItem animated:NO];
67
68 self.pickerView.translatesAutoresizingMaskIntoConstraints = NO;
69 self.navigationBar.translatesAutoresizingMaskIntoConstraints = NO;
70 UIStackView* stackView = [[[UIStackView alloc]
71 initWithArrangedSubviews:@[ self.navigationBar, self.pickerView ]]
72 autorelease];
73 stackView.backgroundColor = [UIColor whiteColor];
74 stackView.axis = UILayoutConstraintAxisVertical;
75 stackView.translatesAutoresizingMaskIntoConstraints = NO;
76 self.view = stackView;
77 }
78
79 - (void)setDoneTarget:(id)target action:(SEL)action {
80 self.doneButton.target = target;
81 self.doneButton.action = action;
82 }
83
84 - (void)setCancelTarget:(id)target action:(SEL)action {
85 self.cancelButton.target = target;
86 self.cancelButton.action = action;
87 }
88
89 - (id<InfoBarPickerViewControllerDelegate>)delegate {
90 return _delegate;
91 }
92
93 - (void)setDelegate:(id<InfoBarPickerViewControllerDelegate>)delegate {
94 _delegate = delegate;
95 }
96
97 #pragma mark - UIPickerViewDataSource
98
99 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
100 return 1;
101 }
102
103 - (NSInteger)pickerView:(UIPickerView*)pickerView
104 numberOfRowsInComponent:(NSInteger)component {
105 return [self.delegate infoBarPickerViewControllerNumberOfRows:self];
106 }
107
108 #pragma mark - UIPickerViewDelegate
109
110 - (UIView*)pickerView:(UIPickerView*)pickerView
111 viewForRow:(NSInteger)row
112 forComponent:(NSInteger)component
113 reusingView:(UIView*)view {
114 DCHECK_EQ(0, component);
115 UILabel* label = [view isKindOfClass:[UILabel class]]
116 ? (UILabel*)view
117 : [[[UILabel alloc] init] autorelease];
118 [label
119 setText:[self.delegate infoBarPickerViewController:self textForRow:row]];
120 [label setTextAlignment:NSTextAlignmentCenter];
121 UIFont* font = [UIFont systemFontOfSize:kUIPickerFontSize];
122 if (row == [self.delegate infoBarPickerViewControllerInitialRow:self]) {
123 font = [UIFont boldSystemFontOfSize:kUIPickerFontSize];
124 }
125 if ([self.delegate
126 respondsToSelector:@selector(
127 infoBarPickerViewControllerDisabledRow:)]) {
128 if (row == [self.delegate infoBarPickerViewControllerDisabledRow:self]) {
129 label.enabled = NO;
130 }
131 }
132 [label setFont:font];
133 return label;
134 }
135
136 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698