I have a scrollView who contain UIWebViews & UIImageViews.
I need to update this views, but I can't access to their properties.
for (UIView *subview in _scrollView.subviews) {
if([subView isKindOfClass:[UIImageView class]]){
subview.image = myimage;
}
if([subView isKindOfClass:[UIWebView class]]){
[subview loadData:mydata MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
}
}
My iterator is a UIView, so I can't access to .image UIImageView properties...
Thanks
Just use a cast expression:
((UIImageView*)subview).image = myimage;
(or:)
UIImageView *imageView = (UIImageView*)subview;
imageView.image = myimage;
for (UIView *subview in _scrollView.subviews) {
if([subView isKindOfClass:[UIImageView class]]){
UIImageView *img = (UIImageView*)subView;
img.image = myimage;
}
if([subView isKindOfClass:[UIWebView class]]){
UIWebView *webView = (UUIWebView*)subView;
[webView loadData:mydata MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
}
}
for (id subview in _scrollView.subviews) {
if([subView isKindOfClass:[UIImageView class]]){
subview.image = myimage;
}
if([subView isKindOfClass:[UIWebView class]]){
[subview loadData:mydata MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
}
}
Related
I have a single UIButton in the view of my UIViewController. I also have ten more that are in a subview in the main view. I want to find all these buttons. So far I have:
-(void)findAllButtons{
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[myButton class]]){
NSLog(#"found a button!");
}
}
}
It is only finding the single button though and not the other ten. Why is that? Shouldn't it iterate every single subview and then find them?
for (UIView *subView in scroll.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton*)subView;
if (btn.tag == selectedButton.tag) {
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
}else{
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = [UIColor clearColor].CGColor;
}
}
}
Just a few lines of code
-(void)findAllButtons {
[self findButtonsInSubviews:self.view.subviews];
}
- (void)findButtonsInSubviews:(NSArray *)subviews {
for(UIView *view in subviews) {
if ([view isKindOfClass:[UIButton class]]){
NSLog(#"found a button!");
} else {
[self findButtonsInSubviews:view.subviews];
}
}
}
A recursive function using Objective-C blocks like this will find all views of a given subclass type as specified in the test block in the view hierarchy of the given view:
NSMutableArray *marrAllButtons = [NSMutableArray new];
BOOL (^viewTest)(UIView*) = ^BOOL(UIView* viewToTest) {
return [view isKindOfClass:[UIButton class]];
};
void(^viewEnumerator)(UIView*) = ^(UIView* outerView){
for (UIView *view in outerView.subviews)
{
if (viewTest(view))
{
[marrAllButtons addObject:view];
}
else
{
viewEnumerator(view);
}
}
};
viewEnumerator(self.view);
NSLog(#"All Buttons %#", marrAllButtons);
- (NSMutableArray *)buttonsInView:(UIView *)view
{
NSArray *subviews = view.subviews;
NSMutableArray *buttons = [NSMutableArray array];
for (UIView *subview in subviews)
{
if ([subview isKindOfClass:[UILabel class]])
{
[buttons addObject:subview];
}
else if(subview.subviews)
{
[buttons addObjectsFromArray:[self buttonsInView:subview]];
}
}
return buttons;
}
Your method is right if all your button already have in self.view (main view).
Just set tag for all button to check and also make sure that all button are on the main view. I hope this will work.
-(void)findAllButtons{
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[myButton class]]){
UIButton *button = (UIButton*)view;
NSLog(#"found a button with tag:%d",button.tag);
}
}
}
for(UIView * subView in view.subviews) // here write Name of you ScrollView.
{
// NSLog(#"test %#", [subView class]);
if([subView isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton*)subView;
[button setSelected:NO] ;
NSString *s1;
s1 = #",";
s1 = [s1 stringByAppendingString:[NSString stringWithFormat:#"%#",button.titleLabel.text ]];
s1 = [s1 stringByAppendingString:[NSString stringWithFormat:#"%#",#"," ]];
NSRange range = [temp_Colors_Name_comma rangeOfString:s1 ];
if(range.location == NSNotFound)
{
}
else
{
[button setSelected:YES];
}
}
}
I am customizing my search bar with this but it is not showing any image. It remove its default image as well.
UITextField *searchField = nil;
for (UIView *subview in [[workLocationSearchBar.subviews lastObject] subviews]) {
NSLog(#"hey");
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
NSLog(#"inside");
break;
}
}
if (searchField) {
UIView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"circle.png"]];
if ([searchIcon isKindOfClass:[UIImageView class]]) {
}
NSLog(#"aye");
searchField.rightView = searchIcon;
searchField.leftView=searchIcon;
}
Try This:
[workLocationSearchBar setImage:[UIImage imageNamed:#"IMAGE_NAME"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
I have an issue with my ImageView animation. In my view there are more than 10 views and each view have are identified from its tag value & I have UIImageView and UIButton. when a button is tapped then that particular image of the view have to be animated. If any other images are animated it have to be stopped. This is my code:
-(void)makeAnimation:(UIButton *)sender {
UIView *tagView=(UIView *)[self.view viewWithTag:sender.tag];
UIView *next=nil;
UIView *previous=nil;
NSLog(#"%d",sender.tag);
for (UIImageView * imageview in [tagView subviews]) {
if ([imageview isKindOfClass:[UIImageView class]]) {
if ([imageview isAnimating]) {
NSLog(#"Animation Happens");
}
else{
imageview.animationDuration=2.0;
imageview.animationImages=[animationArray objectAtIndex:sender.tag-1];
imageview.animationRepeatCount=2;
imageview.tag=sender.tag;
[imageview startAnimating];
}
}
}
next=(UIView*)[self.view viewWithTag:sender.tag+1];
previous=(UIView*)[self.view viewWithTag:sender.tag-1];
NSLog(#"NOT IDEA");
[self previousview:previous nextview:next];
}
-(void)previousview:(UIView *)previous nextview:(UIView*)next
{
for (UIImageView * imageview in [previous subviews]) {
if ([imageview isKindOfClass:[UIImageView class]]) {
[imageview stopAnimating];
NSLog(#"PRREVIOUS");
}
}
for (UIImageView * imageview in [next subviews]) {
if ([imageview isKindOfClass:[UIImageView class]]) {
[imageview stopAnimating];
NSLog(#"NEXT");
}
}
}
Now my issue is when I select more than 4 buttons one after another my app crashed with memory warning.
Find the exact location of leak using profile while running and use #autorelease{} to handle the memory manually
Like this..
-(void)makeAnimation:(UIButton *)sender {
#autorelease{
UIView *tagView=(UIView *)[self.view viewWithTag:sender.tag];
UIView *next=nil;
UIView *previous=nil;
NSLog(#"%d",sender.tag);
for (UIImageView * imageview in [tagView subviews]) {
if ([imageview isKindOfClass:[UIImageView class]]) {
if ([imageview isAnimating]) {
NSLog(#"Animation Happens");
}
else{
imageview.animationDuration=2.0;
imageview.animationImages=[animationArray objectAtIndex:sender.tag-1];
imageview.animationRepeatCount=2;
imageview.tag=sender.tag;
[imageview startAnimating];
}
}
}
next=(UIView*)[self.view viewWithTag:sender.tag+1];
previous=(UIView*)[self.view viewWithTag:sender.tag-1];
NSLog(#"NOT IDEA");
[self previousview:previous nextview:next];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:#"Sample.png"];
[self.view addSubview:imageView];
NSArray *subviews = [self.view subviews];
for(id element in subviews) {
if ([[element class] isKindOfClass:[UIImageView class]]) //check if the object is a UIImageView
{
NSLog(#"element is a UIImageView\n");
[element setCenter:CGPointMake(500., 500.)];
} else {
NSLog(#"element is NOT a UIImageView\n");
}
}
}
I expected the output to be "element is a UIImageView, but it's actually element is NOT a UIImageView. Why? It's not that there are other subviews. There is only one. Furthermore, when run, the image is displayed at 100,100, not 500,500 as expected.
Your check is wrong. You should call isKindOfClass: on object and not on class of object.
[element isKindOfClass:[UIImageView class]]
Try the Following code.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:#"Sample.png"];
[self.view addSubview:imageView];
NSArray *subviews = [self.view subviews];
for(UIView *view in subviews) {
if ([view isKindOfClass:[UIImageView class]]) //check if the object is a UIImageView
{
NSLog(#"element is a UIImageView\n");
[element setCenter:CGPointMake(500., 500.)];
} else {
NSLog(#"element is NOT a UIImageView\n");
}
}
}
You can also Check sub views by tag value:
initially set imageView.tag=101; //anything you want
for(UIView *subview in [view subviews]) {
if(subview.tag== 101)/*your subview tag value here*/
{
NSLog(#"element is a UIImageView\n");
} else {
NSLog(#"element is NOT a UIImageView\n");
}
}
I have this code:
for(Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
return;
}
}
in this code I add an imageView on self.view but in my app i call this "for" many times and finally I have my self.view with 4 or 5 imageView "fix".
What's the way to remove all these imageView from my self.view?
If you only want to remove instances of UIImageView, you can try something like this:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
Update:
As vikingosegundo wrote in the comments, you can do this instad.
If you add each imageview to an array, you can remove them from the view later on like this:
NSMutableArray *images = [[NSMutableArray alloc] init];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix]; // Add the image to the array.
return;
}
}
The later on, remove them from the view:
for (UIImageView *v in images) {
[v removeFromSuperview];
}
Just call removeFromSuperview for every subView. Something like:
for(UIView *subview in self.view.subviews)
[subview removeFromSuperview];
NSMutableArray *images = [NSMutableArray array];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix];
}
}
for (UIView *v in images){
[v removeFromSuperview];
}
Another approach
for(UIView *v in self.view.subviews)
if([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
I put an example together.