1

我有 1 个文本字段和一个静态数组。我有数组中的项目,例如;12345-分支名称。但我只想从文本字段发送 12345 到肥皂电话。我能怎么做?

。H

 -(IBAction)Send:(UIButton *)sender;
 @property (strong, nonatomic) NSArray *branches;
 @property (unsafe_unretained,nonatomic) IBOutlet UITextField *BranchextField;

.m

-(IBAction)Send:(UIButton *)sender{

NSString *mensagemSOAP= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         "<soap:Body>\n"
                         "<UrunToplamiGetir xmlns=\"http://tempuri.org/\">\n"

                         "<BranchCode>%@</BranchCode>\n"
                         "</UrunToplamiGetir>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"_BranchextField.text];
          }

我的阿拉斯在这里

- (NSArray *)allbranches
{
    if(!self.branches){
        NSArray *branchNames = [self allCountries];
        NSMutableArray *mutablebranch = [NSMutableArray new];
        for(NSString *branchName in branchNames){
            DEMOCustomAutoCompleteObject *branch = [[DEMOCustomAutoCompleteObject alloc]
                                                    initWithBranch:branchName];
            [mutableBranches addObject:branch];
        }

        [self setBranchObjects:[NSArray arrayWithArray:mutableBranch]];
    }

    return self.branches;
}

- (NSArray *)allBranches
{
    NSArray *branches =
    @[
      @"12345-NAME OF BRANCH",
      @"67853-BRANCH 2",
      @"43223-BRANHC 3",
      @"66532-BRANCH 4",
      @"76733-BRANCH 99",

      ];

    return countries;
}
4

2 回答 2

1

尝试使用这个

-(IBAction)Send:(UIButton *)sender
{
        NSString *numberString;

        NSScanner *scanner = [NSScanner scannerWithString:_BranchextField.text];
        NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

        [scanner scanUpToCharactersFromSet:numbers intoString:NULL];

        // Collect numbers.
        [scanner scanCharactersFromSet:numbers intoString:&numberString];

        NSLog(@"Number String %@",numberString);



              NSString *mensagemSOAP= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         "<soap:Body>\n"
                         "<UrunToplamiGetir xmlns=\"http://tempuri.org/\">\n"

                         "<BranchCode>%@</BranchCode>\n"
                         "</UrunToplamiGetir>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n",numberString];

}

于 2013-05-17T07:57:54.577 回答
1

如果branches具有相同的格式,如“数字”-“名称”。

您可以使用componentSeparatedByString: 以下方法NSString

像这样的东西:

NSArray *token = [textfield.text componentsSeparatedByString:@"-"];

if([token count]>0)
NSString *code = (NSString *)[token objectAtIndex:0];
于 2013-05-17T08:02:46.747 回答