I'm having some trouble dynamically removing UITextFields from my scrollview when the user taps a button. The UITextields were created programatically. This is what I have thus far, any help would be appreciated.
-(IBAction)resetAll{
int textFieldTag;
for (int i=0; i<[array count]; i++) {
textFieldTag = i + 100;
UITextField *myTextField = (UITextField *)[self.view viewWithTag:textFieldTag];
[myTextField removeFromSuperview];
[myTextField release];
}
}
-(IBAction)resetAll
{
NSMutableArray *arrayTextFields=[yourScrollView subViews]; //get all subviews from your scrollview
for (int i=0; i<[arrayTextFields count]; i++)
{
if([[arrayTextFields objectAtIndex:i] isKindOfClass:[UITextField class]]) //check for UITextField
{
UITextField *textField=(UITextField *)[arrayTextFields objectAtIndex:i];
[textField removeFromSuperView]; //Remove textField
}
}
}
Implement this....
-(IBAction)resetAll
{
for (UITextField *myTextField in [myScrollView subviews])
[myTextField removeFromSuperview];
}
Try this:
-(IBAction)resetAll
{
for (UITextField *tf in myScrollView) {
[tf removeFromSuperview];
[tf release];
}
}
Related
I have a UITextField for the UISearchBar which this was working until iOS 7 upgrade and now it fails at this line: UITextField *textfield=(UITextField*)[[searchBar subviews] objectAtIndex:1];
any idea how to fix this? thanks
// search bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 190.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 230.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
searchBar.layer.borderColor=[UIColor whiteColor].CGColor;
UITextField *textfield=(UITextField*)[[searchBar subviews] objectAtIndex:1];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
try this , it's work in Both IOS6 and IOS7+ and safe approch
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"ArialMT" size:10]];
It's not a good idea to assume that second searchBar's subview will be UITextField.
I printed out subviews for UISearchBar, that's what I got on iOS 7:
<__NSArrayM 0x17d141f0>(
<UIView: 0x17d34f40; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x17d34fa0>>
)
Only one subview, so your ... objectAtIndex:1] will definitely crash.
You can use the following category for UIView to find UITextField in your searchBar:
#interface UIView(Utils)
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass;
#end
#implementation UIView(Utils)
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass
{
if( [self isKindOfClass:subviewClass] ) {
return self;
} else {
for( UIView* child in self.subviews ) {
UIView* result = [child findSubviewRecursivelyOfClass:subviewClass];
if( result ) {
return result;
}
}
return nil;
}
}
#end
Try this one for iOS7.
TESTED
for (id object in [searchBar subviews])
{
for (id subObject in [object subviews])
{
if ([subObject isKindOfClass:[UITextField class]])
{
UITextField *textfield=(UITextField*)subObject;
}
}
}
iOS6
for (id object in [searchBar subviews]) {
if ([object isKindOfClass:[UITextField class]]) {
UITextField *textfield=(UITextField*)object;
}
}
How about a recursive method that can trick to work in any version
UITextField *searchBarTextField = [self findTextFieldFromControl:self.placeSearchBar];
- (UITextField *) findTextFieldFromControl:(UIView *) view
{
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
return (UITextField *)subview;
}
else if ([subview.subviews count] > 0)
{
return [self findTextFieldFromControl:subview];
}
}
return nil;
}
Hi I have a method in which I have created 3 views on a click;
-(void)method{
for (i=0; i<3; i++) {
NSLog(#"i is: %d",i);
NSLog(#"i is: %d",i);
userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake (100,41,60,60)];
userResizableView.tag = i;
imageVw1 = [[UIImageView alloc]initWithFrame:userResizableView.bounds];
imageVw1.image = [UIImage imageNamed:#"redacted2.jpg"];
imageVw1.userInteractionEnabled=YES;
imageVw1.autoresizesSubviews = YES;
imageVw1.alpha = 0.93; // for opacity
userResizableView.contentView = imageVw1;
userResizableView.delegate = self;
[userResizableView showEditingHandles];
currentlyEditingView = userResizableView;
lastEditedView = userResizableView;
[self.view addSubview: userResizableView];
[userResizableView release];
}
}
Now in another method I want to hide these views what I created last.
but I am not able to do it. I am hiding my view is like-
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
userResizableView.tag = a;
userResizableView.hidden = YES;
a++;
}
But only one view is hide and the rest are remaining, no matters how amny times I clicked Hide_method, only one view is hide.
My question is that how to hide last view what I created last. Means views hide like 3,2,1,0. On every time when I clicked hide_method.
Any Idea or suggestions would be highly welcome.
Provide unique tag to not clash with any other subview of self.view with same tag like say any number 999 +.
Make changes accordingly:
for (i=0; i<3; i++) {
SPUserResizableView *userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake(100,41,60,60)];
userResizableView.tag = 999+i;
..........
..........
}
Now hide method would be:
-(void)Hide_method{
// find SPUserResizableView with unique tag
for (i=0; i<3; i++) {
if(i == 2){
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+i];
userResizableView.hidden = YES;
}
}
EDIT : To restore frame to original store while allocating view
CGRect prevFrame = userResizableView.frame //take frame while allocating userResizableView
Now restore whenever you wanted to original frame like this:
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+2]; //get last userResizableView
userResizableView.frame = prevFrame;
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
//userResizableView.tag = a;
SPUserResizableView *_view = (SPUserResizableView *)[self.view viewWithTag:a];
userResizableView.hidden = YES;
a++;
}
Try this:
-(void)Hide_method{
//UIView *v = [self.view viewWithTag:a];
SPUserResizableView *v = (SPUserResizableView *)[self.view viewWithTag:a];
if(v)
[v removeFromSuperview];
a++;
}
Method to remove view having tag=a
for (UIView *subview in [self.view subviews])
{
if (subview.tag == a)
{
[subview removeFromSuperview];
}
}
I am looking for 3 hours now on Google how to remove the tableview and show an image when the tableview is empty (have no more rows). Does someone know this? I know it's possible, because I saw it on many apps.
What I could find was:
// Check if table view has any cells
int sections = [self.tableView numberOfSections];
BOOL hasRows = NO;
for (int i = 0; i < sections; i++)
hasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO;
if (sections == 0 || hasRows == NO)
{
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add image view on top of table view
[self.tableView addSubview:imageView];
// Set the background view of the table view
self.tableView.backgroundView = imageView;
}
where to put this?
Thanks!
If your using Storyboard just put your view behind your UITableView
If your array of data is empty when creating it, simply hide your UITableView to show the "empty table" view behind it.
[tableView setHidden:YES];
Please refer to:
http://www.unknownerror.org/Problem/index/905493327/how-do-i-display-a-placeholder-image-when-my-uitableview-has-no-data-yet/
Thanks to Cocoanut and Thomas:
#interface MyTableViewController : UITableViewController
{
BOOL hasAppeared;
BOOL scrollWasEnabled;
UIView *emptyOverlay;
}
- (void) reloadData;
- (void) checkEmpty;
#end
#implementation MyTableViewController
- (void)viewWillAppear:(BOOL)animated
{
[self reloadData];
[super viewWillAppear: animated];
}
- (void)viewDidAppear:(BOOL)animated
{
hasAppeared = YES;
[super viewDidAppear: animated];
[self checkEmpty];
}
- (void)viewDidUnload
{
if (emptyOverlay)
{
self.tableView.scrollEnabled = scrollWasEnabled;
[emptyOverlay removeFromSuperview];
emptyOverlay = nil;
}
}
- (UIView *)makeEmptyOverlayView
{
UIView *emptyView = [[[NSBundle mainBundle] loadNibNamed:#"myEmptyView" owner:self options:nil] objectAtIndex:0];
return emptyView;
}
- (void) reloadData
{
[self.tableView reloadData];
if (hasAppeared &&
[self respondsToSelector: #selector(makeEmptyOverlayView)])
[self checkEmpty];
}
- (void) checkEmpty
{
BOOL isEmpty = YES;
id<UITableViewDataSource> src = self.tableView.dataSource;
NSInteger sections(1);
if ([src respondsToSelector: #selector(numberOfSectionsInTableView:)])
sections = [src numberOfSectionsInTableView: self.tableView];
for (int i = 0; i < sections; ++i)
{
NSInteger rows = [src tableView: self.tableView numberOfRowsInSection: i];
if (rows)
isEmpty = NO;
}
if (!isEmpty != !emptyOverlay)
{
if (isEmpty)
{
scrollWasEnabled = self.tableView.scrollEnabled;
self.tableView.scrollEnabled = NO;
emptyOverlay = [self makeEmptyOverlayView];
[self.tableView addSubview: emptyOverlay];
}
else
{
self.tableView.scrollEnabled = scrollWasEnabled;
[emptyOverlay removeFromSuperview];
emptyOverlay = nil;
}
}
else if (isEmpty)
{
// Make sure it is still above all siblings.
[emptyOverlay removeFromSuperview];
[self.tableView addSubview: emptyOverlay];
}
}
#end
UITableView is a (non direct) subclass of UIView, so what you want to do is easy.
Say that you have this table view as subview of your view controller's view. This case you just create another view with the same frame as the table view, then you remove your table view from the superview, and add the newly created view as subview of your view controller's view. So simply:
UIImageView* view= [[UIImageView alloc] initWithImage: yourImage];
view.frame= tableView.frame;
[tableView removeFromSuperview];
[self.view addSubview: view];
Put it on - (void)viewDidAppear. Good luck ;)
I want to change UITextField inside UISearchBar by subclass of UISearchBar. But when edit in UISearchDisplayController's UISearchBar, the field doesn't change the size which i set in :layoutSubView method.
How should I do that?
UISearchBar *searchBar = yourSearchController.searchBar;
UITextField *searchField;
for(int i = 0; i < [searchBar.subviews count]; i++) {
if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if(!(searchField == nil))
searchField.frame = CGRectMake(4, 5, 290, 30);
Good Luck.
In my application I have to add a search bar at the head of the tableview. I am able to add the searchbar but problem is without adding default search bar of ios can i add my customize search bar?? I am giving an image to see what types of search bar will be there...
you can subclass the UISearchBar and override the layoutSubviews method :
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:#"yourImage.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
Also you can :
//to clear searchbar backgraound
- (void) clearSearchBarBg
{
for (UIView *subview in theSearchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
}
//display showSearchButtonInitially in a keyboard
- (void)showSearchButtonInitially
{
UIView * subview;
NSArray * subviews = [theSearchBar subviews];
for(subview in subviews)
{
if( [subview isKindOfClass:[UITextField class]] )
{
NSLog(#"setEnablesReturnKeyAutomatically");
[((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
((UITextField*)subview).delegate=self;
[((UITextField*)subview) setEnabled:TRUE];
((UITextField*)subview).borderStyle = UITextBorderStyleNone;
break;
}
}
}
Look for Apple DOC for UISearchBar
You have bunch of methods there to get whatever you want
You can get UITextView Inside the search bar by
UITextField *textField = [searchBar.subviews objectAtIndex:2];
if ([textField isKindOfClass:[UITextField class]]) {
//Do your customization
}
Again look for AppleDoc for UITextField. You have bunch of methods for that also.
Yeah definitely. You can make your custom search bar (which is a sub-class of UIView) and add it as subview to the tableHeaderView.
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];
here searchBarDesign is my searchBar name.
I think it's better just set all properties of UISearchBar when it is loaded.
#interface MySearchBar : UISearchBar
#end
#implementation MySearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self myInitialize];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self myInitialize];
}
-(void)myInitialize
{
self.backgroundImage = [UIImage imageNamed:#"image.png"];
for (UIView* subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
//customize text field
UITextField* textfield = (UITextField*) subview;
}
}
}
#end