Through some fantastic help from a very patient user I managed to get my reset button working with the following code:
- (IBAction)resetAction:(id)sender {
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.text = #"";
}
}
The issue I am having now is that this will not reset the textfields in a scroll view. Any help will be gratefully received! I am so close to completing this!!!!
Many Thanks
You could call the method recursivly:
- (void)resetTextfieldsInView:(UIView*)view {
for (UIView *v in [view subviews]) {
if ([v isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)v;
textField.text = #"";
}else if ([v isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)v;
textView.text = #"";
}else{
[self resetTextfieldsInView:v];
}
}
}
- (IBAction)resetAction:(id)sender {
[self resetTextfieldsInView:self.view];
}
If the textfields are embedded in a scroll view, then you need to iterate the subviews of the scroll view, i.e.
for (UIView *view in [self.scrollView subviews])
Related
NSArray *viewsToRemove = [scrollView subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
When update device to ios8 the code will crash [removeFromSuperview], and show message "Thread 1:EXC_ACCESS(code=1, address=0x5000000c")
Any another code can instead it?
NSArray *viewsToRemove = [scrollView subviews];
for (UIView *v in viewsToRemove) {
if ([v isKindOfClass:[UIView Class]]) {
[v removeFromSuperview];
}
}
I Tried using some sample code but i Can't ..Please sombody Help With the solution...
The code which i tryed is
for (UIView *subview in [controller.view subviews]) {
for (UIView *myview in [subview subviews]) {
for (UIView *theview in [myview subviews]) {
if ([NSStringFromClass([theview class]) isEqualToString:#"MFMailComposeView"]) {
for (UIView *aview in [theview subviews]) {
for (UIView *thisview in [aview subviews]) {
if ([NSStringFromClass([thisview class]) isEqualToString:#"MFComposeScrollView"]) {
for (UIView *bview in [thisview subviews]) {
if ([NSStringFromClass([bview class]) isEqualToString:#"MFComposeTextContentView"]) {
NSLog(#"%#", ((UITextView *)bview).text);
}
}
}
}
}
}
}
}
}
But Its not going inside the loop in this line...
if ([NSStringFromClass([theview class]) isEqualToString:#"MFMailComposeView"]) {
I searched in many sites...what i got is ,this code is broken in ios 6... BUT I am using ios 8.1....Please help me come up with the solution...
Thanks In Advance
Nivetha
I have an iPad app which already works on iOS 7. I used to reduce the text size in the action sheet buttons with this code:
- (void) willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
button.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17.0];
}
}
}
I'm looking for a way to do the same on iOS 8 with UIAlertController and UIAlertAction. Although the UIAlertController has a view with subviews, it doesn't seem to have any UIButton or UILabel in it.
You CAN do that on iOS8. Just subclass UIAlertController, check code below:
#interface AlertController : UIAlertController
#end
#implementation AlertController
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Search for labels and change font
[self changeLabelsFontInView:self.view];
}
- (void)changeLabelsFontInView:(UIView *)view {
if (view.subviews.count > 0) {
for (UIView *subview in view.subviews) {
[self changeLabelsFontInView:subview];
}
} else {
if ([view isKindOfClass:[UILabel class]]) {
[(UILabel *)view setFont:[UIFont boldSystemFontOfSize:35.0F]];
}
}
}
#end
I am new to ios. I have the following code to change the return key type of keyboard to join. It works fine in ios6 but not in ios7. The code inside the if-block is never executed on ios7.
Have anyone seen a similar issue? Is there any workaround for this?
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[searchBar resignFirstResponder];
[(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyJoin];
[searchBar becomeFirstResponder];
}
I googled and found that Join button isn't showing up because the internal structure of UISearchBar, which aren't supposed to modify, has changed. I dont konw how to fix this and where to found out what has changed, Anyone can explain ?
Also tried following(similar way) workaround code not lucky enough
for(UIView *searchBarSubview in [searchBar subviews]) {
if([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)searchBarSubview setReturnKeyType: UIReturnKeyJoin];
} else {
for(UIView *subSubView in [searchBarSubview subviews]) {
if([subSubView conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)subSubView setReturnKeyType: UIReturnKeyJoin];
}
}
}
Try this :
UITextField *searchBarTextField ;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ?
self.searchBar.subviews : [[self.searchBar.subviews
objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.returnKeyType = UIReturnKeyJoin;
try this for get textfield from searchbar
for (UIView *subView in self.searchBar.subviews){
for (UIView *searchView in subView.subviews){
if ([searchView isKindOfClass:[UITextField class]])
{
[(UITextField *)searchView setKeyboardAppearance:UIKeyboardAppearanceAlert];
[(UITextField *)searchView setEnablesReturnKeyAutomatically:NO];
break;
}
}
}
In iOS6 , I use the below code to change inputView of UISearchbar
for (UIView *view in _searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
UISearchbar in iOS7 had changed, I don't know how to find textField to change inputView.
Please help! Thanks!
The hierarchy of subview has been changed in iOS7, so you can use the following code:
// subviews
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField* search=(UITextField*)view;
[search setFont:[UIFont fontWithName:#"MyCustomFont" size:15]];
search.delegate = self;
[search setInputView:self.customKeyboard];
[self.customKeyboard setTextView:search];
}
}
[self.searchBar reloadInputViews];
Use following code :
NSArray *subViewsOfSearchBar = [[self.YOurSearchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i< subViewsOfSearchBar.count; i++) {
if([[subViewsOfSearchBar objectAtIndex:i] isKindOfClass:[UITextField class]])
{
UITextField *searchTxtField=(UITextField*)[subViewsOfSearchBar objectAtIndex:i];
[searchTxtField setInputView:self.customKeyboard];
}
}
[self.searchBar reloadInputViews];
Try this code :
for (UIView *view in _searchBar.inputView.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
in iOS 7 subview is not working. From iOS7 UI objects have a extra wrapper/container. May be this code will help you.