我有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;
}
答案 0 :(得分: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];
}
答案 1 :(得分:1)
如果branches
具有相同的格式,例如“number” - “name”。
您可以使用componentSeparatedByString:
NSString
方法
类似的东西:
NSArray *token = [textfield.text componentsSeparatedByString:@"-"];
if([token count]>0)
NSString *code = (NSString *)[token objectAtIndex:0];