【问题标题】:Picker view selection their parameter can passed on post method选择器视图选择它们的参数可以传递给 post 方法
【发布时间】:2016-05-09 13:27:39
【问题描述】:

我是 IOS 新手,我想根据文本字段中的选择器视图选择显示它们的参数可以作为 Post 方法传递以获取响应并在警报视图中查看。

选取器视图委托:

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

{
    if(pickerView.tag == 2){
        return arrMsg.count;
    }else if(pickerView.tag == 1){
    return currencyname1.count;
    }
    else
    {
        return from_currency.count;
    }
}


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

{


    if(pickerView.tag == 2){
        return [arrMsg objectAtIndex:row];
    }else if(pickerView.tag == 1){
        return [currencyname1 objectAtIndex:row];
    }
    else
    {
        return [from_currency objectAtIndex:row];
    }
}


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

{
    if(pickerView.tag ==2){
    txtText.text = (NSString *)[arrMsg objectAtIndex:row];
        NSLog([arrmsg1 objectAtIndex:row]);
    }else if(pickerView.tag ==1){
    currency1.text = (NSString *)[currencyname1 objectAtIndex:row];
        NSLog([id1 objectAtIndex:row]);

    }
    else
    {
        currency2.text = (NSString *)[from_currency objectAtIndex:row];
        NSLog([id2 objectAtIndex:row]);
    }




}

视频加载:

创建数组:

 arrMsg = [json valueForKeyPath:@"Branches.branch_name"];
        //NSLog(@"%@",json);
        arrmsg1 =[json valueForKeyPath:@"Branches.id"];
        firststr = [arrmsg1 componentsJoinedByString:@","];
        currencyname1 = [json1 valueForKeyPath:@"Currencies.currency_name"];
        id1 = [json1 valueForKeyPath:@"Currencies.id"];

        from_currency = [json1 valueForKeyPath:@"Currencies.currency_name"];
        id2 = [json1 valueForKeyPath:@"Currencies.id"];
        secondstr = [id1 componentsJoinedByString:@","];
        thirdstr = [id2 componentsJoinedByString:@","];
        NSLog(@"%@",secondstr);
        NSLog(@"%@",thirdstr); 

    str = [NSString stringWithFormat:@"branch_id=%@&from_curr=%@&to_curr=%@&value=%@",firststr,secondstr,thirdstr,fourthstr]; 



pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectZero];

pktStatePicker  .delegate = self;

pktStatePicker  .dataSource = self;
txtText.delegate = self ;
currency1.delegate = self;
currency2.delegate = self;
[ pktStatePicker  setShowsSelectionIndicator:YES];

【问题讨论】:

  • 显示您的 arrMsg 一次

标签: ios objective-c nsurlconnection uipickerview


【解决方案1】:

NSNotificationCenter 对象(或简称为通知中心)提供了一种在程序中广播信息的机制。

您可以阅读有关NSNotificationCenter here 的更多信息。

其中一种可能性是通过通知中心发送对象。如果你想发送一个对象,你必须调用postNotification,比如:

[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_name" object:self userInfo:@{@"result":arrMsg}];

您必须将这段代码放在将返回/传递您的数组的方法中。

一旦你调用postNotification,你必须在类中设置一个observer,它将获取你使用postNotification发送的对象。

您可以在viewDidLoad 中设置observer,如下所示:

// NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(doSomething:)
                                             name:@"notification_name"
                                           object:nil];

当这个类中的observer 收到你的通知时,它会调用@selector 中的method。这个方法(在doSomething:上面的代码中)必须在你设置observer的类中定义。

-(void)doSomething:(NSNotification *) notification {

    // You can get the object you have sent - using the `notification` object
    NSMutableArray dataArray = [NSMutableArray arrayWithArray:notification.userInfo[@"result"]];
    // Do something else

}

希望对你有帮助。

【讨论】:

  • 当然!看看是否更清楚。有其他事情请告诉我。
猜你喜欢
  • 2011-04-22
  • 2012-08-31
  • 1970-01-01
  • 2015-04-04
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多