选择器视图选择其参数可以传递给post方法

时间:2016-05-09 13:27:39

标签: ios objective-c nsurlconnection uipickerview

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

Picker视图代表:

- (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]);
    }




}

viedidload:

创建的数组:

 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];

1 个答案:

答案 0 :(得分:0)

  

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

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

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

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

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

致电postNotification后,您必须在班级内设置observer,以获取您使用postNotification发送的对象。

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

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

当此课程中的observer收到您的通知时,它会调用method中的@selector。此方法(在上面的代码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

}

我希望这可以帮到你。