UIButton回复UIPickerview

时间:2014-07-11 14:39:25

标签: ios uibutton uipickerview

所以我有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分支中,但到目前为止还没有运气。

编辑:

好的,我已经在通过时间左右一直搞乱它现在代码看起来像我试图改变UI按钮内的链接;

{
    //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 个答案:

答案 0 :(得分:0)

听起来你需要一个属性(或ivar)来保存正确的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 
}