Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_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 InfoBarPickerController ()<UIPickerViewDelegate, | |
| 16 UIPickerViewDataSource> { | |
| 17 base::mac::ObjCPropertyReleaser _propertyReleaser_InfoBarPickerController; | |
| 18 id<InfoBarPickerControllerDelegate> _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 InfoBarPickerController | |
| 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_InfoBarPickerController.Init( | |
| 37 self, [InfoBarPickerController class]); | |
| 38 _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
| |
| 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 = [self.delegate infoBarPickerControllerInitialRow:self]; | |
| 57 [self.pickerView selectRow:initialRow inComponent:0 animated:NO]; | |
| 58 | |
| 59 self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero]; | |
| 60 UINavigationItem* navigationItem = | |
| 61 [[[UINavigationItem alloc] initWithTitle:@""] autorelease]; | |
| 62 [navigationItem setRightBarButtonItem:self.doneButton]; | |
| 63 [navigationItem setLeftBarButtonItem:self.cancelButton]; | |
| 64 [navigationItem setHidesBackButton:YES]; | |
| 65 [self.navigationBar pushNavigationItem:navigationItem animated:NO]; | |
| 66 | |
| 67 self.pickerView.translatesAutoresizingMaskIntoConstraints = NO; | |
| 68 self.navigationBar.translatesAutoresizingMaskIntoConstraints = NO; | |
| 69 UIStackView* stackView = [[[UIStackView alloc] | |
| 70 initWithArrangedSubviews:@[ self.navigationBar, self.pickerView ]] | |
| 71 autorelease]; | |
| 72 stackView.backgroundColor = [UIColor whiteColor]; | |
| 73 stackView.axis = UILayoutConstraintAxisVertical; | |
| 74 stackView.translatesAutoresizingMaskIntoConstraints = NO; | |
| 75 self.view = stackView; | |
| 76 } | |
| 77 | |
| 78 - (void)setDoneTarget:(id)target action:(SEL)action { | |
| 79 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-
| |
| 80 self.doneButton.action = action; | |
| 81 } | |
| 82 | |
| 83 - (void)setCancelTarget:(id)target action:(SEL)action { | |
| 84 self.cancelButton.target = target; | |
| 85 self.cancelButton.action = action; | |
| 86 } | |
| 87 | |
| 88 - (id<InfoBarPickerControllerDelegate>)delegate { | |
| 89 return _delegate; | |
| 90 } | |
| 91 | |
| 92 - (void)setDelegate:(id<InfoBarPickerControllerDelegate>)delegate { | |
| 93 _delegate = delegate; | |
| 94 } | |
| 95 | |
| 96 #pragma mark - UIPickerViewDataSource | |
| 97 | |
| 98 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView { | |
| 99 return 1; | |
| 100 } | |
| 101 | |
| 102 - (NSInteger)pickerView:(UIPickerView*)pickerView | |
| 103 numberOfRowsInComponent:(NSInteger)component { | |
| 104 return [self.delegate infoBarPickerControllerNumberOfRows:self]; | |
| 105 } | |
| 106 | |
| 107 #pragma mark - UIPickerViewDelegate | |
| 108 | |
| 109 - (UIView*)pickerView:(UIPickerView*)pickerView | |
| 110 viewForRow:(NSInteger)row | |
| 111 forComponent:(NSInteger)component | |
| 112 reusingView:(UIView*)view { | |
| 113 DCHECK_EQ(0, component); | |
| 114 UILabel* label = [view isKindOfClass:[UILabel class]] | |
| 115 ? (UILabel*)view | |
| 116 : [[[UILabel alloc] init] autorelease]; | |
| 117 [label setText:[self.delegate infoBarPickerController:self textForRow:row]]; | |
| 118 [label setTextAlignment:NSTextAlignmentCenter]; | |
| 119 UIFont* font = [UIFont systemFontOfSize:kUIPickerFontSize]; | |
| 120 if (row == [self.delegate infoBarPickerControllerInitialRow:self]) { | |
| 121 font = [UIFont boldSystemFontOfSize:kUIPickerFontSize]; | |
| 122 } | |
| 123 if ([self.delegate | |
| 124 respondsToSelector:@selector(infoBarPickerControllerDisabledRow:)]) { | |
| 125 if (row == [self.delegate infoBarPickerControllerDisabledRow:self]) { | |
| 126 label.enabled = NO; | |
| 127 } | |
| 128 } | |
| 129 [label setFont:font]; | |
| 130 return label; | |
| 131 } | |
| 132 | |
| 133 @end | |
| OLD | NEW |