UIButton 响应 UIPickerview

Posted

技术标签:

【中文标题】UIButton 响应 UIPickerview【英文标题】:UIButton respond to a UIPickerview 【发布时间】:2014-07-11 14:39:25 【问题描述】:

所以我有一个UIPickerViewUIButton

基本上我已经构建了它,因此当滚动选项时图像会发生变化。我现在想添加一个提交按钮 (UIButton),以便在点击它时将它们发送到一个网址,以获取有关所选选项的更多信息。

这是我目前的代码。

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Load up NSArray
    _characters = [[NSArray alloc] initWithObjects:@"bergerac" , @"biarritz" , @"chareleroi" , @"liege" , @"oostende" , nil];


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


#pragma mark - UIPickerView Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    return 1;


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

    return _characters.count;


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

    return [_characters objectAtIndex:row];


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

    //Create UIImage
    UIImage *bergeracimage = [UIImage imageNamed:@"Bergerac.JPG"];
    UIImage *biarritizimage = [UIImage imageNamed:@"Biarritz-Anglet-Bayonne_Airport_view_from_landing_strip.jpg"];
    UIImage *chareleroiimage = [UIImage imageNamed:@"Aéroport_de_Charleroi,_B737_Ryanair_-_2011.JPG"];
    UIImage *liegeimage = [UIImage imageNamed:@"Liege_airport.jpg"];
    UIImage *oostendeimage = [UIImage imageNamed:@"Luchthavengebouw_Antwerpen-Deurne.jpg"];

    switch (row) 
        case 0:
            characterimage.image = bergeracimage;
            break;
        case 1:
            characterimage.image = biarritizimage;
            break;
        case 2:
            characterimage.image = chareleroiimage;
            break;
        case 3:
            characterimage.image = liegeimage;
            break;
        case 4:
            characterimage.image = oostendeimage;
            break;
        default:
            characterimage.image = oostendeimage;
            break;
    

我将如何实现UIButton

我一直在尝试将它合并到每个 case 分支中,但到目前为止还没有成功。

编辑:

好的,我在过去的一个小时左右一直在搞砸它,现在代码看起来像这样,因为我正在尝试更改 UIbutton 中的链接;


    //Create UIImage
    UIImage *bergeracimage = [UIImage imageNamed:@"Bergerac.JPG"];
    UIImage *biarritizimage = [UIImage imageNamed:@"Biarritz-Anglet-Bayonne_Airport_view_from_landing_strip.jpg"];
    UIImage *chareleroiimage = [UIImage imageNamed:@"Aéroport_de_Charleroi,_B737_Ryanair_-_2011.JPG"];
    UIImage *liegeimage = [UIImage imageNamed:@"Liege_airport.jpg"];
    UIImage *oostendeimage = [UIImage imageNamed:@"Luchthavengebouw_Antwerpen-Deurne.jpg"];

    //Create UIButton
    UIButton *bergeraclink = [NSURL URLWithString:@"https://www.google.com"];

    switch (row) 
        case 0:
            characterimage.image = bergeracimage;
            _characterpath = bergeraclink;
        case 1:
            characterimage.image = biarritizimage;
            break;
        case 2:
            characterimage.image = chareleroiimage;
            break;
        case 3:
            characterimage.image = liegeimage;
            break;
        case 4:
            characterimage.image = oostendeimage;
            break;
        default:
            characterimage.image = oostendeimage;
            break;
    

它仍然无法工作,但代码等中没有错误并且应用程序运行,我是不是走错了路?

【问题讨论】:

【参考方案1】:

听起来您需要一个属性(或 ivar)来保存要调用的正确 URL。然后,当您点击按钮时,您将可以访问该 URL。也许是这样的:

//a property to hold the URL
@property (strong, nonatomic) NSString *urlString;

...

//set the URL in your switch statement
switch (row) 
        case 0:
            characterimage.image = bergeracimage;
            self.urlString = @"http://yourserver.com/bergerac";
            break;
        ...

-(IBAction)buttonTapped 
    //call your web service with self.urlString 

【讨论】:

感谢您的回复,我已将 IBAction 与新属性一起添加到 .h 文件中,但它会将 IBAction 标记为“预期表达式”? @user3788098 - 您需要研究如何为按钮添加操作。 感谢您的帮助 我已将所有操作按钮都连接起来并且现在可以正常工作了 :) 很抱歉,您能解释一下如何调用 Web 服务吗?我尝试了几种方法,但似乎没有注册 urlstring....?

以上是关于UIButton 响应 UIPickerview的主要内容,如果未能解决你的问题,请参考以下文章

UIButton 响应 UIPickerview

UIButton 作为子视图没有响应

UIButton 没有响应点击

UIButton 子类不响应超级方法?

UIButton 没有响应

UIPickerview 中的 UIButton 没有响应