reloadData after Login on iPhone app - iphone

I have a Login that is initiated on the viewDidLoad of the mainApp. This means numberOfRowsInSection in the mainApp runs before the Login and results my tableview not being populated.
I have my JsonArray in the viewWillAppear in the mainApp the reloadData function to make numberOfRowsInSection run after the Login as well. So I put:
[self.tableView reloadData];
...in the viewWillAppear. But this doesn't make it run after Login.
Any suggestions?
Thanks in advance.
CODE UPDATE
Login Page
if (serverOutput != nil) {
//UIAlertView *alertSuccess = [[UIAlertView alloc] initWithTitle:#"Congrats" message:#"You are authorised" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
//[alertSuccess show];
//[alertSuccess release];
LucentaAppDelegate *appDelegate = (LucentaAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.userProducts = serverOutput;
loginButton.enabled = FALSE;
[self dismissModalViewControllerAnimated:YES];
Main View
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
LucentaAppDelegate *appDelegate = (LucentaAppDelegate *)[[UIApplication sharedApplication] delegate];
self.jsonArray = [appDelegate.userProducts JSONValue];
//[self.tableView reloadData];
[self performSelector:(#selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Test Alert" message:#"View Will Appear." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil];
[alert show];
[alert release]; }
-(void)refreshDisplay:(UITableView *)tableView {
[self.tableView reloadData];}

When your login (started in viewWillLoad) is completed, you should get some delegate callback call that tells you that your request as completed. You usually specify the delegate when creating the request.
If you are using NSURLRequest and NSURLConnection, the delegate is specified in one of:
+ connectionWithRequest:delegate:
– initWithRequest:delegate:
– initWithRequest:delegate:startImmediately:
depending on how your create the request, and the delegate callback to override is connectionDidFinishLoading: (or connection:didFailWithError: in case of communication errors).
connectionDidFinishLoading: is the right place where you should issue your [self.tableView reloadData];, after having updated your table data source.
If you are not using NSURLRequest, then the delegate callback will be different.
EDIT:
if your mainView receive the JSon data it needs, what you should do there is update your table data source and call reloadData on the table.
Indeed, calling reloadData will force calling numberOfRowsInSection again, but this information is provided by your data source, so if it is not updated to reflect the new data you have received, it will always reflect the old value.
Could you provide more information about your data source?

That depends on the actual code of your app. I suggest just to put a method like
- (void)loginSucceeded {
[self.tableView reloadData];
}
to your view controller and call it after the login was successful. It is also convenient as you might need to do some more complicated actions in this situation when your app grows.

Related

how to display tweet after it was sent?

I'm working on a twitter app and i want to immediately display tweet after sending. I tried to use reloadData, but it didn't work. Can anyone help me? Thank you!
Here's my code:
- (IBAction)postNewTweet:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#""];
[self presentViewController:tweetSheet animated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tweetTableView reloadData];
});
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Blaze, you are reloading the table view right after presenting the compose sheet, so it reload the table right there - and you are passing the completion block as nil, so nothing will happen when user finishes tweeting.
Also, you need to be sure that you are reloading not only the table, but the array or any other object that is the source of tweetTableView.
For example:
When calling the tweet compose sheet:
[self presentViewController:tweetSheet animated:YES completion:^(SLComposeViewControllerResult result)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadTweets];
});
}];
And then, you reload the tweets source and only then the table
-(void)reloadTweets
{
tweetsArray = .... //load the tweets from the internet, and only then reload the table
[self.tweetTableView reloadData];
}

load a different nib if phone is not connected to internet

I use Apple's Reachability class and it's working fine using an alert to tell the user that the connection is not available or the connection is lost. However, I want to change the alert to something more visual. I want to load a nib that tells the user no active connection is present but the nib is not loading. I also tried loading my other nibs but it also doesn't load the nib.
- (BOOL) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(#"The internet is down.");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"No Internet Connection" message:#"You are currently not connected to a WI-FI or cellular Data.\nPlease make sure you are connected." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
//NoConnection *noConn = [[NoConnection alloc] initWithNibName:#"NoConnecton" bundle:nil];
//[self presentModalViewController:noConn animated:NO];
//[NoConnection release];
self.isConnected = NO;
return NO;
break;
}
//more cases.........
the alert part is working just fine but the part for loading the nib is not. can you tell me whats wrong here? I'm calling this function in viewWillAppear. Thanks!
You can do the following:
if ( ! isConnected )
{
NoConnection *noConn = [[NoConnection alloc] initWithNibName:#"NoConnecton" bundle:nil];
[self presentModalViewController:noConn animated:NO];
[NoConnection release];
}
The code you have presented should work, sow the problem must be somewhere else probably in the nib - linking, you might have forgot to link something to the nib file.
try this
[self.navigationController presentModalViewController:noConn animated:YES];
Does your nib has NoConnection as a File's Owner (I guess NoConnection is a subclass of UIViewController, check it. I'll call this NoConnectionViewController bellow because you should name it like that for no mistake) ?
Is the file's owner view property linked with the graphical view ? Check it.
Are you working without status bar at top of the window ? That could be a problem.
Are your here inside a modalViewController ? If yes, your code won't work, you must use instead :
NoConnectionViewController* nextWindow = [[NoConnectionViewController alloc] initWithNibName:#"NoConnecton" bundle:nil]; // Check your nib name here, seems to be a mistake
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
[self presentModalViewController:navController animated:YES];
[navController release];
[nextWindow release];
You need to use the delegate method of alert view
#pragma mark - AlertView Delegates
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
NoConnection *noConn = [[NoConnection alloc] initWithNibName:#"NoConnecton" bundle:nil];
[self presentModalViewController:noConn animated:NO];
[NoConnection release];
}
}
don't forget to assign tag value of alertView to 1.
and also dont forget to conforms to the UIAlertViewDelegate protocol
Happy Coding :)

Showing alert while calling webservice

i have the following code.
UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
UIActivityIndicatorStyleWhite];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Processing" delegate:self otherButtonTitles:nil];
[alert addSubview:activity];
[activity startAnimating];
[alert show];
WebServiceController *web = [[WebServiceController alloc]init];
NSDictionary *dict = [web getDetails];
The problem is that the alert is not getting displayed. The WebServiceController is an XML parser which gets details from a specified URL and returns them. The alert should be shown while the service is called as it takes time to get the details. But it displays the alert only after the service call is over. Why is that?
because [alert show] will require animation, since the service controller call is taking place on the main thread, the main thread is busy executing the service call, blocking the alert view animation to execute.
You need to perform the ServiceCall on backend thread, see NSOperation or PerformSelectorOnBackgroundThread, make sure you pass in delegate of the ViewController that has the AlertView to the backend thread, callback the delegate as soon as the service call is completed. Make sure you perform the call for the callback on the mainthread using performSelectorOnMainThread. All UI related calls should be executed on the main thread.
Adding to the above post, you have to write the alert like this:
UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
UIActivityIndicatorStyleWhite];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Processing" delegate:self otherButtonTitles:nil];
[alert addSubview:activity];
[activity startAnimating];
[alert show];
[self performSelectorInBackground:#selector(doTheWork) object:nil];
You need to declare a function (doTheWork) which will take care of the web service call as follows:
-(void)doTheWork {
WebServiceController *web = [[WebServiceController alloc]init];
NSDictionary *dict = [web getDetails];
[alert dismissWithClickedButtonIndex:0 animated:YES]; //dismissing the alert
}

Update text in alert message

Still trying to update the message in an UIAlertview while the alert is active. I removed most of the first part of the question and publish more code in the update below.
UPDATE 3: Adding more code!
In the .h file I declare the following (among others):
#interface WebViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *webView;
UIAlertView *alert;
}
I #property and #synthesize the UIAlertview to.
Next I create the alert in an IBAction which is run by a button click:
-(IBAction)convert {
convertButton.enabled = NO;
mailButton.enabled = NO;
backButton.enabled = NO;
//show the alert window
alert = [[UIAlertView alloc] initWithTitle:#"Converting in progress\nPlease Wait..." message:#"\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
[alert setMessage:#"getting roster"];
}
It then jumps to the following function:
- (void)didPresentAlertView:(UIAlertView *)progressAlert {
//A lot of code
[alert setMessage:#"Checking history"];
//more code
[alert setMessage:#"writing history"];
//even more code
[alert setMessage:#"converting roster"];
}
The didPresentAlertView method ends with an ASIHTTPRequest to post data to a webpage and when this request is finished the code finally jumps to the last method to exit the UIAlertView and closes everything up:
- (void)requestFinished:(ASIHTTPRequest *)request {
[timer invalidate];
[alert dismissWithClickedButtonIndex:0 animated:YES];
backButton.enabled = YES;
[alert release];
}
I also removed the autorelease from my UIAlertView init to make sure it exists during the rest of the process.
As it is now, the code only fires the very first setMessage -> 'getting roster' and the very last -> 'converting roster'. The setMessage requests in the middle do not get fired..
Really hope someone can help me here!
Thanks all!
Now I see the problem.
When you update the message property, it doesn't fire redrawing the view right away. The view is marked as 'needed to be drawn', and the actual drawing happens later, typically at the end of the current or next runloop in the main thread.
Therefore, while your didPresentAlertView method is running on the main thread, the alert view is not redrawn until the method is finished. This is why a computation-intensive job needs to run on a separate thread to increase the responsiveness of the UI, as the UI-related job is done on the main thread.
What you should do is run your //A lot of code //more code and //even more code on a separate thread, and update the message property on the main thread.
For example, your code may look similar to :
// this is inside didPresentAlertView method
NSOperationQueue* aQueue = [[NSOperationQueue alloc] init];
[aQueue addOperationWithBlock: ^{
// A lot of code
[alert performSelector:#selector(setMessage:) onThread:[NSThread mainThread]
withObject:#"Checking history" waitUntilDone:NO];
// more code
[alert performSelector:#selector(setMessage:) onThread:[NSThread mainThread]
withObject:#"writing history" waitUntilDone:NO];
// it goes on
}];
If you are working with iOS 4.0 and later, and want to use the GDC, be careful because it may detect independency of your computation and message updates and let them happen concurrently (which is not desired here).

UIAlertView shows up twice

My alertview appears twice and requires 2 user clicks to dismiss.
- (void) showAlert: (NSString *) message
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"You chose"
message: message
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"ok",nil];
av.tag = SLIDER_ALERT;
[av show];
}
I am then releasing the alertview in the delegate method "alertView: (UIAlertView *) alertView clickedButtonAtIndex: (int) index"
I suspect the problem is that I have built my own view hierarchy programmaticaly. I have one parent view for the viewcontroller. Under that i have 2 other views (parentView -> subview1 and subview2). I've tried to call [self.view addSubview: av] but that does not work. Neither does bringToFrontSubView:
Any help would be much appreciated
Peyman
The Alert code is fine (other than the release, mentioned in the comments).
[av show] is all that's required to show a view. You don't add UIAlertViews as subviews.
Call it after a delay of 0.1 sec [self performSelector:#selector(showAlert:) withObject:nil afterDelay:0.10];
Hope this will solve your problem.
With using autorelease the Alert View seems to be twice or 3 times. And for iOS4 it needs to be autoreleased otherwise it will crash.