Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

anuhoho

macrumors newbie
Original poster
May 22, 2011
10
0
Hi

I have a peculiar problem where my NSNotification seems to die in UIAlertView delegate function

I have four options present in a UIAlertView and to handle them i used its delegate class so that at each options I can do some different task. One of those task is to send some data to the server. For that I am using asynchronous request and also I have a loading view popping up whenever user select the option of uploading the data . In the loading view i have a activityindicator scroller which keeps rotating. I close the loading view as soon as I get any response from the server. This i did with NSNotification. But the following code is not working

Code:
#pragma mark -
#pragma mark UIAlertViewDelegate methods

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex == 2)
		{
            //Save to server
            if([self checkemail])
            {
                return;
            }
            
            //get loading screen up
            NSDictionary* loadDict = [NSDictionary dictionaryWithObject:@"Loading..." forKey:@"text"];
            [[NSNotificationCenter defaultCenter] postNotificationName:NTF_LOADING_SCREEN_SHOULD_SHOW object:nil userInfo:loadDict];
            
            //request url
            [self requestForUploading];
            [userManager isLabeltobeShown:4];

		}

The problem is the loading screen stops suddenly when I choose the option above. In ideal scenario it should stop when I again call

[[NSNotificationCenter defaultCenter] postNotificationName:NTF_LOADING_SCREEN_SHOULD_HIDE object:nil userInfo:loadDict];

which I am calling once I get the response from the server. But in my case the loading screen simply dies before i get any response from the server.

Can anyone suggest some ways
 
I have made a class LoadingScreenManager where I have added the observers and that class I have initiated in the application delegate class. I have done this much only.
 
Here is what I did

In the application delegate class I registered the loading screen manager as this:

Code:
    //loading screen manager
	LoadingScreenManager* newLoadingScreenManager = [[LoadingScreenManager alloc] initWithoutArguments];
	self.loadingScreenManager = newLoadingScreenManager;
	[newLoadingScreenManager release];

The loading screen manager class is below. Here LoadingView is a UIView class where defined a UILabel and activity indicator controller.

Code:
- (id)initWithoutArguments
{
	if(self == [super init])
	{
		//loading view
		LoadingView* newLoadingView = [[LoadingView alloc] initWithoutArguments];
		loadingView = [newLoadingView retain];
		[newLoadingView release];
		
		//register for notifications
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadingViewShouldShow:) name:NTF_LOADING_SCREEN_SHOULD_SHOW object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadingViewShouldHide:) name:NTF_LOADING_SCREEN_SHOULD_HIDE object:nil];
        
	}
	
	return self;
}

- (void)loadingViewShouldShow:(NSNotification*)notification
{
	//check userInfo for 'text' key
	NSDictionary* infoDict = [notification userInfo];
	NSString* newText = [infoDict objectForKey:@"text"];
	if([newText length] == 0)
	{
		newText = @"Loading...";
	}
	
	//bring back to view
	[self.loadingView setText:newText];
	if([self.loadingView superview] == nil)
	{
		UIWindow* window = [[UIApplication sharedApplication] keyWindow];
		[self.loadingView setFrame:window.frame];
		[window addSubview:self.loadingView];
		[window bringSubviewToFront:self.loadingView];
		
		[self.loadingView setHidden:NO];
		[self.loadingView.spinner startAnimating];
	}
}

- (void)loadingViewShouldHide:(NSNotification*)notification
{
	if([self.loadingView superview])
	{
		[self.loadingView removeFromSuperview];
		[self.loadingView.spinner stopAnimating];
		[self.loadingView setHidden:YES];
	}
}
- (void)dealloc
{
	[loadingView release];
	
	[super dealloc];
}

In the class where I have used this notification is below

Here I have a button which pops up four options in the alert view and in one of the options I start the loading screen. But the screen dies very quickly just after I start it.

Code:
-(void)saveButton:(id)sender {
...
..
if([buttontext isEqualToString:@"Always Ask"])
    {
        
        UIAlertView *alertSave=[[UIAlertView alloc]initWithTitle:@"Select" message:@"Where do you want to save" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Local Contact",@"Server Connections",@"Both",nil];
        
        alertSave.tag = kAlertViewOne;
		[alertSave show];
	    [alertSave release];

	}
..
}

#pragma mark -
#pragma mark UIAlertViewDelegate methods

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex == 2)
		{
            //Save to Server
            if([self checkemail])
            {
                return;
            }
            //get loading screen up
            NSDictionary* loadDict = [NSDictionary dictionaryWithObject:@"Loading..." forKey:@"text"];
            [[NSNotificationCenter defaultCenter] postNotificationName:NTF_LOADING_SCREEN_SHOULD_SHOW object:nil userInfo:loadDict];
            
            //request url
            [self requestForUploading];

		}

Then in the http delegate class ie.,in the connectionDidFinishLoading class I hide the loading screen

Code:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    
    NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
    
    NSLog(@"Response: %@",responseString);
    
    [responseString release];
    
    [dataWebService release];
    
    //dismiss loading screen
	[[NSNotificationCenter defaultCenter] postNotificationName:NTF_LOADING_SCREEN_SHOULD_HIDE object:nil];
    

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:NO];
    
    
}

But before reaching it only the loading screen dies. Here is the code. Can anyone help me what may be the problem.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.