How to stop mutiple activity indicator animation in multiple webview? - iphone

Currently i am developing a sample where i am displaying 4 webview in ipad screen and trying to show 4 different URL in them.
All the web pages are showing in the webview perfectly.
Also i am displaying 4 activity indicator once the webpage starts loading.
But i can not figure out how to stop activity indicator in all the respective webpage one by one once they are loaded in to the screen.
-(void)createWebView
{
//******************** First *********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:#"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second *********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:#"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third ************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth ************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
[firstIndicator release];
[secondIndicator release];
[thirdIndicator release];
[fourthIndicator release];
}

overwrite below delegate method and check which webview is loading finished like below
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(webView== firstWebView)
{
[firstIndicator setHidden:YES];
}
else
{
so on...
}
}

Related

Issue in UIActivityIndicatorView in iPhone?

Currently i am working in iphone app, Using UIWebView (Webpage shown in presentModelViewController) to show webpage on the screen, then i add UIActivityIndicatorView to show in load url request, but the UIActivityIndicatorView didn't show in the screen, i tried my level best, please help me.
Thanks in Advance
I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
adding your activitiy indicatore View in to your webView like this:-
- (void)viewDidLoad
{
[super viewDidLoad];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[web addSubview:activity];
[self.view addSubview:web];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
Try This,
- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
[self.view bringSubviewToFront:activity];
}
Define UIActivityIndicatorView after defining UIWebView.
- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
}
The order you add the sub views to the super view is significant. The UIActivityIndicatorView gets added and then the UIWebView overlays it. If you add the UIWebView first, i.e
[self.view addSubview:web];
[self.view addSubview:activity];
the UIActivityIndicatorView will overlay the UIWebView.

Refresh view or remove subviews ios

I'm trying to make a scene where the user can swipe over the screen to browse between posts. The post can be both an image with text or just a note, and the view is altered depending on which one occurs.
The getting process works just perfect. It gets the right post whether i swipe right or left. The problem is that the old view wont disappear and the views are overlapping. This is especially bothering when you go from a note to a photo or vice versa since the sizes ar different...
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#" GETRESULT %# POSTNUMBER %d", getResult, postNumber);
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[[self view] addGestureRecognizer:recognizerRight];
UISwipeGestureRecognizer *recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizerLeft];
// Do any additional setup after loading the view.
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE RIGHT");
if(postNumber > 0){
postNumber--;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if (getImageString) {
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE LEFT");
if(postNumber < [getResult count] - 1){
postNumber++;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if(getImageString){
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
At first i only increased postNumber and called viewDidLoad in the swipe actions. That gave me the exact same result though... This is only one of countless trial and error attempts, and I'm sorry this is among the messier ones...
Would greatly appreciate it if anyone got a solution.
Thanks in advance,
Tom
Did you try this?
for (UIView * view in self.view.subviews) {
if ([view isEqual:postText]||[view isEqual:imgView]) {
[view removeFromSuperview];
}
}
I couldn't remove the variables from the superview because I did it in a new instance of viewDidLoad. I tried to do so before calling viewDidLoad this time and it worked!
Sorry for wasting your time...
Tom

Multiple loading of webpages in webview is slower

I am developing an sample iPad app where I am displaying some webapages in UIWebview , the code is working properly.But I think the webpages are taking too much time to load in to UIWebview.My code is as below:
-(void)createWebView
{
//******************** First Webview*********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:#"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second Webview*********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:#"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third Webview************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth Webview************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
}
So how can I make it more faster, please guide me.
For this you have to use NSOpertaionQueue or GCD queue is another options

Parse image issue in Iphone

I have a array which have a one content image URL for key "Image". I want to show that image in my table view.
How to do it .Please give me suggestion.
You can search in google using "lazy loading image for tableview".
It is also available with apple example.
img_view = [[UIImageView alloc]init];
img_view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
img_view.frame = CGRectMake(0,0,130, 75);
activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
[img_view addSubview: activityIndicator];
activityIndicator.center = CGPointMake(50,30);
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(loadImage:)
object:[[xmlParser data1] objectAtIndex:indexPath.row]];
[queue addOperation:operation];
[operation release];
[cell.contentView addSubview:img_view];
-(void)loadImage:(Book *)book
{
[activityIndicator startAnimating];
NSString *imgurl=[book image_url];
NSURL *url=[[NSURL alloc]initWithString:imgurl];
NSData *img=[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[UIImage imageWithData:img];
img_view.image=image;
[activityIndicator stopAnimating];
}

ios proper way to update programatic uilabels with viewdidappear

Every time this tab get loaded instead of updating the dynamic label, it writes a brand new label over it. What do I need to put in this code so that it updates it or clears the dynamic labels and then put in the new label. I feel like its probably just a simple one line fix. Here is a simplified version of the code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
goalArray = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"http://localhost/goal.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:test forKey:#"name"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
...
for (id myArrayElement in goalArray)
{
NSLog(#"y value:%i", yValue);
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, yValue, 80, 44)];
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.text = myArrayElement;
[self.view addSubview:label];
yValue += 44;
}
}
In requestFinished: before the for loop, add this:
for ( UIView *die in [self subviews]) { // clear out previous label
if ( die.tag == 123 ) {
[die removeFromSuperview];
}
}
and then inside your for loop, add this:
label.tag = 123; //doesn't have to be 123, as long as it's an int that matches the one above.