Refresh view after UIBarButtonItem is clicked - iphone

I have a refresh button on my navigationbar
buttonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonItemClicked)];
self.navigationItem.rightBarButtonItem = buttonItem;
-(void)buttonItemClicked{
NSLog(#"buttonItemclicked");
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
[spinner stopAnimating];
[spinnerView setHidden:YES];
}
If I go in and out of the view, it works fine. But when I call the same methods in buttonItemClicked, it doesn´t work. I also tried calling the view methods inside my action method, but that doesn´t work either.
What I´m trying to do is set my labels to nil, add my UIActivityIndicatorView and remove it after the labels are set again.
I have already tried [self.view setNeedsDisplay];
The refresh it self works, but the animations doesn´t work.
Any suggestions?

The animation is not working because you call startAnimating and stopAnimating (and setHidden) in the same method.
The render start at the end of the method call.
You need to set
[spinner stopAnimating];
[spinnerView setHidden:YES];
in requestAPI.
Edit:
Using Grand Central Dispatch. Like:
- (void)buttonItemClicked {
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
}
- (void)requestAPI {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:#"http://example.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *stringResult = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[spinnerView setHidden:YES];
myView.labelName.text = stringResult;
});
});
}

Try [myView setsNeedToDisplay];.

Related

After removeFromSuperview completes, a white screen is shown in place

So for my application, I add a view (aView) on top of my current view (classesWebView) as a Subview. All the aView is, is a UIView with a UIActivityIndicatorView on top of it that is supposed to animate while the view underneath (classesWebView) loads the appropriate Web Page.
I can see that the classesWebView webpage does appear (aView has an alpha of .5), but as soon as it finishes loading all the way, aView is sent [aView removeFromSuperview] and it disappears but after it goes away, all that's left is a white screen in it's place.
I have done this for two other methods and I don't know why, on only this method, it refuses to cooperate.
Any suggestions would be appreciated. The App is for iOS 6.
viewDidLoad method:
-(void)viewDidLoad
{
[super viewDidLoad];
classesWebView.delegate = self;
[classesWebView addSubview:aView];
NSURL *class = [NSURL URLWithString:#"mywebistelink"];
NSURLRequest *classRequest = [NSURLRequest requestWithURL:class];
[classesWebView loadRequest:classRequest];
}
webViewDidStartLoad method:
preView and switchView are Activity Indicators.
- (void)webViewDidStartLoad:(UIWebView *) webview
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if(aView.superview != nil)
{
[preView startAnimating];
}
else
{
[switchView startAnimating];
}
}
webViewDidFinishLoad method:
- (void)webViewDidFinishLoad:(UIWebView *) webview
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if(aView.superview != nil)
{
[aView removeFromSuperview];
[preView stopAnimating];
[preView setOpaque:false];
}
else
{
[switchView stopAnimating];
[switchView setOpaque:false];
}
}
[Further clarity: The reason I have the if-statement, is because I want another indicator for the classesWebView when loading pages but I do NOT want it to appear unless aView is gone (since aView already has it's on indicator: preView)]
EDIT: Just to prove that it is ONLY THE removeFromSuperview that is causing the problem, if I call [aView setAlpha:0.0] it disappears and the webPage below it loads properly. But the second that I call [aView removeFromSuperview] the web page turns into a white screen. T_T
try this:
NSURL *class=[NSURL URLWithString:[yourwebistelink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request=[NSURLRequest requestWithURL:class];
[self.classesWebView loadRequest:request];
try this one.. and try to dont add aView for activityIndicator., try this this one for indicating activityIndicator
UIWebView * classesWebView = [[UIWebView alloc] init];
[classesWebView addSubview:activityIndicator];
[self.activityIndicator startAnimating]; //for activityIndicator
NSURLRequest *request = [NSURLRequest requestWithURL:weburl];
classesWebView.frame = CGRectMake(0, 200, 768, 400);
[classesWebView setScalesPageToFit:NO];
[classesWebView loadRequest:request];
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
[self.activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error : %#",error);
}
if still webView is displaying blank white, then you have to encode your url by this one
NSString *encodedString=[graphStringUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *weburl = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:weburl];
then load this request in webView
[classesWebView loadRequest:request];
I don't think you should be adding subviews to a UIWebView. Try adding aView to the view that contains your classesWebView and see if that fixes the problem.
So I never got it to work but I cheated it by just setting [aView setAlpha:0]. That worked for what I needed. Thanks for the suggestions though, guys.

Show and Hide ActivityIndicator using SDWebImageDownloader

I am loading image using SDWebImageDownloader classes on every button click in my view.
__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = mainImage.center;
activityIndicator.hidesWhenStopped = YES;
[activityIndicator startAnimating];
[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholderLarge.png"] options:0 andResize:CGSizeMake(mainImage1.frame.size.width,imgview.frame.size.height) withContentMode:UIViewContentModeScaleAspectFit];
[imgView addSubview:activityIndicator];
I am not getting where to remove activityIndicator
You can use the following block.. Its more effective and it works for me...
[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"Default.png"] success:^(UIImage *image, BOOL cached){
[activityIndicator stopAnimating];
} failure:^(NSError *error){
//do any additional tasks if its failure
}];

UIRefreshControl hangs indefinitely

I have a UIRefreshControl, and it works fine when you use it, but it hangs, it never goes away. I've tried
[self.refreshControl endRefreshing];
This solution was from a similar question, but it has not addressed mine.
Even with this, the refreshControl continues spinning, not dismissing. Why is this?
In my viewDidLoad:
[NSThread detachNewThreadSelector:#selector(refreshFeed) toTarget:self withObject:nil];
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self
action:#selector(refreshFeed)
forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
refreshFeed method...
-(void)refreshFeed
//load those feeds
{
RSSLoader* rss = [[RSSLoader alloc] init];
[rss fetchRssWithURL:feedURL
complete:^(NSString *title, NSArray *results) {
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader",NULL);
dispatch_async(downloadQueue, ^{
_objects = results;
//completed fetching the RSS
dispatch_async(dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
[self.tableView reloadData];
});
});
}];
}
Can you try changing this line
[self.tableView addSubview:refreshControl];
with
[self setRefreshControl:refreshControl];

Refresh view after calling webservice [duplicate]

I have a refresh button on my navigationbar
buttonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonItemClicked)];
self.navigationItem.rightBarButtonItem = buttonItem;
-(void)buttonItemClicked{
NSLog(#"buttonItemclicked");
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
[spinner stopAnimating];
[spinnerView setHidden:YES];
}
If I go in and out of the view, it works fine. But when I call the same methods in buttonItemClicked, it doesn´t work. I also tried calling the view methods inside my action method, but that doesn´t work either.
What I´m trying to do is set my labels to nil, add my UIActivityIndicatorView and remove it after the labels are set again.
I have already tried [self.view setNeedsDisplay];
The refresh it self works, but the animations doesn´t work.
Any suggestions?
The animation is not working because you call startAnimating and stopAnimating (and setHidden) in the same method.
The render start at the end of the method call.
You need to set
[spinner stopAnimating];
[spinnerView setHidden:YES];
in requestAPI.
Edit:
Using Grand Central Dispatch. Like:
- (void)buttonItemClicked {
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
}
- (void)requestAPI {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:#"http://example.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *stringResult = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[spinnerView setHidden:YES];
myView.labelName.text = stringResult;
});
});
}
Try [myView setsNeedToDisplay];.

activity indicator is not display

-(IBAction)actionPrevious:(id)sender{
[self startact];
pageNumber = pageNumber - 1;
if (pageNumber>0) {
NSString *str_Img =[array_Image objectAtIndex:pageNumber];
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str_Img]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[imageView1 setImage:myimage];
[self.view addSubview:imageView1];
lbl_PhotoName.text = [array_Name objectAtIndex:pageNumber];
lbl_PhotoDate.text = [array_Date objectAtIndex:pageNumber];
lbl_PhotoDesc.text = [array_Desc objectAtIndex:pageNumber];
[mydata release];
[myimage release];
}
[self endact];
}
-(void)startact{
[act setHidden:NO];
[act startAnimating];
}
-(void)endact{
[act stopAnimating];
[act setHidden:YES];
}
In above code activity activity indicator is not display. Photo are display using the web service. please Help!
Thank You
You need to work on the same thread and need to call by this way
[self performSelector:#selector(startact) withObject:nil afterDelay:1];
You need to use threading in these kinds of scenarios.
Because activity indicator is on same thread as of the images work; thats why it is creating problem.
This is a silly mistake that I always seem to make: If you added the activity indicator programmatically did you make sure to addSubview: ? Or maybe it's hidden by something? Everything else looks fine, and you definitely don't need to startAnimating in a separate thread.