Chromium Code Reviews| Index: ios/chrome/browser/infobars/infobar_picker_controller.mm |
| diff --git a/ios/chrome/browser/infobars/infobar_picker_controller.mm b/ios/chrome/browser/infobars/infobar_picker_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c7b33374275e29bb6256f3b4b3bdbfbed6e5a76d |
| --- /dev/null |
| +++ b/ios/chrome/browser/infobars/infobar_picker_controller.mm |
| @@ -0,0 +1,133 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/infobars/infobar_picker_controller.h" |
| + |
| +#include "base/logging.h" |
| +#import "base/mac/objc_property_releaser.h" |
| + |
| +namespace { |
| +// Font size of text in the picker view. |
| +CGFloat kUIPickerFontSize = 26; |
| +} // namespace |
| + |
| +@interface InfoBarPickerController ()<UIPickerViewDelegate, |
| + UIPickerViewDataSource> { |
| + base::mac::ObjCPropertyReleaser _propertyReleaser_InfoBarPickerController; |
| + id<InfoBarPickerControllerDelegate> _delegate; // weak |
| +} |
| +// "Done" button presented in the navigation bar. |
| +@property(nonatomic, retain) UIBarButtonItem* doneButton; |
| +// "Cancel" button presented in the navigation bar. |
| +@property(nonatomic, retain) UIBarButtonItem* cancelButton; |
| +@end |
| + |
| +@implementation InfoBarPickerController |
| + |
| +@synthesize pickerView = _pickerView; |
| +@synthesize navigationBar = _navigationBar; |
| +@synthesize doneButton = _doneButton; |
| +@synthesize cancelButton = _cancelButton; |
| + |
| +- (instancetype)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle { |
| + self = [super initWithNibName:nibName bundle:nil]; |
| + if (self) { |
| + _propertyReleaser_InfoBarPickerController.Init( |
| + self, [InfoBarPickerController class]); |
| + _pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; |
|
marq (ping after 24h)
2016/06/30 16:10:11
View controllers usually create subviews in -viewD
Jackie Quinn
2016/06/30 18:00:39
I created them originally to make sure delegate/ta
marq (ping after 24h)
2016/07/01 11:48:44
- loadView is called when a UIViewController's |vi
|
| + _doneButton = [[UIBarButtonItem alloc] |
| + initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
| + target:nil |
| + action:nil]; |
| + _cancelButton = [[UIBarButtonItem alloc] |
| + initWithBarButtonSystemItem:UIBarButtonSystemItemCancel |
| + target:nil |
| + action:nil]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)loadView { |
| + self.pickerView.showsSelectionIndicator = YES; |
| + self.pickerView.backgroundColor = [UIColor whiteColor]; |
| + self.pickerView.delegate = self; |
| + self.pickerView.dataSource = self; |
| + NSInteger initialRow = [self.delegate infoBarPickerControllerInitialRow:self]; |
| + [self.pickerView selectRow:initialRow inComponent:0 animated:NO]; |
| + |
| + self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero]; |
| + UINavigationItem* navigationItem = |
| + [[[UINavigationItem alloc] initWithTitle:@""] autorelease]; |
| + [navigationItem setRightBarButtonItem:self.doneButton]; |
| + [navigationItem setLeftBarButtonItem:self.cancelButton]; |
| + [navigationItem setHidesBackButton:YES]; |
| + [self.navigationBar pushNavigationItem:navigationItem animated:NO]; |
| + |
| + self.pickerView.translatesAutoresizingMaskIntoConstraints = NO; |
| + self.navigationBar.translatesAutoresizingMaskIntoConstraints = NO; |
| + UIStackView* stackView = [[[UIStackView alloc] |
| + initWithArrangedSubviews:@[ self.navigationBar, self.pickerView ]] |
| + autorelease]; |
| + stackView.backgroundColor = [UIColor whiteColor]; |
| + stackView.axis = UILayoutConstraintAxisVertical; |
| + stackView.translatesAutoresizingMaskIntoConstraints = NO; |
| + self.view = stackView; |
| +} |
| + |
| +- (void)setDoneTarget:(id)target action:(SEL)action { |
| + self.doneButton.target = target; |
|
marq (ping after 24h)
2016/06/30 16:10:11
This (and its companion method) should never be ne
Jackie Quinn
2016/06/30 18:00:39
I agree... these seemed a bit silly to me. I didn'
marq (ping after 24h)
2016/07/01 11:48:44
This is one problem we have with having these not-
|
| + self.doneButton.action = action; |
| +} |
| + |
| +- (void)setCancelTarget:(id)target action:(SEL)action { |
| + self.cancelButton.target = target; |
| + self.cancelButton.action = action; |
| +} |
| + |
| +- (id<InfoBarPickerControllerDelegate>)delegate { |
| + return _delegate; |
| +} |
| + |
| +- (void)setDelegate:(id<InfoBarPickerControllerDelegate>)delegate { |
| + _delegate = delegate; |
| +} |
| + |
| +#pragma mark - UIPickerViewDataSource |
| + |
| +- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView { |
| + return 1; |
| +} |
| + |
| +- (NSInteger)pickerView:(UIPickerView*)pickerView |
| + numberOfRowsInComponent:(NSInteger)component { |
| + return [self.delegate infoBarPickerControllerNumberOfRows:self]; |
| +} |
| + |
| +#pragma mark - UIPickerViewDelegate |
| + |
| +- (UIView*)pickerView:(UIPickerView*)pickerView |
| + viewForRow:(NSInteger)row |
| + forComponent:(NSInteger)component |
| + reusingView:(UIView*)view { |
| + DCHECK_EQ(0, component); |
| + UILabel* label = [view isKindOfClass:[UILabel class]] |
| + ? (UILabel*)view |
| + : [[[UILabel alloc] init] autorelease]; |
| + [label setText:[self.delegate infoBarPickerController:self textForRow:row]]; |
| + [label setTextAlignment:NSTextAlignmentCenter]; |
| + UIFont* font = [UIFont systemFontOfSize:kUIPickerFontSize]; |
| + if (row == [self.delegate infoBarPickerControllerInitialRow:self]) { |
| + font = [UIFont boldSystemFontOfSize:kUIPickerFontSize]; |
| + } |
| + if ([self.delegate |
| + respondsToSelector:@selector(infoBarPickerControllerDisabledRow:)]) { |
| + if (row == [self.delegate infoBarPickerControllerDisabledRow:self]) { |
| + label.enabled = NO; |
| + } |
| + } |
| + [label setFont:font]; |
| + return label; |
| +} |
| + |
| +@end |