繁体   English   中英

使用UIPickerView编辑UIButton的文本

[英]Edit text of UIButton with UIPickerView

我希望用户能够更改按钮的标题,这是我要执行的硬编码示例:

   [myButton setTitle:@"B" forState:UIControlStateNormal];

但是,我宁愿用UIPicker提示用户,并允许用户从许多选项中进行选择以更改标题。

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;

   if ([sender.view isKindOfClass:[UIButton class]]) {
      NSLog(@"user has clicked the button%@", sender.view);
      UIButton *myButton = (UIButton *)sender.view;
       UIPickerView *picker = [[UIPickerView alloc] init];
       myButton.titleLabel = picker;

   }

}

在这一点上,我看到一个错误*assignment to readonly property是否可以将UIPickerView与UIButton的setTitle属性一起使用? 如果是这样,如何完成?

错误很明显:

titleLabel是UIButton类的UILabel属性,并且是只读的(不能将其设置为另一个值)。 您需要改用其标签的text属性:

myButton.titleLabel.text = @"some text";

并不是:

myButton.titleLabel = picker;

要么

myButton.titleLabel.text = picker;

因为选择器不是NSString对象。

让我稍后解释如何实现此目标...

在您的View Controller类中,创建一个属性(我建议在私有接口中执行此操作),并实现UIPickerViewDataSource,UIPickerViewDelegate协议:

@interface PPMyViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *pickerView;

@end

现在,对pickerView属性使用惰性实例化:

- (UIPickerView *)pickerView
{
    if (!_pickerView)
    {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
    }

    return _pickerView;
}

Make sure you add to the main view, you can do it in the viewDidLoad method:

- (void)viewDidLoad
{
   [super viewDidLoad];

   [self.view addSubview:self.pickerView];
}


And implement most of the 2 protocols methods:

#pragma mark - UIPickerViewDataSource Methods

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 10;
}

#pragma mark - UIPickerViewDelegate Methods

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
{
    return 200;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"row %d", row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"Row %d selected", row);
//    myButton.titleLabel.text = @"text you need depending on row selection";
}

请注意,我的代码将使UIPickerView始终在底部可见。 您可以根据需要隐藏/取消隐藏它。 或更妙的是,使用动画将其放置在屏幕外而不是隐藏。

您无法像尝试那样去做。 您需要做的就是聆听用户对按钮的点击,然后向用户展示一个选择器。

有一些可以简化此操作的开源控件,例如ActionSheetPicker

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM