UIActivityIndicatorView not working when added to UITableView - iphone

I'm trying to add a UIActivityIndicatorView to the center of my UITableView while it waits for the UITableViews data to load. I am doing this as follows:
UIActivityIndicatorView *activityIndicatorTemp = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicatorTemp setCenter:[[self tableView] center]];
[self setActivityIndicator: activityIndicatorTemp];
[activityIndicatorTemp release];
[[self tableView] addSubview:activityIndicator];
[activityIndicator startAnimating];
However the activity indicator is not showing up at all when I launch the code. I'd like to do it programmatically, any suggestions on how to fix this or why it isn't working?

please move
[activityIndicatorTemp release];
after addSubview:
[[self tableView] addSubview:activityIndicator];

The problem here is when I was setting the location of the activityindicator. I did this before the tableview had been initialized.
[activityIndicatorTemp setCenter:[[self tableView] center]];
I removed the above and it worked.

Related

Hide a UITableView of a UIScrollview on a button click in iPhone

I have a UITableView with some data in -(void)ViewDidLoad. So it appears accordingly. But when I click a button then my tableview should be hidden and I should load a UIlabel with some text.
But for me on button click empty tableview with some rows is loading. I should avoid it. How can I do it?
Here is my code
-(void)ViewDidLoad {
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,380,320,200) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(void)DatePickerDoneClick:(id)sender {
[tableview setHidden:YES];
displayError =[[UILabel alloc]init];
[displaytError setFrame:CGRectMake(20,400,320,100)];
displayError.textAlignment=UITextAlignmentLeft;
displayError.backgroundColor=[UIColor clearColor];
displayError.text=[NSString stringWithFormat:#"Not found"];
[self.view addSubview:displayError];
}
Could not get where I'm going wrong?
To hide Table View you can use
myTableView.hidden = YES;
or
[mtTableView setHidden:YES];
Hope this will help you out.
if tableview.hidden = YES; is not doing the job
try the next code:
[self.tableview removeFromSuperView];

Why aren't UITabBarItems' titles displaying?

First time poster. Thanks in advance.
I want to use a UITabBar in my iPhone app. I have been led to believe that I cannot use UITabViewController because I would be nesting it inside a UINavigationController. I read that you can do this, but you have to use UITabBar instead of the full-fledged controller. I can make this work with Interface Builder, but I've got a lot of redundant XIBs and I'd rather do it in code anyway. I want to know how the magic works, as it were.
Here's the .m of my controller:
#import "DumbViewController.h"
#implementation DumbViewController
-(void) loadView {
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 30)];
[label setText:#"hello world"];
[view addSubview:label];
[label release];
UITabBar *tb = [[UITabBar alloc] initWithFrame:CGRectMake(0, [view frame].size.height - 64, [view frame].size.width, 64)];
[tb setDelegate:self];
[tb setItems:[NSArray arrayWithObject:[[[UITabBarItem alloc] initWithTitle:#"bob" image:[UIImage imageNamed:#"21-skull.png"] tag:0] autorelease]]];
[view addSubview:tb];
[tb release];
[self setView:view];
[view release];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"selected item %#", item);
}
#end
When I run the app, I get my label and a tab bar at the bottom. I get the goofy little skull icon, but "bob" never displays. Clicking the skull works as you would expect.
Why isn't that title showing?
Thanks!
Try the following code....
UITabBarItem *someTabBarItem = [[UITabBarItem alloc] initWithTitle:#"bob" image:[UIImage imageNamed:#"21-skull.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:someTabBarItem,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:someTabBarItem];
[tabBarItems release];
[someTabBarItem release];
The problem was that I was setting the frame of the UITabBar outside the frame of the parent view.
I added this line after the UITabBar alloc line.
[tb setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
The advantage here is that even if I rotate or if the app is running on an iPad, the tab bar stays at the bottom of the screen.
I also changed from a 64 pixel height to a 44 pixel height because it looks much more like how the XIB did originally.

How to show the activity indicator on tap gesture

I want to show the activity indicator on tap gesture and also navigate from current viewcontroller to next viewcontroller I have written code on tap gesture method my code is -
- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
textLabel.hidden = YES;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
[indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicator];
[indicator startAnimating];
AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
[self.navigationController pushViewController:accountAnfordernViewController animated:YES];
[accountAnfordernViewController release];
}
It navigates to next view controller but indicator are not showing, I want when it navigate to nextviewcontroller at that time indicator also should be show for some time and then navigate to next view controller. What should I do for that?
I suspect you need to do something like this:
- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
textLabel.hidden = YES;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
[indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicator];
[indicator startAnimating];
[self performSelector:#selector(pushAccountAnfordern) withObject:nil afterDelay: 0.1];
}
- (void) pushAccountAnfordern;
{
AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
[self.navigationController pushViewController:accountAnfordernViewController animated:YES];
[accountAnfordernViewController release];
}
You can vary the delay. And, indeed, 0.0 may be just fine. This way of doing it lets the current run loop end, giving a chance for the spinner to start showing before starting the next bit of code. For a different approach (with potential side effects) to forcing the spinner to start before the current run loop ends this this SO question.

Showing UIWebView over tableView in UITableViewController

I added a rightBarButton and I'd like to have that button hide the TableView and show my UIWebView, but I am not seeing the web view.
UITableViewController
viewDidLoad:
mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 380)];
[mWebView loadHTMLString:#"<html><body>Testing</body></html>" baseURL:nil];
mWebView.hidden=YES;
[self.view addSubview:mWebView];
onButton:
mWebView.hidden = NO;
self.tableView.hidden=YES;
The tableView disappears, but all I get is a white screen instead of the expected 'Testing'
You may need to add [mWebView setNeedsDisplay] and possibly [self.tableView setNeedsDisplay].
Try [webview setBackGroundColor: [UIColor clearColor]];
and [webview setOpaque: NO];

UIActivityIndicator not showing up

I have implemented a UIActivityIndicator that shows up in one part of my program but not another. I have the activity indicator come up while i am loading a table, however, i am trying to get it to start animating again after the user has clicked a button and is waiting for the table to reload. The table reloads, but no indicator. Here is the code.
- (void)viewWillAppear:(BOOL)animated {
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
activity = [[UIActivityIndicatorView alloc] initWithFrame: frame];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activity startAnimating];
[navigationUpdateFromDetail.window addSubview: activity];
[super viewWillAppear:animated];
}
It comes up for that part. However, for the next part it does not want to seem to come up.
- (IBAction) btnGreaterTen_clicked :(id)sender {
self.searchDistance = [NSNumber numberWithDouble : 10];
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
activity = [[UIActivityIndicatorView alloc] initWithFrame: frame];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[navigationUpdateFromDetail.window addSubview: activity];
[activity startAnimating];
NSLog(#" search value after change %#", [searchDistance description]);
[self getSetDisplay];
[activity stopAnimating];
}
That button changes a variable and is suppose to start the animation, but it does not. I have changed the color to make sure it was not just blending in, so that is not the solution. I tried to recreate the same object, but still no luck.
Thank you in advance.
That won't work, because the animation will only show when the main application loop is "running". In your code, you're blocking the main thread by calling [self getSetDisplay].
You should load your data asynchronously to make this work (in a background thread). Then you can call startAnimating, start your thread, and when the thread finishes, stop the animation.
This has been bugging me for ages, and I just found that when using a search display controller I had to add the activity indicator as a subview of the search results to get it to show. In my viewDidLoad, I put...
// create activity indicator
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.searchDisplayController.searchResultsTableView addSubview:activityIndicator];
When I want to start animating, i put...
[activityIndicator setCenter:CGPointMake(self.searchDisplayController.searchResultsTableView.frame.size.width/2.0f, self.searchDisplayController.searchResultsTableView.frame.size.height/2.0f)];
[activityIndicator startAnimating];
Hope this helps someone.
try adding
activity.hidden = NO;
just before
[activity startAnimating];