用户从 xcode 中的多组件选择器中选择某些值后,如何显示某个消息/警报?

Posted

技术标签:

【中文标题】用户从 xcode 中的多组件选择器中选择某些值后,如何显示某个消息/警报?【英文标题】:How can I display a certain message/alert after the user chooses certain values from a multicomponent picker in xcode? 【发布时间】:2020-11-12 17:51:54 【问题描述】:

当用户从双 component picker 中选择 2 个值时,我试图显示一条消息。

如果用户选择“高烧”和“剧烈咳嗽”,请使用警报视图显示一条消息,指示:“您应该进行 PCR 测试”。

如果用户选择“高烧”和“中度咳嗽”或“中度发烧”和“严重咳嗽”,则显示一条消息,指示:“这只是普通流感”。

在所有其他可能选择的情况下,显示一条消息:“放松...没什么好担心的!”

下面是代码:

#import "FirstViewController.h"
#define kFeverComponent 0
#define kCoughComponent 1

@interface FirstViewController ()
@property (strong, nonatomic) NSArray *feverTypes;
@property (strong, nonatomic) NSArray *coughTypes;
@property (weak, nonatomic) IBOutlet UIPickerView *dependentPicker;

@end

@implementation FirstViewController

- (void)viewDidLoad 
   [super viewDidLoad];
   self.feverTypes = @[@"High Fever", @"Medium Fever", @"No Fever"];
   self.coughTypes = @[@"Strong Cough", @"Medium Cough", @"No Cough"];

- (void)didReceiveMemoryWarning 
   [super didReceiveMemoryWarning];

- (IBAction)symptomsButtonPressed:(id)sender 
   NSInteger feverRow = [self.dependentPicker selectedRowInComponent: kFeverComponent];
   NSInteger coughRow = [self.dependentPicker selectedRowInComponent: kCoughComponent];
   NSString *fever = self.feverTypes[feverRow];
   NSString *cough = self.coughTypes[coughRow];
   NSString *message = [[NSString alloc] initWithFormat:
     @"You chose %@ and %@.", fever, cough];
   UIAlertController *alert =
     [UIAlertController
       alertControllerWithTitle:@"Thank you for choosing" message:message 
       preferredStyle:UIAlertControllerStyleAlert];
   UIAlertAction *action = [UIAlertAction actionWithTitle:@"Okay" 
     style:UIAlertActionStyleDefault handler:nil];
   [alert addAction:action];
   [self presentViewController:alert animated:YES completion:nil];

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
   return 2;

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: 
   (NSInteger)component 
   if (component == kCoughComponent) 
     return [self.coughTypes count];
 else 
    return [self.coughTypes count];



#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
    titleForRow:(NSInteger)row
    forComponent:(NSInteger)component 
    if (component == kCoughComponent) 
      return self.coughTypes[row];
     else 
      return self.feverTypes[row];



@end

【问题讨论】:

【参考方案1】:

您需要实现pickerview:didSelectRow:inComponent 委托方法。所以让你的类遵守UIPickerViewDelegate 协议,将其添加到接口定义中,例如

@interface FirstViewController () < UIPickerViewDelegate >

然后实施例如

- ( void ) pickerView:( UIPickerView * ) pickerView
         didSelectRow:( NSInteger      ) row
          inComponent:( NSInteger      ) component

    // logic here e.g.
    NSInteger fever = [pickerView selectedRowInComponent:kCoughComponent];
    NSInteger cough = [pickerView selectedRowInComponent:kFeverComponent];
    NSString * msg;

    if ( fever >= threshold && cough >= threshold )
    
       msg = @"Seek help";
    
    else
    
       msg = @"All fine";
    

    // Show alert with msg

【讨论】:

以上是关于用户从 xcode 中的多组件选择器中选择某些值后,如何显示某个消息/警报?的主要内容,如果未能解决你的问题,请参考以下文章

在 Xcode 中为两个相同的选择器组件指定数组

有选择地绘制到着色器中的某些输出

从android中的意图选择器中选择选项(相机或画廊)后请求权限

iPhone中的多值选择/取消选择

Laravel-从下拉列表中选择值后自动填充输入

角度 6:在选择“选择”的值后尝试循环抛出一个对象(通过 *ngFor)