0

I was just wondering if there is a way to simplify or have multiple items in the one "self" statement. I currently have about 5 UITextfields and the code is just repeating its self but only targeting different textfields.

The code is below:

self.cell1Field1Dismiss.inputAccessoryView = numberToolbar;
self.cell1Field2Dismiss.inputAccessoryView = numberToolbar;
self.cell2Field1Dismiss.inputAccessoryView = numberToolbar;
self.cell2Field2Dismiss.inputAccessoryView = numberToolbar;
self.cell3Field1Dismiss.inputAccessoryView = numberToolbar;
self.cell3Field2Dismiss.inputAccessoryView = numberToolbar;
self.cell4Field1Dismiss.inputAccessoryView = numberToolbar;
self.cell4Field2Dismiss.inputAccessoryView = numberToolbar;
self.cell5Field1Dismiss.inputAccessoryView = numberToolbar;
self.cell5Field2Dismiss.inputAccessoryView = numberToolbar;

Is there a way to have something like: (this doesn't work for some reason)

self.cell1Field1Dismiss, cell1Field2Dismiss, etc... .inputAccessoryView = numberToolbar
Barmar
  • 741,623
  • 53
  • 500
  • 612
Ed3121577
  • 510
  • 1
  • 6
  • 13

4 Answers4

3

Use an IBOutletCollection to hold a reference to all of your textfields instead of individual IBOutlets. This works the same way as connecting IBOutlets.

Declare a property like so

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

Then iterate through all the textfields and set whatever properties you want.

for (UITextField *textField in self.textFields) {
    textField.inputAccessoryView = numberToolbar;
}
Kevin
  • 16,696
  • 7
  • 51
  • 68
2

According to @Kevin , here is my upgrade:

[self.textFields enumerateObjectsUsingBlock:^(UITextField *textField, NSUInteger idx, BOOL *stop) {
    textField.inputAccessoryView = numberToolbar;
}];

Block methods of collection class are recommended personally (Thanks to @Patrick Goley). There are a lot of issues talking about that: When to use enumerateObjectsUsingBlock vs. for http://www.mikeabdullah.net/slow-block-based-dictionary-enumeration.html Finally(I think it is right): http://lists.apple.com/archives/objc-language/2012/Sep/msg00012.html

Community
  • 1
  • 1
Danny Xu
  • 401
  • 3
  • 10
  • recommended why? Kevin's NSFastEnumeration is faster than any block implementation. Even if it weren't, speed isn't a concern here. There's not likely enough UITextViews for style of iteration to make any difference what so ever – Patrick Goley Dec 27 '13 at 04:09
  • @Patrick Goley, check this issue here:http://stackoverflow.com/questions/4486622/when-to-use-enumerateobjectsusingblock-vs-for. Althought, http://lists.apple.com/archives/objc-language/2012/Sep/msg00012.html and http://www.mikeabdullah.net/slow-block-based-dictionary-enumeration.html . So, personally I will use block-based. – Danny Xu Dec 27 '13 at 04:13
  • look at the actual benchmarks, they aren't even close really. http://www.objc.io/issue-7/collections.html (look for Enumeration and High Order Messaging). even if block-based is built on NSFastEnumeration, invoking blocks incurs overhead in itself. it's notable that concurrent options are not viable here since we are manipulating UIKit stuff – Patrick Goley Dec 27 '13 at 04:58
0

If you want all of the text fields in your view to have the same inputAccessoryView you can do the following:

for (UIView *view in[self.view subviews]) {
    if ([view isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)view;
        textField.inputAccessoryView = numberToolbar;
    }
}
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
0

Try the below code. Similar to assigning the values for integer/float/.. variables you can assign like below.

self.cell1Field1Dismiss.inputAccessoryView = self.cell1Field2Dismiss.inputAccessoryView = self.cell2Field1Dismiss.inputAccessoryView = .......... = numberToolbar

Hope this will help you.. If it not works let me know..

RAJA
  • 1,214
  • 10
  • 13
  • Thats not really simplifying it. Your still having to declare all of the textfields as an inputAccessoryView. – Ed3121577 Dec 27 '13 at 03:37