English 中文(简体)
• 连接所有广域网
原标题:Get array of all UITextFields

我怎么能够从控制人员那里获得一系列的UIT延伸区?

EDIT:我不想将案文领域硬化成一个阵列。 我实际上想从该代表的召集人那里获得所有领域的代表名单。

最佳回答

Recursive implementation to search all subviews subviews: (this way you catch textfields embedded in uiscrollview, etc)

-(NSArray*)findAllTextFieldsInView:(UIView*)view{
    NSMutableArray* textfieldarray = [[[NSMutableArray alloc] init] autorelease];
    for(id x in [view subviews]){
        if([x isKindOfClass:[UITextField class]])
            [textfieldarray addObject:x];

        if([x respondsToSelector:@selector(subviews)]){
            // if it has subviews, loop through those, too
            [textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]];
        }
    }
    return textfieldarray;
}

-(void)myMethod{
   NSArray* allTextFields = [self findAllTextFieldsInView:[self view]];
   // ...
 }
问题回答

如果你知道需要<条码>。 NSArray 包含所有UIText Field的代码,然后是为什么不添加到一个阵列中?

NSMutableArray *textFields = [[NSMutableArray alloc] init];

UITextField *textField = [[UITextField alloc] initWithFrame:myFrame];

[textFields addObject:textField]; // <- repeat for each UITextField

如果你使用刀子,则使用<代码>。 缩略语

@property (nonatomic, retain) IBOutletCollection(UITextField) NSArray *textFields;

然后将所有<代码>UIText Field连接到该阵列。

- 采用以下代码,以获取包含所有以观点表述的统法特领域的文本价值:

   NSMutableArray *addressArray=[[NSMutableArray alloc] init];

   for(id aSubView in [self.view subviews])
   {
           if([aSubView isKindOfClass:[UITextField class]])
           {
                  UITextField *textField=(UITextField*)aSubView;
                  [addressArray addObject:textField.text];
           }
   }
   NSLog(@"%@", addressArray);
extension UIView 
{
   class func getAllSubviewsOfType<T: UIView>(view: UIView) -> [T] 
   {
       return view.subviews.flatMap { subView -> [T] in
       var result = UIView.getAllSubviewsOfType(view: subView) as [T]
       if let view = subView as? T {
           result.append(view)
       }
       return result
     }
   }

   func getAllSubviewsWithType<T: UIView>() -> [T] {
       return UIView.getAllSubviewsOfType(view: self.view) as [T]
   }
}

如何使用文本领域:

let textFields = self.view.getAllSubviewsWithType() as [UITextField]

你们可以通过控制者的观点表。

参看<><><>>>>>>>。

NSMutableArray *arrOfTextFields = [NSMutableArray array];
for (id subView in self.view.subviews)
{
    if ([subView isKindOfClass:[UITextField class]])
       [arrOfTextFields addObject:subView]; 
}

http://www.ohchr.org。 无全球变量的回收(时间期限)

-(NSArray*)performUIElementSearchForClass:(Class)targetClass onView:(UIView*)root{

    NSMutableArray *searchCollection = [[[NSMutableArray alloc] init] autorelease];

    for(UIView *subview in root.subviews){

        if ([subView isKindOfClass:targetClass])  
            [searchCollection addObject:subview];

        if(subview.subviews.count > 0)
            [searchCollection addObjectsFromArray:[self performUIElementSearchForClass:targetClass onView:subview]];

    }

    return searchCollection;
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签